Python serial port read until it finishes - python

#!/usr/bin/python
I am struggling with quite the simple problem. I want to read some data from the serial port first than start writing the data. The data reading and writing works well. The problem is I need to read first around 7 lines like
X7_SEM_V3_6
ICAP OK
SA OK
IC OK
RBDK OK
status OK
S OK
Then send 'I' followed by N and C, then 9 hex digits from the text file. The code below read one line and went into the writing section and read the whole text file;
X7_SEM_V3_6
000062240
000062241
ICAP
000062240
000062241
so on
after doing this to all seven read lines than it send I. I want it read all seven lines once than send I and start working. this is in while loop. If I use something else it just read first line and stuck. Please some one help.
import serial, time
import binascii
ser = serial.Serial()
ser.port = "/dev/ttyUSB1"
ser.baudrate = 38400
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.xonxoff = False
ser.rtscts = False
ser.dsrdtr = False
number_address = 1341602
number_char = 9
timeout=1
f=open('lut.txt','r')
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
if ser.isOpen():
try:
ser.flushInput()
ser.flushOutput()
# reading
max_packet = 20
lines = 0
while True:
byteData = ser.read_until('\r',max_packet)
newdata=str(byteData)
print newdata.strip()
#ser.write('I')
ser.write('I')
time.sleep(0.01)
for line in f.readlines():
print line
ser.write('N')
time.sleep(0.01)
ser.write(' ')
time.sleep(0.01)
ser.write('C')
time.sleep(0.01)
for i in line:
newdata=i
ser.write(newdata)
time.sleep(0.01)
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "

Related

python code to read and write after each write it read the whole file than increment

I have a python code to read some data from the serial port of the Artix-7 FPGA board it reads well. Then After read it suppose to write some data that is 9-digit hexadecimal number to the port it did well but the problem is after each write is read all the text file first then increment and the size of my text file is too big its about 1348065 lines .
here is the code please guide someone.
#!/usr/bin/python
import serial, time
from addresstable import *
import binascii
ser = serial.Serial()
ser.port = "/dev/ttyUSB1"
ser.baudrate = 38400
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.xonxoff = False
ser.rtscts = False
ser.dsrdtr = False
number_address = 1341602
number_char = 9
timeout=1
#f=open('lut.txt','r')
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
if ser.isOpen():
try:
ser.flushInput()
ser.flushOutput()
# reading
max_packet = 20
lines = 0
while True:
byteData = ser.read_until('\r',max_packet)
newdata=str(byteData)
print newdata.strip()
ser.write('I')
time.sleep(0.01)
# writing
with open('adder-sem-address-sen0.txt', 'r') as f:
for line in f.readlines():
#print line
ser.write('N')
time.sleep(0.01)
ser.write(' ')
time.sleep(0.01)
ser.write('C')
time.sleep(0.01)
for i in line:
newdata=i
ser.write(newdata)
time.sleep(0.01)
time.sleep(1)
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "
My expected output is looks line
I>NC0000AABBF
I>NC0000AABBF
I>NC0000AABBA
and continue, it does same but after each line read it first it red the whole text file than read the next line

python serial data read code with carriage return \r

This is my working code to read the data from the Artix-7 serial port. But it comes form the carriage return. Like;
'FFFDB03\r'
'FFFFB606'
'\r00006C0'
'D\rFFFFD8'
'1B\rFFFFB'
'037\r0000'
'606F\rFFF'
'FC0DF\rFF'
'FF81BE\r0'
'000037D\r'
how to solve this, can I set the CR and LF in the serial.Serial() settings. If anyone knows please reply
#!/usr/bin/python
import serial, time
ser = serial.Serial()
ser.port = "/dev/ttyUSB0"
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.xonxoff = False
ser.rtscts = False
ser.dsrdtr = False
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
if ser.isOpen():
try:
ser.flushInput()
ser.flushOutput()
while True:
print repr(ser.read(8))
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "
The solution to the synchronization problem is to read until the \r. That can be done by replacing your inner loop with:
max_packet = 8
while True:
data = ser.read_until(terminator='\r', size=max_packet*2)
print repr(data[:-1])

Python serial port read data in linux - terminal didn't display the output

I am trying to read data from the serial port of the Artix-7 FPGA. If I read the data from Putty it shows me the data. The data is a 4-Byte hexadecimal numbers. Like;
000FFFFA
0000FCA0
000078CC
........ almost more than 10,000 lines data...
But I want to read the data from the Python script the script mentioned below should work; it connected to the port but shows only the empty strings. Seems like Ubuntu terminal didn't display the data. But if I didn't display the data and only save it, again no data. Any idea !!!!!!
#
!/usr/bin/python
import serial, time
ser = serial.Serial()
ser.port = "/dev/ttyUSB0"
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.timeout = None
ser.timeout = 1
ser.xonxoff = False
ser.rtscts = False
ser.dsrdtr = False
ser.writeTimeout = 2
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
if ser.isOpen():
try:
ser.flushInput()
ser.flushOutput()
time.sleep(0.5)
numOfLines = 0
f=open('signature.txt','w+')
while True:
response = ser.readline()
f=ser.write(response)
print response
numOfLines = numOfLines + 1
f.close()
ser.close()
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "

How to read input from port and print to terminal

here's the situation:
I have two stamps, one connected to port: /dev/ttyUSB1, and one external one, unconnected. The external one has a (Contiki based) program on it to send (every 2 seconds) a single letter across to the stamp that is plugged into the computer. stamp plugged into the computer has a program, also Contiki based, which reads this information via a radio signal.
Now, I have written a python program, making use of pyserial, which aims to read the information coming into the port and display it in terminal. I know the message is arriving correctly as I can print the message from the stamp connected via USB to terminal fine. But I am having trouble reading the information from the python code.
import serial, time, threading
ser = serial.Serial()
ser.port = "/dev/ttyUSB1"
ser.baudrate = 9600
ser.bytesize = serial.SEVENBITS
ser.parity = serial.PARITY_EVEN
ser.timeout = 0.1
ser.rtscts = 1
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
if ser.isOpen():
try:
numOfLines = 0
while True:
print ser.read()
numOfLines = numOfLines + 1
time.sleep(2)
if (numOfLines >= 50):
break
ser.close()
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "
When run, instead of printing the expected letter sent from the stamp every couple of seconds, it will print a few consistent, yet random letters. For example:
w
z
z
w
g
z
g
z
z
z
w
z
w
There is no output from the program when I remove the USB connected to the stamp. So it must be reading something from the port... Any help would be much appreciated.
It sounds like it could be a problem with the baudrate. you didn't post the code that does this:
ser = serial.Serial('/dev/ttyUSB1', timeout=1, baudrate=9600, rtscts=1)

Using pyserial for sending keystrokes

I have a task where I have to send keystrokes to a machine and to achieve the same I have two machine out of which one is Host Machine (running python scripts) and other is the target machine (where I have to send keystrokes). I am using "L3 Systems Inc's KeyAT device" on COM1.
Now the problem is I am unable to send keystrokes and following is the code I am running.
import serial, time
#initialization and open the port
#possible timeout values:
# 1. None: wait forever, block call
# 2. 0: non-blocking mode, return immediately
# 3. x, x is bigger than 0, float allowed, timeout block call
ser = serial.Serial()
#ser.port = "/dev/ttyUSB0"
ser.port = "COM1"
#ser.port = "/dev/ttyS2"
ser.baudrate = 9600
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
#ser.timeout = None #block read
ser.timeout = 1 #non-block read
#ser.timeout = 2 #timeout block read
ser.xonxoff = False #disable software flow control
ser.rtscts = False #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False #disable hardware (DSR/DTR) flow control
ser.writeTimeout = 2 #timeout for write
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
if ser.isOpen():
try:
ser.flushInput() #flush input buffer, discarding all its contents
ser.flushOutput()#flush output buffer, aborting current output
#and discard all that is in buffer
#write data
# ser.write("AT+CSQ")
ser.write("~~~~~~~~~~\r")
ser.write("~:04\r")
# ser.write('\x03')
#print("write data: AT+CSQ")
time.sleep(0.5) #give the serial port sometime to receive the data
numOfLines = 0
while True:
response = ser.readline()
print("read data: " + response)
numOfLines = numOfLines + 1
if (numOfLines >= 5):
break
ser.close()
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "
Can anyone please help
Thanks,
Vipul
The only problem is the ser.readline() returns binary , you need transform it into string: str(response), so it will work perfectly!

Categories