Can't receive reply using PySerial but hyperterminal works - python

I have a device (Pololu Wixel) that I'm trying to communicate with using a serial connection over USB. Hyperterminal works fine but I'm trying to use Python for more flexibility. I can send commands to the device, but when I try to receive all I get is the command I just sent. However, if I open Hyperterminal, I'll receive the reply there to the command sent from the script. My code is below. I'm at a bit of a loss and it seems like this should be fairly straightforward. I appreciate any help.
import serial
import time
'''
Go through 256 COM ports and try to open them.
'ser' will be the highest port number. Fix this later.
'''
for i in range(256):
currentPort = "COM" + str(i+1)
try:
ser = serial.Serial(currentPort,baudrate=115200,timeout=5)
print("Success!!")
print(ser.name)
except:
pass
print(ser.isOpen())
str = "batt" #Command to request battery levels.
ser.write(str.encode())
x = ser.inWaiting()
print(x)
while ser.inWaiting() > 0:
out = ser.readline()
print(out.decode())

Add a break after finding an active port,
Try passing a different eol value to readline(), "\r" or "\r\n".

Related

Communicate with a terminal using pySerial

I have a terminal which I connect to with serial communication, and I want to read from it and write to it some commands I prepared in advance in a string array
Using pySerial, I need to write a code that will read the lines with a stop condition which is a wait from the console for input from the user, and then write the commands from the array
Just want to clarify, it's like an automatic PuTTY, and no I can't connect to the terminal through ssh, and no I can't bood the machine's terminal since it's not a pc
here is what i tried:
import serial
Baud_Rate = 9600
Ser = serial.Serial('COM4', Baud_Rate)
while Ser.isOpen():
input('Error: Ser is already open, please close it manually. if the port is
closed, press enter\n')
try:
Ser.close()
except serial.serialutil.PortNotOpenError:
pass
Ser.open()
Serial_com = [] #a str array with the relevant commands
for i in range(len(Serial_com)):
ter = Ser.readline()
while ter != 0xaf:
#print(str(ter))
print(str(ter.decode('utf-8').rstrip('\r\n')))
ter = Ser.readline()
sleep(1)
if i == 0:
print('login: root')
Ser.write(bytes("root", encoding='utf-8'))
else:
print('\n\n\n\n\nroot # Ser: ~ # ' + str(Serial_com[i]))
Ser.write(bytes(Serial_com[i], encoding='utf8'))
I realized that once the serial port is waiting for the python code (or the user) to write commands, that it sends the character 0xaf. It might be a coincidence, but still I wrote that as a stop condition for the reading from the terminal
the code can read from the serial port, but once it needs to write to the serial port it won't proceed
I can't share the rest because it's confedencial for a project

How can I get python to trigger a process based on an incoming string from serial listening?

Totally new to Python. Working with a Paspberry Pi and Rockblock 2 satellite SBD transceiver connected over FTDI cable. Have managed enough python code to listen to the rockblock to catch a SBDRING trigger. Once received I need it to recognise this so I can try to get it to take an action. My code here fails to trigger and just keeps listening. Is there some rule or reason I've not been able to find as to why the python equalto won't work on what is being listened to?
import time
import serial
port = "/dev/ttyUSB0"
ser = serial.Serial(port, baudrate=19200, timeout=5)
print "Starting monitor of '" + port + "'"
try:
ser.isOpen()
print(port + " is open")
except:
print("Error")
exit()
if(ser.isOpen()):
try:
while(1):
print(ser.readline())
if(ser.readline() == "SBDRING"):
print "Message detected!"
except Exception:
print("Error reading serial")
else:
print("Cannot open '" + port + "'")
Expected result: on display a new line ticks over every 5 seconds. When "SBDRING" appears it should be followed by "Message detected!" and carrying on.
Actual result: on display a new line ticks over every 5 seconds. When "SBDRING" appears it does not displays "Message detected!", just carries on.
I intend to replace the 'print "Message detected!"' part with a actual action once it functions.
Maybe the string you're expecting includes a terminating character like a CR or LF.
You should print what you get from the port and study its length.
Otherwise, you can loosen a bit your comparison, maybe something like:
if("SBDRING" in ser.readline()):
print "Message detected!"
EDIT: Looking at the manual, it seems the device terminates everything with "\r" so I guess you should manage with:
if(ser.readline() == "SBDRING\r"):
print "Message detected!"

Attempting to use pyserial between applications, yet I get terminal

(update)
So I found some documentation on this link
https://elinux.org/RPi_Serial_Connection#Connections_and_signal_levels
If you scroll down you will find a section "S/W: Preventing Linux from using the serial port" It says "By default Linux will grab the serial port and use it as a terminal."
So it appears that this is a thing, however, the instructions it gives is for a Raspberry Pi, and it calls for you to use raspi-config. It doesn't give anything for regular linux use.
Using python I'm attempting to communicate between my laptop and an Up-Board. I'm connecting the Up-board by using an FTDI cable, connected to the serial connection on the board.
(OP)
I've done something similar before with C++ on a different board. The code I'm using I pulled from this site, http://www.varesano.net/blog/fabio/serial%20rs232%20connections%20python
import time
import serial
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
print ser.isOpen()
input=1
while 1 :
input = raw_input(">> ")
print "Check"
try:
if input == 'exit':
ser.close()
exit()
else:
ser.write(input + '\r\n')
out = ''
time.sleep(1)
while ser.inWaiting() > 0:
out += ser.read(1)
if out != '':
print ">>" + out
except:
ser.close()
break
I'm doing something similar on the Up-board. The only difference is that it waits for a message, then returns the message back to my laptop. Just a loop back between the Upboard and my laptop.
Here's where it gets interest.
I'm having two seperate issues.
1) I'll attempt to send a word, ie "test" and it will only send "e", "tst" doesn't get sent
2) The other issue is, it sends message, and I get a return for a password. So I reset the software and attempt to connect again, this time I send the username as the first message. I get back a reply for password, then I send the password, and now I have terminal access to the Upboard. While, all I really want is to connect to the application on the other end.
Does anyone have any suggestions on what is going on?
So I found the resolution, it appears that the system was configured in grub to connect to terminal on the same port address.
if you go to /etc/default/grub you will find a line
GRUB_CMDLINE_LINUX="console=ttyS0, 115200n8"
I ended up commenting that line, and I now can connect without it giving me console control.

How to read and write from a COM Port using PySerial?

I have Python 3.6.1 and PySerial Installed. I am trying the
I am able to get the list of comports connected. I now want to be able to send data to the COM port and receive responses back. How can I do that? I am not sure of the command to try next.
Code:
import serial.tools.list_ports as port_list
ports = list(port_list.comports())
for p in ports:
print (p)
Output:
COM7 - Prolific USB-to-Serial Comm Port (COM7)
COM1 - Communications Port (COM1)
I see from the PySerial Documentation that the way to open a COM Port is as below:
import serial
>>> ser = serial.Serial('/dev/ttyUSB0') # open serial port
>>> print(ser.name) # check which port was really used
>>> ser.write(b'hello') # write a string
>>> ser.close() # close port
I am running on Windows and I get an error for the following line:
ser = serial.Serial('/dev/ttyUSB0')
This is because '/dev/ttyUSB0' makes no sense in Windows. What can I do in Windows?
This could be what you want. I'll have a look at the docs on writing.
In windows use COM1 and COM2 etc without /dev/tty/ as that is for unix based systems. To read just use s.read() which waits for data, to write use s.write().
import serial
s = serial.Serial('COM7')
res = s.read()
print(res)
you may need to decode in to get integer values if thats whats being sent.
On Windows, you need to install pyserial by running
pip install pyserial
then your code would be
import serial
import time
serialPort = serial.Serial(
port="COM4", baudrate=9600, bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE
)
serialString = "" # Used to hold data coming over UART
while 1:
# Wait until there is data waiting in the serial buffer
if serialPort.in_waiting > 0:
# Read data out of the buffer until a carraige return / new line is found
serialString = serialPort.readline()
# Print the contents of the serial data
try:
print(serialString.decode("Ascii"))
except:
pass
to write data to the port use the following method
serialPort.write(b"Hi How are you \r\n")
note:b"" indicate that you are sending bytes

esp8266 and cp2102 don't work with python serial

esp8266 and cp2102 don't work! Why?
import serial
sp="/dev/ttyUSB0"
port = serial.Serial(sp)
while True:
port.write("AT+RST")
rcv = port.read(10)
print rcv
I pressed "AT+RST"[Enter] and don't have "READY" after it.
Make sure you include CRLF (\r\n) characters at the end of your command. I lost a day messing with this before I figured that out. I got the local echo back of the command but since I never sent a \r\n I would not get any more data. Here is what works for me as a basic terminal in Python using pyserial:
import serial
import time
ser = serial.Serial('/dev/tty.usbserial-A8004xaO', 115200, timeout=2.5)
while True:
cmd = raw_input("> ");
ser.write(cmd + "\r\n")
ret = ser.read(len(cmd)) # eat echo
time.sleep( 0.2 )
while ( ser.inWaiting() ):
ret = ser.readline().rstrip()
print ret
You aren't setting a baud rate when opening the serial port. The default is probably not appropriate for the ESP8266.

Categories

Resources