0

I am trying to send a value over serial port. I am unsure how to convert it into the correct format. I tried bin() for binary and format() but it didn't work.

result = 2
ser.open()
ser.write(b'1')
time.sleep(3)

ser.write(result)                 # wrong format
ser.write("{0:b}".format(result)) # TypeError: unicode strings
                                  # are not supported, please
                                  # encode to bytes: '10'

The first write operation sends '1' as binary string. Now I want the variable to be send as binary string as well.

1
  • It is called bytes string. write() method accept a bytes string parameter, not a binary representation string parameter. Posting module you are using would help a lot but forget about it. Commented Nov 17, 2017 at 18:44

3 Answers 3

1

write() requires a bytes object.

>>> help(serial.Serial.write)
Help on function write in module serial.serialwin32:

write(self, data)
    Output the given byte string over the serial port.

To convert an integer to bytes, call int.to_bytes().

>>> result = 2
>>> b = result.to_bytes(4, 'little')
>>> b
b'\x02\x00\x00\x00'
>>> # to convert back to an integer
>>> int.from_bytes(b, 'little')
2
Sign up to request clarification or add additional context in comments.

Comments

1

Like this :

import binascii

def write(num):
    pack = binascii.unlexlify("%04X"%num)
    ser.write(pack)

Important point: Which number system used on device (8,16,32,64 bit) ?

8 Bit = 1 Byte(0-255)

16Bit = 2 Byte(0-65535)

32Bit = 4 Byte(like upside)(0-4294967295)

All ranges are UNSIGNED(look up), but float got extra definition !

You can't type binary 1 value with keyboard :

binascii.unlexlify("%01X"%1) so equal to \x01 (of course you can use struct package)

Comments

1

write() method accepts a string parameter. You can convert result to string with str() builtin function like this.

result = str(result)

Python 2:

result = 2
ser.open()
ser.write(b'1')
time.sleep(3)

ser.write(str(result))

Python 3:

You have to encode string in bytes.

result = 2
ser.open()
ser.write(b'1')
time.sleep(3)

ser.write(str(result).encode('utf-8'))

8 Comments

In Python 3 strings are different to bytes. OP's code strongly implies he's only Python 3.
@Reti43 I do not understand you. In Python 3, string is Unicode by default. OP wants just to write string representation of variable. OP does not want to pack integer in bytes, he wants to write it as string.
Read the error properly. The function does not accept a string object, but a bytes object. TypeError: unicode strings are not supported, please encode to bytes: '10'
@Reti43 1. OP do not imply he is using Python 3. It may be Python 2 too with from future import unicode. 2. It does not make sense to convert result to string representation of bits in bytes. 3. I will update answer for Python 3 too.
Regardless of whether he's using Python 3, or unicode in Python 2, whch I'll grand you, the underlying point was that you were suggesting a solution with strings when the function (regardless of Python version) explicitly disliked. At least you've addressed that now, so your answer is good. While it's only a minor detail, your answer is somewhat unorthodox because it converts a number into a string and converts that to bytes. An integer with 5 digits will end up having 5 bytes. It's just more direct to translate a number straight to its bytes representation, but that's nitpicking.
|

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.