Read Serial
Using PySerial the following program was created:
import serial
class comunicacao():
def __init__(self, porta, baud):
s = serial.Serial(porta, baud)
data = s.read(18)
data = data
print("Data: ", (data))
comunicacao('COM7', 57600)
It is receiving the number 10000
in decimal for tests, and the print output is: Data: b'\x020000000000002710\x03'
Because 2710 in HEX is 10000 in DEC.
Conversion
So trying to convert with the following ways:
print("Data: ", int(data, 16))
gives teh error:
print("Data: ", int(data, 16))
ValueError: invalid literal for int() with base 16: b'\x020000000000002710\x03'
- With
data = s.read(18).decode()
the print output isData: 0000000000002710
and trying to convert withint()
gives the error:
print("Data: ", int(data, 16))
ValueError: invalid literal for int() with base 16: '\x020000000000002710\x03'
data = data.lstrip("0")
withdata = s.read(18).decode()
didn't strip the leading zeroes.- And
data = data.lstrip("0")
withdata = s.read(18)
gives the error:
print("Data: ", (data.lstrip("0")))
TypeError: a bytes-like object is required, not 'str'
Question
How to convert this data type (I think it is as ASCII, but is a HEX number) to DEC?