aster94
aster94

Reputation: 357

pySerial reading hex value from MCU

i am new to python, i am using 2.7 with spyder

i have an anduino style board running this:

void setup() {
  Serial1.begin(115200);
}

void loop() {
  Serial1.write(0x80);
}

on my laptop i am not able to read the incoming data neither i am able to assing it to a variable:

# -*- coding: utf-8 -*-
import serial
print (serial.__version__)
#3.4
ser = serial.Serial(
    port='/dev/ttyUSB1',
    baudrate=115200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout = None
    #timeout = 1
)

while 1:
    print ser.read()
    a = ser.read()
    print a

i really don't understand what i am doing wrong, when i try to assign ser.read() to "a" spyder crash i just would like to read the incoming hex value

Upvotes: 2

Views: 19396

Answers (2)

Ross
Ross

Reputation: 131

This way also works:

in_hex = ser.read().hex()

Upvotes: 3

aster94
aster94

Reputation: 357

solved it with this:

print hex(int(a.encode('hex'), 16))

i hope it could be usefull now i am trying to understand why pyserial crashes so often

edit: upgrading to python 3.x solved all the crashed with pyserial and also the sintax became more clear

in_bin = ser.read()
in_hex = hex(int.from_bytes(in_bin,byteorder='little')) 

Upvotes: 5

Related Questions