Question

I want to preface this with noting that I am really lost right now, so there may be things I say that don't make sense because I have no idea what I'm talking about. Feel free to dissect my question so long as it is helpful.

Alright, so the overall project I'm working on is to write a program in python to interface with a custom built temperature control unit (built with this chip) and copy data from a memory buffer where data points are stored.

I was not the one who programmed it and I'm just going off my limited experience with Arduinos (also I'm new to python). Here is some of what I was sent regarding the commands to interface with it:

#define COMMAND_PING                'p'
//  ->  'p'
//  <-  'p' uuuu
//      uuuu - unique number == 0x03645145

#define COMMAND_GET_RECORD_NUMBER   'i'
//  -> 'i'
//  <- 'i' nnnn
//      nnnn - current record number (not written yet)

#define COMMAND_GET_DATA            'd' //Length to 32-bit boundary
//  ->  'd' ssss llll
//  <-  'd' ddddddd...
//      ssss - starting address (should, but probably don't need to be at 32-byte boundary)
//      llll - data length - this must be a multiplicity of 32
//      ddddddd... - data returned

I can use con.write('p') just fine and then using con.readline() I get back \x00\x00pEQd\x03 which converted to hex is 7045516403 (note values sent and returned are little endian).

My problem is with the 'd' command. I think I need to send 'd' as a string along with two 32-bit binary values but I'm not sure how to do that. I'm hardly even sure of the right questions to ask, any help you guys can give would be highly appreciated.

Looking through internet resources (for hours) I think I might want to use struct or maybe something like this? I'd really love an example showing the whole process of connecting, writing, and reading to/from the serial port.

Was it helpful?

Solution

Something like

port.write(struct.pack('<cii', 'd', address, length)
d = port.read(1)         #'d' expected
data = port.read(length) #length bytes of data expected
if d != 'd' or len(data) < length:
    raise Exception("Bad response received")

where < specifies little-endian byte order, c is a single char and i is a 32-bit int.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top