2

I am aiming to write a code that will be indefinitely listening and reading from to a serial port that will have this output produced every few seconds

serial port output:

aaaa::abcd:0:0:0
//printf("%d\n",data[0]);
2387
//printf("%d\n",data[1]);
14
-9
244
-44
108

I want the data to be appended in a list like this, python supposed output

[abcd::abcd:0:0:0, 2387, 14, -9, 244, -44, 108]

I tried this code amongst many others but nothing worked, I keep on getting no output EDIT- the code below gives me this output

'''[['abcd::', 'abcd::', 'abcd::', 'abcd::', 'abcd::']] #or
[['abcd::abcd:0:0:c9\n', '2406\n', '14\n', '-7\n']] # and so on, different output for each iteration''' 
#[['aaaa::c30c:0:0:c9\n', '2462\n', '11\n', '-9\n', '242\n', '-45\n', '106\n']] apparently it worked only once. 


ser = serial.Serial('/dev/ttyUSB1',115200, timeout=10)
print ser.name
while True:
    data = []
    data.append(ser.readlines())
    print data 
    # further processing 
    # send the data somewhere else etc
print data
ser.close()
Ahmed Al-haddad
  • 805
  • 2
  • 16
  • 41

1 Answers1

3

readline will keep reading data until read a terminator(new line). please try: read.

UPDATED:

use picocom -b 115200 /dev/ttyUSB0 or putty(serial model) to detect the port and baud-rate is right. I got two different ports in your two questions.if open error port, read() will keep waiting until read a byte. like this:

import serial
# windows 7
ser = serial.Serial()
ser.port = 'COM1'
ser.open()
ser.read() # COM1 has no data, read keep waiting until read one byte.

if you type this code in console, console will no output like this:

>>> import serial
>>> ser = serial.Serial()
>>> ser.port = 'COM1'
>>> ser.open()
>>> ser.read()
_

we need add timeout for read to fix it.
you can try this:

import serial
import time

z1baudrate = 115200
z1port = '/dev/ttyUSB0'  # set the correct port before run it

z1serial = serial.Serial(port=z1port, baudrate=z1baudrate)
z1serial.timeout = 2  # set read timeout
# print z1serial  # debug serial.
print z1serial.is_open  # True for opened
if z1serial.is_open:
    while True:
        size = z1serial.inWaiting()
        if size:
            data = z1serial.read(size)
            print data
        else:
            print 'no data'
        time.sleep(1)
else:
    print 'z1serial not open'
# z1serial.close()  # close z1serial if z1serial is open.
Mitchell Chu
  • 137
  • 5
  • but I have a terminator at the end of each line, so I thought that I can store each line at a time. – Ahmed Al-haddad Mar 11 '16 at 10:44
  • Be carefully when using readline(). Do specify a timeout when opening the serial port otherwise it could block forever if no newline character is received. Also note that readlines() only works with a timeout. readlines() depends on having a timeout and interprets that as EOF (end of file). It raises an exception if the port is not opened correctly. -- pySerial’s doc – Mitchell Chu Mar 14 '16 at 09:26
  • So how to go about it without the timeout? Because I may receive the input at different and random times. – Ahmed Al-haddad Mar 15 '16 at 13:38
  • `inWaiting()` or `in_Waiting()`(v3.0+) return the number of bytes in the input buffer. try `read()` when `inWaiting()` not 0.You can get more information at [pySerial's Doc](http://pythonhosted.org/pyserial/). – Mitchell Chu Mar 16 '16 at 01:40
  • I tried the following based on what you said: `while 1: time.sleep(0.2) data_left = ser.inWaiting() if data_left != 0: tdata = ser.read(data_left) print tdata` But there's no output at all even though I can see the output coming out from the other tab!!!! : – Ahmed Al-haddad Mar 17 '16 at 04:31
  • http://stackoverflow.com/questions/36051868/no-output-from-pyserial I put the question here in more details – Ahmed Al-haddad Mar 17 '16 at 05:09
  • Thanks. The port was a typo. I went through your answer, and I needed to change `is_open` to `isOpen`, because I am using **pyserial v2.6**. Anyways, I don't know but it didn't work. I just got `no data` every 2 seconds even though the port is printing data. I used `picocom` and it showed the correct parameters. ._. so frustrating – Ahmed Al-haddad Mar 17 '16 at 10:58
  • I found out that I was supposed to close the connection to the other port before using the script. Please do include this in your answer, sir. – Ahmed Al-haddad Mar 17 '16 at 12:02