7

I'd like to test the content of a variable containing a byte in a way like this:

line = []
while True:
    for c in self.ser.read(): # read() from pySerial
        line.append(c)
        if c == binascii.unhexlify('0A').decode('utf8'):
            print("Line: " + line)
            line = []
            break

But this does not work... I'd like also to test, if a byte is empty: In this case

print(self.ser.read())

prints: b'' (with two single quotes)

I do not until now succeed to test this

if self.ser.read() == b''

or what ever always shows a syntax error...

I know, very basic, but I don't get it...

6
  • what syntax error? Commented Oct 13, 2016 at 8:30
  • Could it simply be that you're missing the colon at the end of your if statement? Please paste a complete sample of code that is giving you a syntax error. Commented Oct 13, 2016 at 8:35
  • Remeber to ALWAYS include the error message you get. This helps narrowing down the possible solution and pin-point the problem. stackoverflow.com/help/how-to-ask Commented Oct 13, 2016 at 8:45
  • It should be: if self.ser.read() == "b''" Commented Oct 13, 2016 at 9:50
  • @Metareven I don't think so. b'' is an empty bytes, which is the correct thing to compare against. "b''" is a three character str (which in Python 2 happens to be the same thing as bytes, but we don't know what Python version is in use here). Commented Oct 13, 2016 at 10:05

2 Answers 2

3

Thank you for your help. The first part of the question was answerd by @sisanared:

if self.ser.read():

does the test for an empty byte

The second part of the question (the end-of-line with the hex-value 0A) stil doesn't work, but I think it is whise to close this question since the answer to the title is given.

Thank you all

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

Comments

2

If you want to verify the contents of your variable or string which you want to read from pySerial, use the repr() function, something like:

import serial
import repr as reprlib
from binascii import unhexlify
self.ser = serial.Serial(self.port_name, self.baudrate, self.bytesize, self.parity, self.stopbits, self.timeout, self.xonxoff, self.rtscts)
line = []
while 1:
    for c in self.ser.read(): # read() from pySerial
        line.append(c)
        if if c == b'\x0A':
            print("Line: " + line)
            print repr(unhexlify(''.join('0A'.split())).decode('utf8'))
            line = []
            break

3 Comments

Why not: if c == b'\x0A': ?
Yes, I think it's reasonable
while True: print("read_line_loop") for c in self.ser.read(): line.append(c) print("appended") if c == b'\x0A': print("stop condition") print("Line: " + line) 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.