Mar-01-2018, 03:56 PM
Hi
maybe someone could have a look at my code or suggest a better one (or another place to look for)
I need to do serial communication with a device: write to device and listen to what it is saying
I should write a command (send some bytes). I finish my command sendind byte 0XFF (chr(255)) 3 times in a row
Then the device sends me a reply that i have to read. This reply also finishes after 3 times 0XFF
I wrote this
It's not working, why?
maybe someone could have a look at my code or suggest a better one (or another place to look for)
I need to do serial communication with a device: write to device and listen to what it is saying
I should write a command (send some bytes). I finish my command sendind byte 0XFF (chr(255)) 3 times in a row
Then the device sends me a reply that i have to read. This reply also finishes after 3 times 0XFF
I wrote this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import serial ser = serial.Serial( port = '/dev/ttyAMA0' , baudrate = 9600 , parity = serial.PARITY_NONE, stopbits = serial.STOPBITS_ONE, bytesize = serial.EIGHTBITS, timeout = 1 ) ser.reset_output_buffer() print ( 'Serial connected' ) def nxRead(value): #first i need to send a 'command' (for example 'sendme X.value' command = 'sendme X.value' ser.write(command.encode( 'latin-1' )) ser.write( chr ( 255 ).encode( 'latin-1' )) # 0xFF ser.write( chr ( 255 ).encode( 'latin-1' )) # 0xFF ser.write( chr ( 255 ).encode( 'latin-1' )) # 0xFF #by now the device should have understood my request and will send me a reply byte by byte, finishing with 0XFF 0XFF 0XFF reply = [] count = 0 t3FF = False # the termination has not stopped while t3FF = = False : r = ser.read().decode('latin - 1 ) c = ord (r) reply.append(c) if c = = 0xff : count = count + 1 if count = = 3 : t3FF = True return else : count = 0 return s |