3

I am trying to send one byte (128) to a device through serial port and expect to receive back 81 in Hex[space].

Note: The device is automatically transmitting the data it has. Before running Python script, I first tested whether my serial connection is working. First I tested the serial connection with a terminal emulation program called RealTerm: Serial/TCP Terminal.

For program configuration, I set Port baud rate to 115200 and kept everything else to default. I set display as Hex[space].

For testing, I connected the device to PC through serial port (rs232), I first sent 128 and successfully received 82 as response from the device. second, I sent 0x82 and successfully received all stored data in the device.

enter image description here

enter image description here

I tested also to see whether the data is read whenever it is available in buffer but it seems that python is reading before writing.

enter image description here

The Python script is just to send first byte, that is 128. I am expecting to have as output 82.

import serial, struct

rs232 = serial.Serial(
    port = 'COM6', 
    baudrate = 115200, 
    bytesize = serial.EIGHTBITS,
    parity = serial.PARITY_NONE,
    stopbits = serial.STOPBITS_ONE
    )
print(rs232.isOpen)
if rs232.isOpen() == True:
    bytes_to_send = struct.pack('B', 128)
    device_write = rs232.write(bytes_to_send)
    device_read = rs232.read(1)
    print(hex(int.from_bytes(device_read, byteorder='little')) )
else:
    print('rs232 is not open.')
    rs232.close()

The output of the script is: b'\x00'.

I am afraid, I am confusing in data conversion which is why I am not receiving the correct output.

Joe
  • 575
  • 6
  • 24
  • What does "[space]" imply after the word "Hex" in both cases? Your question confusingly refers to both decimal and hex values without distinction. Use the `0x` prefix of `h` suffix to be explicit and probably best to use on or the other not both. Also you state that the expected response to 0x80 is 0x81, but then say you received 0x82. Check the apparent contradiction. – Clifford Jul 23 '22 at 17:04
  • 2
    "python is reading before writing" it is not clear what you mean by that, or how you have drawn that conclusion. What does the timing diagram indicate? – Clifford Jul 23 '22 at 17:05
  • More likely that the Rx buffer is not empty when you transmit the 0x80 - you might do well to `serial.reset_input_buffer()` before the initial write to discard any stray data. – Clifford Jul 23 '22 at 17:15
  • Thanks a lot, @Clifford. I am new to software embedding, sorry for some confusion. For me to extract the whole data stored in the device, I need to send, first, one byte (0x80) and expect to receive a value of 0x81 from the device. Second, I need to send one more byte (0x81) then I expect to receive the entire data. I tested this using Matlab and RealTerm software. – Joe Jul 23 '22 at 17:25
  • "python is reading before writing" means that from the timing diagram I can see that it is trying to read before even receiving the data, for this, I am getting the buffer empty (0x00). – Joe Jul 23 '22 at 17:26
  • But my point is that the timing diagram is unannotated and you have not described how it was generated or what it represents or even proves, it is just lines and numbers and provides no information. – Clifford Jul 23 '22 at 22:06
  • sorry about that, that plot was generated by a software engineer who works in embedded systems. I don't have access to it nor know about embedded systems. It seems you're working on embedded systems. I would appreciate your help though on. Thank you! – Joe Jul 23 '22 at 23:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/246705/discussion-between-joe-and-clifford). – Joe Jul 24 '22 at 00:59
  • it seems when I run Python in Terminal, I write line by line, without storing when writing and reading into variables, I write `0x80` then read, I can receive `b'\x87'`. I am not sure why It gives `b'\x87'` even if I do `rs232.read(size=100)`, I get `b'\x87\x87\x87\x87\x....` all values are same!! – Joe Jul 24 '22 at 02:18
  • I open the question here: https://stackoverflow.com/q/73095561/10543310 – Joe Jul 24 '22 at 03:35
  • Why do the screen captures indicate (in the bottom status line) that `Port 5` is accessed, but your Python script opens 'COM6'? "*python is reading before writing*" -- Typically you have no indication of when your program issues its read or write requests unless you explicitly add indicators, such as printing a timestamped message or toggle a GPIO/LED. So you need to explain in detail what that timing diagram represents for it to have any utility. If you cannot explain what it is, then it is not useful for basing a question on it. – sawdust Jul 26 '22 at 09:11

0 Answers0