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

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

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)

Can I use the USB port of the raspberry pi (model B+) for serial communication (RS232)

I need to interface an old machine (thermostream) to interface with the raspberry pi (model B+)
The thermostream device has a RS232 serial port and I want to connect it to the USB port of the raspberry pi using the RS232 serial to usb cable (where the usb end of the cable is inserted in the pi and the serial end is connected to the device). And I need to write the code in python.
IS this possible? If yes, how should I proceed? Any help is greatly appreciated.
Yes. First you need to install pyserial
Then, in Python, you can use the following function to create a serial object that connects to a port. The usb ports on the pi are dynamically assigned a name and those names can change. This function will enable you to loop through each port for the name. (might not work well with multiple devices attached). Check your connection settings on the device you wish to communicate with (baudrate, parity, stopbits etc) and modify the code to use those settings.
import serial
def serialConnect():
serlocations=['/dev/ttyACM', '/dev/ttyACM0', '/dev/ttyACM1','/dev/ttyACM2', '/dev/ttyACM3','/dev/ttyACM4', '/dev/ttyACM5','/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3', '/dev/ttyUSB4', '/dev/ttyUSB5', '/dev/ttyUSB6', '/dev/ttyUSB7', '/dev/ttyUSB8', '/dev/ttyUSB9', '/dev/ttyUSB10','/dev/ttyS0', '/dev/ttyS1', '/dev/ttyS2', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9', 'com10', 'com11', 'com12', 'com13', 'com14', 'com15', 'com16', 'com17', 'com18', 'com19', 'com20', 'com21', 'com1', 'end']
for device in serlocations:
try:
ser = serial.Serial(
port=device,
baudrate=9600,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.SEVENBITS
)
print device
return ser
except:
x=0
if device == 'end':
print "No Device Found"
ser = serialConnect()
if ser:
ser.write("TEST")
ser.timeout=5
for i in ser.readlines():
print i

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

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.

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.