I am trying to use PySerial to automate some data collection but I don't understand how the device is asking me to pass/write the commands.
The pseudocode is as follows:
Structure{
WORD Handle //Reserves address space of a 2 byte word to store Handle
DWORD ParameterA[] // Reserves address space of one 4 byte word to store ParameterA (4 byte word is a double word)
DWORD ParameterB[6] //Resereves address space of 6 double words to store an array of 6 parameters called ParameterB
} PackedData
... // Later in program
Handle = 1200
ParamaterA = 1
SendStringToSerialPort(PackedData, 6)//This routine transfers the data found at theaddress of structure PackedData to the serial port. 6bytes used, 2 for the Handle, 4 for the DWORD ParameterA
Here is a link to the original document if it's helpful: http://www.gentec-eo.com/Content/downloads/user-manual/User_Manual_SOLO_2_V7.pdf on pg.40
Here is how I have interpreted it so far but I know it's not correct, as it only writes 5 bytes.
import serial
ser = serial.Serial()
ser.baudrate = 9600
ser.port = 3
ser.open()
class PackedData:
Handle = 1200
ParameterA = bytearray(0)
ParameterB = bytearray(6)
powerMeter = PackedData()
powerMeter.ParameterA = long(1)
print(ser.write(str(PackedData.Handle)+ str(powerMeter.ParameterA)))
Is anyone able to tell me where I am going wrong?
You're sending 8-bit characters (strings) whereas the protocol described in your question is sending packed bytes. Use the struct
module to convert what you are sending to packed bytes:
import struct
print ser.write(struct.pack("<HL", PackedData.Handle, powerMeter.ParameterA))
You may need to fix endianess (use ">" or "<" in front of the struct format string). And of course, adapt the format to what is expected by the remote device (in the example above, I assumed unsigned integers).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.