Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python serial, can write but can't read

I am trying to write a very basic script which will allow me to have full control over a device via serial. I can send data (and I know the device receives it, leaving screen open on the device allows me to see the input appearing).

But I cannot receive data, with screen open, and inputting data via screen I get the error:

Traceback (most recent call last): File "serialRec.py", line 4, in for line in ser: File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 456, in read raise SerialException('device reports readiness to read but returned no data (device disconnected?)') serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected?)

There is no error if I open the port waiting for a message without screen. My application as I said sends data no problem... What can I do? How can I get this reading? I am running this script on a Ubuntu 12.04 installation... Screen works fine on the device the Ubuntu laptop is attatched to

The sys arguments are: argv[1] = device (/dev/ttyUSB0) and argv[2] = braud rate (e.g. 9600).

import serial
import sys
import time

def enterdata():
    ser = serial.Serial(sys.argv[1], sys.argv[2])
    scom = raw_input("type away:" )
    incli = str(scom)
    time.sleep(.2)
    if (incli  == "exit the app"):
        print ("Exiting the data send, nothing was sent from the exit command")
    else:
        while True:
            print ser.readline()
        enterdata()

print ("Welcome to the serial CLI")
enterdata()

UPDATE:

I now have it working, but limited and ugly it prints the return on multiple lines from sending one command. Though for this I am going to try a few things out. I will post and share some nice working code once i get it to a good place.

import serial
import sys
import time
def enterdata():
 ser = serial.Serial(sys.argv[1], sys.argv[2])
 scom = raw_input()
 incli = str(scom)
 if (incli  == "exit the app"):
  print ("Exiting the data send, nothing was sent from the exit command")
 else:
  ser.write(incli+"\r\n")
  time.sleep(0.5)
  while True:
   data = ser.read(ser.inWaiting())
   if (len(data) > 0):
    for i in range(len(data)):
     sys.stdout.write(data[i])
    break
  ser.close()
  enterdata()


print ("Welcome to the serial CLI, hit enter to activate:")
enterdata()

This is the changes I have made, it works. Though it seems to always print double or maybe send an extra character

like image 456
TheHidden Avatar asked Dec 06 '25 03:12

TheHidden


1 Answers

You might want to try with a listener first, but you have to make sure that your device is sending data to your serial port at the correct baudrate.

import serial, sys
port = your_port_name
baudrate = 9600
ser = serial.Serial(port,baudrate,timeout=0.001)
while True:
    data = ser.read(1)
    data+= ser.read(ser.inWaiting())
    sys.stdout.write(data)
    sys.stdout.flush()
like image 157
tglaria Avatar answered Dec 07 '25 17:12

tglaria



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!