[Tutor] Passing Data to .DLL

Alan Gauld alan.gauld at btinternet.com
Wed Oct 22 11:52:25 CEST 2014


On 22/10/14 01:04, Wilson, Pete wrote:
> The python code is below..
>
> ''' read_bat.py
> '''

You are using triple quoted strings as a commenting feature.
Thats good practice where you want a docstring to appear in
the help() screen but its not so good for comments like the
above or where you are commenting out code blocks because
the quotes are esy to miss and hard to spot the closing
match.  (OK in an IDE because it colours the string but
in a mail message its much harder to see)

Could you, when posting long bits of code, please remove
the commented out sections - it makes us (well me at least)
much more willing to study the code. I've tried to remove
the redundant sections below, and made a few style comments.
But apart from the spurious 'comment' at the end of the
calling line I don't see anything obvious.

> from serial import *
> from string import *

Are you sure you need string? It is very rarely used and I
don't see it anywhere in your code?

> from time import *

Also in general its bad practice to use this style of import.
The risk of name collisions is quite high. If you don;t like the long 
names use an alias:

import serial as ser

for example.

> system_state = "G2G"
>
> #Set-up and register cb_send_serial_data
> from ctypes import *

And its best to keep the imports all together

> pt_dll = CDLL("c:/py_stuff/ProductionTest.dll")
> SendSerialData_t = CFUNCTYPE(None, POINTER(c_uint8), c_uint8)
>
> reg_send_serial_data = pt_dll.RegSendSerialData
> reg_send_serial_data.argtypes = [SendSerialData_t]
> reg_send_serial_data.restype = None
>
> global new_serial_tx_data
> global new_serial_size

You don't need the global keyword at the module level,
its only needed inside the function to indicate that
the variable is non-local.

> def send_serial_data(tx_data, size):
>      # testing
>      print "tx_data = ", tx_data
>      print "size = ", size
>      print "tx_data[:size] = ", tx_data[:size]
>
>      global new_serial_tx_data
>
>      new_serial_tx_data = tx_data[:size]
>
>      global new_serial_size

Its traditional to put all the global declarations together
at the top of the function.

>      new_serial_size = size
>
> cb_send_serial_data = SendSerialData_t(send_serial_data)
> global cb_send_serial_data
> reg_send_serial_data(cb_send_serial_data)
> print "reg_send_serial_data done"
>
> #Set-up and register cb_bat_vol_read
> #this triggers send_serial_data when it is registered.
>
> BatVolReadRequest_t = CFUNCTYPE(None, c_uint16, c_uint8)
> prod_bat_vol_read_request = pt_dll.ProdBatVolReadRequest
> prod_bat_vol_read_request.argtypes = [BatVolReadRequest_t]
> prod_bat_vol_read_request.restype = None
>
> def bat_vol_read(bat_vol, status):
>      print "bat_vol_read()"
>      print bat_vol, status
>
> cb_bat_vol_read = BatVolReadRequest_t(bat_vol_read)
>
> prod_bat_vol_read_request(cb_bat_vol_read)
> print "prod_bat_vol_read_request done"
>
> #set-up ProcessingIncomingSerialData
>
> print "breakpoint"
>
> class rx_data_t:
>      def _init_(self):
>          self.data = []
>          self.size = ''
>
> fake_rx_data = rx_data_t()
> fake_rx_data.data = ['\x01', '\x05', '\x00', '\x1c', '\x00', '\x99',
> '\x0c', '\x04']
> fake_rx_data.size = 8

I'm not sure why you used a class here. But if you are
using one you might as well make the __init__() do
something useful:

class rx_data_t:
     def _init_(self,data=[],size=None):
         self.data = data
         self.size = size

And then initialize it on creation:

fake_rx_data = rx_data_t(['\x01','\x05','\x00','\x1c',
                           '\x00','\x99', '\x0c','\x04'],
                           8)

But since the class doesn't do anything(no methods)
you might as well just use a tuple or dictionary.

fake_rx_data = {data: ['\x01','\x05','\x00','\x1c',
                        '\x00','\x99', '\x0c','\x04'],
                 size: 8}


> print "fake_rx_data.data = ", fake_rx_data.data
> print "fake_rx_data.size = ", fake_rx_data.size
>
> ProcessIncomingSerialData_t = CFUNCTYPE(None, POINTER(c_uint8), c_uint16)
> process_incoming_serial_data = pt_dll.ProcessIncomingSerialData
> process_incoming_serial_data.argtypes = [ProcessIncomingSerialData_t]
> process_incoming_serial_data.restype = None
>
> yummy_thing = ProcessIncomingSerialData_t(fake_rx_data)passing pointers to

Is there supposed to be comment marker after the parens?

> process_incoming_serial_data(yummy_thing)
> print "Done."
> print "system_state = ", system_state

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list