Attempting to use pyserial between applications, yet I get terminal - python

(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.

Related

Issue sending enter key through serial port in python

I am having trouble connecting to a data acquisition box using a python3 script through a USB serial port. The box expects to receive an Enter Key to start responding and to get a username and password.
I have tried the following code to send the Enter key but I never receive anything from the serial port. Using a Terminal Emulator, it works just fine and after pressing enter I see the prompt waiting to log in.
import serial
ser = serial.Serial('COM5', 115200)
ser.write(str.encode('\r'))
ser.write(str.encode('\n'))
ser.writelines(str.encode('\r'))
ser.writelines(str.encode('\r\n'))
ser.write(bytes("\r",encoding='ascii'))
while True:
s = ser.readline()
print(s.decode('utf-8'))
Could someone kindly help?

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!"

Python PySerial changing settings on timeout

I have a question about Python and PySerial.
On my Raspberry Pi i want to read a serial port from a device. I got 2 types of devices i want to read from. They both got different settings:
ser = serial.Serial()
ser.baudrate = 9600
ser.bytesize=serial.SEVENBITS
ser.parity=serial.PARITY_EVEN
ser.stopbits=serial.STOPBITS_ONE
ser.xonxoff=0
ser.rtscts=0
ser.timeout=20
ser.port="/dev/ttyUSB0
and:
ser = serial.Serial()
ser.baudrate = 115200
ser.bytesize=serial.EIGHTBITS
ser.parity=serial.PARITY_NONE
ser.stopbits=serial.STOPBITS_ONE
ser.xonxoff=1
ser.rtscts=0
ser.timeout=20
ser.port="/dev/ttyUSB0
This is the code to read the serial port:
try:
ser.open()
except:
sys.exit ("Error opening %s." % ser.name)
t_count = 0
while t_count < 20:
t_line = ''
try:
t_raw = ser.readline()
except serial.SerialException:
sys.exit ("Serial port %s could not be read." % ser.name )
t_str = str(t_raw)
t_line = t_str.strip()
print (t_line)
t_count += 1
try:
ser.close()
except:
sys.exit ("Oops %s. Program aborted. Could not close serial port." % ser.name )
So when i connect to a device with 115200 but the device runs 9600, i get timeouts ofcourse. But in my program it will just time out twenty times (times the for loop will run), and no exception to be thrown. Not even just before the program exits after looping 20 times. No error message nothing.
What i want to achieve is the following, i want to make the python script self detecting what he is connected to. When the readline() times out 20 times it should change settings. (by running another function or something).
I cant check if the readline returns something empty, because there are empty lines in the serial message.
Is there any way to get the right exception? Or any other smart way to solve this?
(By the way, i am sure the settings work. As i tested them both and run fine.)
Thanks in advance.
Cheers!
Easiest way would be to create a handshake function,
send something with one serial handler that you know the device will respond to correctly. If the answer is jibberish then change the handler and try the other one until you succceed.

Can't receive reply using PySerial but hyperterminal works

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".

Categories

Resources