Python support?

All about Sub-20 Multi Interface USB Adapter USB to I2C, SPI, GPIO, RS232, RS485, Ir, LCD

Moderator: serg

mmintz
Posts: 3
Joined: Mon Jan 03, 2011 10:01 pm

Python support?

Post by mmintz »

Hi,

Is anyone out there using Python to call the sub20.dll ?

I am getting an error:

>>>from ctypes import *
>>> welper = windll.LoadLibrary("C:\\Windows/System32/sub20")
>>> welper.sub_open ()
0
>>> welper.sub_errno ()
Traceback (most recent call last):
File "<pyshell#173>", line 1, in <module>
welper.sub_errno ()
WindowsError: exception: access violation writing 0x0043ED1C

I realize I need to send a parameter into sub_open(). I am using Python 3.0

Take Care,
<mmintz> at gmail

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: Python support?

Post by serg »

Use "cdll" instead of "windll"

mmintz
Posts: 3
Joined: Mon Jan 03, 2011 10:01 pm

Re: Python support?

Post by mmintz »

I had tried that. Same behaviour.

>>> helper = cdll.LoadLibrary("C:\\Windows/System32/sub20")
>>> helper.sub_open ()
0
>>> helper.sub_errno()
Traceback (most recent call last):
File "<pyshell#203>", line 1, in <module>
helper.sub_errno()
WindowsError: exception: access violation writing 0x0043ED1A

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: Python support?

Post by serg »

Hi,

- sub_open should return a valid handle. In your case however it is 0 i.e the SUB-20 board is not connected, probably. Call this function with a parameter, i.e helper.sub_open (0)
- sub_errno is a variable, not a function, see the user manual doc or header file for more info

Regards

mmintz
Posts: 3
Joined: Mon Jan 03, 2011 10:01 pm

Re: Python support?

Post by mmintz »

Hi Serg,

OK, I have it working. Here is some example code (file mdio.py):

---------------------
from ctypes import *

#need to raise exception if dev_id is 0
def get_device_info ():
"""
Returnes a string which has the aaa product name and serial number
of the mdio interface device
"""
helper = cdll.LoadLibrary("C:\\Windows/System32/sub20")
dev_id = helper.sub_open (0)
errno = c_int.in_dll (helper, "sub_errno")
print ("device id is: {0} sub_errno is: {1}".format (dev_id, errno.value))

sub_get_product_id = helper.sub_get_product_id
sub_get_product_id.argtypes = [c_int, c_char_p, c_int]
s = create_string_buffer('\000' * 60)

sub_get_product_id (dev_id, s, 60)
#print ("Product ID:", s.value.decode("utf-8"))


sub_get_serial_number = helper.sub_get_serial_number
sub_get_serial_number.argtypes = [c_int, c_char_p, c_int]
s2 = create_string_buffer('\000' * 67)

sub_get_serial_number (dev_id, s2, 67)
#print ("Serial Number:", s2.value.decode("utf-8"))

helper.sub_close (dev_id)

return "Device: " + s.value.decode("utf-8") + " " + s2.value.decode("utf-8")

def transaction (op_code, phy_adrdess, device_address, data):
"""
Execute a mdio cluase 45 transaction.
Returnes an integer on read or read post increment.
op_code
0 is for Address
1 is for Write
2 is for Read
3 is for Read post increment
"""
helper = cdll.LoadLibrary("C:\\Windows/System32/sub20")
dev_id = helper.sub_open (0)
errno = c_int.in_dll (helper, "sub_errno")
print ("device id is: {0} sub_errno is: {1}".format (dev_id, errno.value))

sub_mdio45 = helper.sub_mdio45
sub_mdio45.argtypes = [c_int, c_int, c_int, c_int, c_int, POINTER (c_int)]

i = c_int(1)
returned = pointer(i)
foo = sub_mdio45 (dev_id, op_code, phy_adrdess, device_address, data, returned)
errno = c_int.in_dll (helper, "sub_errno")
print ("returned is: {0} sub_errno is: {1}".format (foo, errno.value))

helper.sub_close (dev_id)

return returned[0]

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: Python support?

Post by serg »

hi mmintz,

Thank you for publishing your work. We will refer our customers to your Python script in case somebody is interested.

Happy New Year !

sub-20 support

mfinch
Posts: 1
Joined: Fri Oct 07, 2011 8:10 pm

Re: Python support?

Post by mfinch »

I am having trouble reading the variables out of the dll. I can read error number, but the SPI parameters all yield a "not found" error. If there is something obvious I am doing wrong, or if you can publish the values hidden i the dll I would appreciate it.
Mike
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> from ctypes import *
>>> helper = cdll.LoadLibrary("C:\\Windows/System32/sub20")
>>> errno = c_int.in_dll (helper, "sub_errno")
>>> SPI_SLAVE = c_int.in_dll(helper,"SPI_SLAVE")
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
SPI_SLAVE = c_int.in_dll(helper,"SPI_SLAVE")
ValueError: symbol 'SPI_SLAVE' not found
>>>

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: Python support?

Post by serg »

Hello Mike
There is nothing hidden in the sub20.dll. The SPI_SLAVE and all the other constants are C/C++ preprocessor definitions, located in the C:\Program Files\SUB-20\inc\libsub.h header file. You can use this file as a reference.

siguy
Posts: 12
Joined: Thu May 10, 2012 11:29 pm

Re: Python support?

Post by siguy »

Hello, I'm having issues with MDIO0 and MDIO1 talking to a known good MDIO target - it works well with the FDI USB-MPC. However, with SUB-20 (and either SUBTOOL or Python using your supplied DLL), the MDC never toggles. This is the same for MDIO0 and MDIO1 channels. The target appears to be behaving correctly per MDIO spec, but the SUB-20 never starts the clock?

The Python example above was useful and I can do sub_open() on the SUB-20 with no errors. However, on sub_mdio45() or sub_mdio_xfer_ex() READ calls, I always get back errno=5 - Failed to setup async transaction
What could cause this error? Is there a missing step in my setup of the SUB-20 that is also missing from the forum example?

Thanks!
SIguy

siguy
Posts: 12
Joined: Thu May 10, 2012 11:29 pm

Re: Python support?

Post by siguy »

A little more clarification - the MDC never toggles, but is held low by the SUB-20 the whole time. The MAX3397 level translator that sits between the SUB-20 and the target has internal 10k pullups. They are holding MDIO high correctly, and the SUB-20's MDIO line is 3-stated.

If I disconnect the wire between SUB-20 and the MAX3397, the wire is pulled high, so the SUB-20 has MDC stuck low. This is the case regardless of what MDIO transaction I'm trying to initiate on MDIO0 or MDIO1

Post Reply