How to Run a Command on the Serially Connected Device in python - python-2.7

I Have Connected a Telepresence Device serially to my PC and it is connected successfully but i am not able to read or write the Data from the Device,Can Any Buddy Help me..
Here is my Python Script.I Need my script to Execute the command whatever i give in its command prompt.
import serial
ser = serial.Serial()
ser.braudrate = 115200
ser.port = "/dev/ttyUSB0"
ser.open()
print ser.name
if ser.isOpen():
print True
elif ser.isOpen():
print False
ser.readall()
print ser
ser.write("hello\n")
print ser

I'm having a similar problem right now, but, in your case, instead of using ser.write() I'd try using ser.writelines. If you are still looking for documentation try here.

Related

Keyboard input over SSH to the raspberry pi3 with pynput function in python

I'm trying to control a small car toy using the raspberry pi 3 the SSH
from my laptop.
The challenge I'm facing is, when I'm directly connected on RPi (mouse,
keyboard and monitor plugged to the RPI) everything works well, but
when I connect to RPi over the SSH connection, this is what I see as an
output: Xlib.error.DisplayNameError: Bad display name ""
The method I'm using to connect to my RPi is "ssh pi#"
Please can you advice me what to do next?
The sample code I'm using is shown below:
Thank you,
Johny 1984
from pynput import keyboard
from pynput.keyboard import Key, Listener
def on_press(key):
# DO SOMETHING
def on_release(key):
# DO SOMETHING
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
the way I connecting to RPI over SSH
ssh pi#(RPI_IP_ADDRESS)
The error with the bunch of text above:
Xlib.error.DisplayNameError: Bad display name ""
It happens pynput needs X and some other setup listed in their documentation. So I ended up using keyboard package https://pypi.org/project/keyboard/ instead of pynput.keyboard.
https://unix.stackexchange.com/questions/427345/keyboard-monitoring-without-display
I modified his solution a bit so that I can exit from listening when escape key is pressed. Roughly something like this:
import keyboard
import time
#declaring it global so that it can be modified from function
global releaseListening
keepListening = True
def key_press(key):
print(key.name)
#if escape is pressed make listening false and exit
if key.name == "esc"
keepListening = False
keyboard.on_press(key_press)
while keepListening :
time.sleep(1)
I had a similar problem recently, and I could find a solution.
To make above work over ssh it is required to open /etc/ssh/sshd_config and uncomment the following lines if
they are commented:
X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost yes
then on remote RPI type echo $DISPLAY. The result should be something like this:
localhost:10.0
when running/debugging your script, you should set env variable DISPLAY=result_of_echo_$DISPLAY
P.s Do not forget to connect via ssh using X server -> ssh user#address -X

Serial Communication in Raspberry pi 3 B+

I want to communicate between Raspberry pi 3 B+ and GSM GPRS A6. I tried and I am unable to send data to GPRS Module from Raspberry pi.
Now, I know that GPIO serial port is disabled by default in newer Operating Systems (in my case Raspbian Stretch), so I have enabled it by adding following line in config.txt file,
enable_uart=1
Here's my Code:
import serial
import time
port = "/dev/ttyS0"
COMM = serial.Serial(port, baudrate=115200)
while(1):
COMM.write("AT\r")
print (COMM.read(5))
This command is supposed to return "OK", but it does not and nothing is printed. I am using python 2.7.
Some people suggested me to send data using this method,
COMM.write('AT' + '\r')
I tried but it didn't help.
There is no problem with my GPRS module. It works file with arduino.
So, what am I doing wrong here?
Thanks in advance!
,
First , be sure to enable the Serial.
sudo raspi-config -> Interfacing Option -> Serial
Second , sudo nano /boot/cmdline.txt
Delete "console=serial,115200"
And Then
sudo nano /boot/config.txt
Add the end
dtoverlay=pi3-disable-bt
core_freq=250
While you use : Serial(/dev/ttyAMA0,9600)
try sending:
import serial
port = "/dev/ttyS0"
comm = serial.Serial(port, baudrate=115200)
while True:
comm.write('AT' + '\n\r')
msg = comm.readline()
print(msg)

Python file sp\xe9cifi\xe9 not found during the opening of the COMx port

I'm trying to open a port COM on my computer using Python.
Here is my code
import serial
ser = serial.Serial()
ser.port = 4
ser.baudrate = 9600
ser.open()
When I execute my code line by line on the Python shell, the ser.open() generates the following error :
I SerialException: could not open port 'COM5': WindowsError(2, 'Le fichier sp\xe9cifi\xe9 est introuvable.')
it means : sp\xe9cifi\xe9 not found !!
I don't know what's happening with this file :/ and how I can fix this issue.
If need any further information please don't hesitate.
thanks for the help.
The solution was to put plug something in the Port COM5 to open it !!
You can't open the port if there is nothing physically plugged in it

Python serial fails to write

I am using Python 2.7 and serial v 2.6 to listen to a serial port. I can listen to the port just fine, but I cannot write to the port.
import serial
cp = 5
br = 9600
ser = serial.Serial(5,br)
a = ser.readline()
Using this, I can listen to the outcoming data stream. However, if I want to change the status of the instrument (e.g. set GPS to off) I would write a command:
ser.write('gps=off')
When I do this, I get "6L" returned and the gps stay on. However, if I connect via TeraTerm I can see the data stream in in real time. While the data streams in I can type gps=off followed by a return and suddenly my GPS is off. Why is my command in Python not working like TeraTerm?
UPDATE
If I instead do
a = ser.write('gps=on')
"a" is assigned value of 6. I also tried sending a "junk" command via
a = ser.write('lkjdflksdjflksdjf')
with "a" assigned a value of 17, so it seems to be assigning the length of the string to a, which does not make sense.
I think the problem was the ser.write commands were getting stuck in buffer (I am not sure of that, but that is my suspicion). When I checked the input buffer I found it to be full. After flushing it out I was able to write to the instrument.
import serial
ser = serial.Serial(5, 9600)
# the buffer immediately receives data, so ensure it is empty before writing command
while ser.inWaiting()>0:
ser.read(1)
# now issue command
ser.write('gps=off\r')
That works.

pySerial could not open port COM6: Element not found

i tried to use the following code:
import serial
ser = serial.Serial()
ser.baudrate = 56700
ser.port = 'COM6'
ser.open() # HERE IS THE ERROR
When i do it from Python shell, typing line by line, it works and i can recieve data from a bluetooth device. But when i run it from cmd (C:\>python serial.py) it raises the error "could not open port COM6: Element not found". I can see in the bluetooth device that it connects for a second and then disconnects.
Anyone know what this is?
BTW, Im using Windows 7 64-Bits and Python 2.7. Thanks!
Perhaps you forgot to run the Command prompt as Administrator?
I had the same problem. I fixed it by adding time.sleep(5) around ser.open(). So it would look like this:
import serial
import time
ser = serial.Serial()
ser.baudrate = 56700
ser.port = 'COM6'
time.sleep(5)
ser.open()
time.sleep(5)
I didn't experiment much with the code, but you probably don't have to sleep for 5 seconds, you can probably just sleep for 0.1 seconds and it would still work. This is probably not the best way to fix it, but it works.