0

I am trying to read and store data in a csv file in packets saved every 5 seconds from a rs232 serial port using Python. An example of a packet to be stored, in each row, is: 308 921 q53 246 133 137 022 1 0 1 1 1 130 C13 330 0000000199 04002201 (there are thousands of different csv rows like this) where each number has meaning which I will need to plot after.

My problem is exact same this like this one: Encode/decode data from RS232 serial port I tried their solution but still did not work for me.

I tried the following code:

import serial

rs232 = serial.Serial('COM6', 115200, timeout=2)
print(rs232.isOpen)
if rs232.isOpen() == True:
    while True:
        size = rs232.inWaiting()
        if size:
            data = rs232.read(size)
            print(data)
        else:
            print('No Data Found!')
        time.sleep(1)
else:
    print('rs232 is not open.')
    rs232.close()

The output was:

<bound method SerialBase.isOpen of Serial<id=0x24a180511c8, open=True>(port='COM6', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=2, xonxoff=False, rtscts=False, dsrdtr=False)>
No Data Found!
b'\x00'
No Data Found!
No Data Found!
No Data Found!

The loop continues for infinity printing only No Data Found!.

The desired output should be a csv file where each row will be something similar to the one given above. That sample row was generated using a software written in a non standard programming language.

I also tried this code (https://stackoverflow.com/a/45458473/10543310) and the output for reading was always empty.

I am using Python v3.7.13, pyserial v3.5 on Windows 10.

Joe
  • 575
  • 6
  • 24
  • Seems like you're dealing with one too many unknowns at once: your code is untested, and you don't know if your serial connection is working. So simplify your setup, and test incrementally. First test the serial connection with a terminal emulation program such as putty or teraterm. After you have an understanding of what/how/when this data is sent to your PC, then work on your program. "*My problem is exact same ... but still did not work for me*" -- Instead of a negative summation, describe what does happen (i.e. only what you actually observe), and why that is a problem. – sawdust Jul 20 '22 at 01:34
  • Thank you! That helped me a lot. Unfortunately, I am still facing difficulties reading the data. I opened a new post here: https://stackoverflow.com/q/73054548/10543310 describing details including your useful testing steps. – Joe Jul 20 '22 at 17:34

1 Answers1

0

I haven't used the python serial function inWaiting(), how I would code it would be something like this:

while True:
    data = ''
    for c in rs232.read(): 
        data += str(c)
    if (data == ''):
        print('No Data Found!')
    else:
        print(data)      
    time.sleep(1)

Other than that, it can only be that no data is actually being sent to COM6. Do you have any code that is doing the sending part of this?

  • This is my first time to work with serial port data. I don't have code that does the sending part, I am not sure whether I need that. The device already contains data and I just need to read it and store it in `csv` file. – Joe Jul 19 '22 at 18:28
  • The code provided throw a traceback: `TypeError: can only concatenate str (not "int") to str` – Joe Jul 19 '22 at 18:31
  • Unless the device is automatically transmitting the data it has, that could definitely be the problem. Otherwise, it may be a good idea to also verify that COM6 is the correct port to receive the data on. You can use device manager on Windows, and verify that it is actually connected on COM6 – Pootis Pootingburg Jul 19 '22 at 18:34
  • I fixed it. Do "data += str(c)" instead of just "data += c" – Pootis Pootingburg Jul 19 '22 at 18:36
  • Yes, COM6 is connected and I used a software to make sure I am connected to the device. How to know whether the device is automatically transmitting the data it has? Is there any solution for this? – Joe Jul 19 '22 at 18:37
  • Thanks for the update, the problem remain same. – Joe Jul 19 '22 at 18:39
  • How to solve the problem if the device automatically transmit the data it has? – Joe Jul 19 '22 at 18:43
  • Well if you are still not getting anything than that must mean that the device is not transmitting anything, assuming that the port is correct and everything is wired up correctly. What device is this? – Pootis Pootingburg Jul 19 '22 at 18:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/246589/discussion-between-joe-and-pootis-pootingburg). – Joe Jul 19 '22 at 18:46