harmsm / PyCmdMessenger

Python interface for CmdMessenger arduino serial communication library
MIT License
80 stars 32 forks source link

Add ability to receive byte strings #33

Open iFreilicht opened 6 years ago

iFreilicht commented 6 years ago

Right now, it is impossible to receive arbitrary byte-strings.

I wrote a simple setup where my python-script sends the character '\xff' and the Arduino sends that back.

When a string is received where any characters are larger than 127, the receive() method fails:

  File "[...]", line 20, in <module>
    main()
  File "[...]", line 15, in main
    response = messenger.receive()
  File "[...]\Python\Python36-32\lib\site-packages\PyCmdMessenger\PyCmdMessenger.py", line 284, in receive
    received.append(self._recv_methods[arg_format_list[i]](f))
  File "[...]\Python\Python36-32\lib\site-packages\PyCmdMessenger\PyCmdMessenger.py", line 593, in _recv_string
    s = value.decode('ascii')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

So I propose adding the format character 'B' to indicate a byte string. This will receive the byte string just like _recv_string does, but will just return it without decoding it or stripping any character.

For sending, behaviour should be equivalent to 's' as that already works perfectly fine.

I'll implement this and create a pull request when I'm done.

ilovetogetspamed commented 6 years ago

Any ETA on when this Pull Request is going to be added? I could really use this feature for 1-Wire addresses which look like '\x12\x5C\xEC\xA0\x00\x00\x00\x80' (64-bits). Thanks.

iFreilicht commented 6 years ago

It seems the author hasn't had time to respond to any issues for weeks. In the meantime, you could just clone from my fork and then set this original repo as the remote once the changes are merged.

ilovetogetspamed commented 6 years ago

Will do, thanks.

On Dec 11, 2017, at 9:11 AM, Felix Uhl notifications@github.com wrote:

It seems the author hasn't had time to respond to any issues for weeks. In the meantime, you could just clone from my fork and then set this original repo as the remote once the changes are merged.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/harmsm/PyCmdMessenger/issues/33#issuecomment-350734924, or mute the thread https://github.com/notifications/unsubscribe-auth/ADHZnB3EvNK7Q8WIlu0l_N-Ri7L1k-4Uks5s_TgmgaJpZM4Qk7W_.

ehiverson commented 6 years ago

I'm trying to use this code with a teensy 3.6. I've made sure the baud rates all match between the ArduinoBoard class instance, the sketch on the teensy, and the pingpong_test.py script. Also, I updated the data type sizes of the ArduinoBoard class to match here: https://forum.pjrc.com/threads/36658-datatypes-bit-depth But I keep getting errors from struct.unpack struct.error unpack requires a buffer of 2 bytes I commented out the 'kBBool', 'kBByte', and 'kChar', and found that 'kBInt16' tests pass up to -65536, each showing 'caught overflow', which I'm not sure is good or bad, but then it fails again from struct.unpack. Any reason the smaller types shouldn't be working?

EDIT: I had the wrong sketch uploaded onto the micro. So now it works!