1

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.

1
  • Yes, struct module is what you need. Commented May 19, 2013 at 12:35

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This looks really promising! I'll let you know once I get into work in a few hours.
Yep! It is working beautifully! Here is what I am using for future reference: con.write(struct.pack('<cii','d',address,length) data = con.read(33) data = struct.unpack('<ci3h6b8h',data) and then data[x] to access any specific element in the tuple. Thanks so much!
For some reason double spacing isn't giving me a line break... :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.