Raspberry Pi 4 stuck in serial.Serial() - python

I'm try to use RS232HAT With raspberry Pi 4 and I found this problem.
This is My code.
import serial
#print(serial.__file__)
dev = "/dev/ttySC0"
Baudrate = 115200
ser = serial.Serial(dev, Baudrate) <<< Stuck in this Function
print("START")
try:
while(1):
data = ser.read(value)
print(data.decode("utf-8"))
except KeyboardInterrupt:
print("EXIT")
exit()
When I interrupt by keyboard. The Program show this log.
Traceback (most recent call last):
File "/home/pi/Desktop/2-CH-RS232-HAT/python/examples/main.py", line 15, in <module>
ser = config.config(dev = "/dev/ttySC0")
File "/home/pi/Desktop/2-CH-RS232-HAT/python/lib/waveshare_2_CH_RS232_HAT/config.py", line 12, in __init__
ser.serial = serial.Serial(ser.dev, Baudrate)
File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 240, in __init__
self.open()
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 265, in open
self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
KeyboardInterrupt
My available devices:

you have to link the file, as example.
ln -s /dev/ttySC0 /dev/ttyS80
it sometimes cant read real serial info

Related

Why does SimpleMFRC522.write not work when using RFID device on the raspberry-pi?

I just want to connect an RFID device to my raspberry. I did everything as in the tutorial (https://pimylifeup.com/raspberry-pi-rfid-rc522/) but as soon as I want to write to an RFID tag it no longer works. This is my code:
#!/usr/bin/env python
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
try:
text = input('New data:')
print("Now place your tag to write")
reader.write(text)
print("Written")
finally:
GPIO.cleanup()
and this is the error it gave me after I press control+c (I press control+c because after I write in the data nothing happens at all):
Now place your tag to write
^CTraceback (most recent call last):
File "/home/pi/pi-rfid/Write.py", line 11, in <module>
reader.write(text)
File "/usr/local/lib/python3.9/dist-packages/mfrc522/SimpleMFRC522.py", line 62, in write
id, text_in = self.write_no_block(text)
File "/usr/local/lib/python3.9/dist-packages/mfrc522/SimpleMFRC522.py", line 66, in write_no_block
(status, TagType) = self.READER.MFRC522_Request(self.READER.PICC_REQIDL)
File "/usr/local/lib/python3.9/dist-packages/mfrc522/MFRC522.py", line 259, in MFRC522_Request
(status, backData, backBits) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, TagType)
File "/usr/local/lib/python3.9/dist-packages/mfrc522/MFRC522.py", line 210, in MFRC522_ToCard
self.Write_MFRC522(self.CommandReg, command)
File "/usr/local/lib/python3.9/dist-packages/mfrc522/MFRC522.py", line 159, in Write_MFRC522
val = self.spi.xfer2([(addr << 1) & 0x7E, val])
KeyboardInterrupt

getting OSError -202 where running urequests.get from micropy

hi im having error with this code but it runs in python shell could any body help me
from machine import Pin
import time
import network
import urequests
p0 = Pin(0,Pin.OUT)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('ssid', 'pass')
response = urequests.get('http://jsonplaceholder.typicode.com/albums/1')
while True:
ans = response.json()['userId']
p0.value(1)
time.sleep(1)
p0.off()
time.sleep(1)
print('ok')
and this is the error:
Traceback (most recent call last):
File "<stdin>", line 9, in <module>
File "urequests.py", line 108, in get
File "urequests.py", line 53, in request
OSError: -202
Your issue (my guess) is that you begin to urequest.get() without connected to WiFi. Create function that do wifi connection and call it
def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('essid', 'password')
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
Explain: wlan.connect() is asynchronous function and you have to wait, while it connects to wifi and only then continue with urequest.get()

I get an error of traceback (most recent call last) when I try to run this Python program. I want to know the solution to this

This is a Python program to receive the data from an XBee module using the python-xBee library. I have installed both the xbee and pyserial modules.
import serial
from xbee import XBee
serial_port = serial.Serial('/dev/ttyUSB0', 9600)
xbee = XBee(serial_port)
while True:
try:
print xbee.wait_read_frame()
except KeyboardInterrupt:
break
serial_port.close()
But when I run this and any kind of program with serial port, this is the error I am getting:
Traceback (most recent call last):
File "C:/Users/Manurajeev/PycharmProjects/untitled/one.py", line 4, in
<module>
serial_port = serial.Serial('/dev/ttyUSB0', 9600)
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 31, in
__init__
super(Serial, self).__init__(*args, **kwargs)
File "C:\Python27\lib\site-packages\serial\serialutil.py", line 240, in
__init__
self.open()
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 62, in
open
raise SerialException("could not open port {!r}:
{!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port '/dev/ttyUSB0':
WindowsError(3, 'The system cannot find the path specified.')
Process finished with exit code 1
I don't understand what the problem is. I tried everything, but the same error keeps popping up every time.
In Linux, check the permissions on the tty device (ls -l /dev/ttyUSB0) to ensure that you have read/write access to it. Note that it might have a different name.
For Windows, have you been able to open COM5 with a terminal emulator and send/receive data on the XBee? Do you still have it open in another program when you're trying to open it in Python? Only one program can access a COM port at a time.

Calling method from module

I'm new to python and have issues getting my code working.
I got two different modules, in module a.py I put all my methods, in module b.py I put the logic.
The development environment I'm using is Sypder.
# module a
import serial
ser = serial.Serial()
def serWrite ( str ):
ser.write (str + "\x0D")
print "Write data :", str + "\x0D"
time.sleep(0.1)
return
def configuration():
flagAT = 0
while (flagAT == 0):
serWrite("at")
while True:
ok = ser.readline()
if (ok[0:2] == "OK"):
print ("AT OK, DEVICE CONNECTED" + "\x0D")
flagAT = 1
break
else:
print "DEVICE NOT CONNECTED OR NOT WORKING"
break
print("Starting with configuration")
Module b.py :
#module b
import serial
import a
ser = serial.Serial()
ser.port = "/dev/ttyS1"
ser.baudrate = 115200
### more serial configuration here###
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
configuration()
Now to the issue:
When I run module b.py AFTER running a.py INSIDE Spyder everything is working as intended.
BUT: Spyder code analysis tells me
'a' imported but unused
When I try to call module b.py OUTSIDE Spyder I get following error
pi#(none) ~/WorkingDirectory $ python b.py
Traceback (most recent call last):
File "b.py", line 83, in <module>
configuration()
NameError: name 'configuration' is not defined
Importing configuration() with:
import a
a.configuration()
Gives following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "/home/workingDirectory/test.py", line 85, in <module>
a.configuration()
File "a.py", line 336, in configuration
serWrite("at")
File "a.py", line 16, in serWrite
ser.write (str + "\x0D")
File "build/bdist.linux-i686/egg/serial/serialposix.py", line 490, in write
serial.serialutil.SerialException: Attempting to use a port that is not open
I don't understand why my programm is running within spyder without problems but not outside.
Can someone help here?
You need to change module b to:
#module b
import serial
from a import ser, configuration
#ser = serial.Serial()
ser.port = "/dev/ttyS1"
ser.baudrate = 115200
### more serial configuration here###
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
configuration()

Can't open serial connection in Pyserial: "termios.error: (22, 'Invalid argument') "

I'm having problem executing the following code:
import serial
ser = serial.Serial(
port='/dev/tty.FireFly-16CB-SPP',
baudrate=115200,
#parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
ser.open()
ser.isOpen()
This worked yesterday, and I don't know what I changed. Now I get the following error message:
Traceback (most recent call last):
File "main.py", line 32, in <module>
bytesize=serial.EIGHTBITS
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/serial/serialutil.py", line 260, in __init__
self.open()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/serial/serialposix.py", line 280, in open
self._reconfigurePort()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/serial/serialposix.py", line 409, in _reconfigurePort
termios.tcsetattr(self.fd, TERMIOS.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
termios.error: (22, 'Invalid argument')
I'm a complete beginner to Python, and can't decipher the error message above. I've tried reinstalling Pyserial, but that didn't fix the error. What is wrong?
I had the same issue, though I was using a 5 port USB hub with 3 different USBs all plugged into it, and it turned out the hub wasn't providing enough power. Once I got an externally powered USB (one that wasn't just pulling off the line from my computer's port) it worked just fine.
Is the port still there? I meen /dev/tty.FireFly-16CB-SPP. It could have a new name today...
Try this :
ser = serial.Serial('/dev/tty.FireFly-16CB-SPP',115200)
print "port is open" if ser.isOpen() else "port is closed"
you dont have to call open() unless you changed the configuration of the port manually eg:
ser = serial.Serial()
ser.baudrate = 19200
ser.port = 0
ser.open()
and don't forget to cles the port when you are done with it
Reinstalled Python and downgraded to 2.6 which solved everything.
I have the same problem.
Just run the miniterm.py from pySerial examples (http://sourceforge.net/projects/pyserial/develop) .
It ran fine from shell: python miniterm -p ttyUSB0 -D
but when tried to load it into Eclipse. and run debug from there.
it gave me:
pydev debugger: starting
Traceback (most recent call last):
File "/eclipse/plugins/org.python.pydev.debug_2.0.0.2011040403/pysrc/pydevd.py", line 1134, in <module>
debugger.run(setup['file'], None, None)
File "/eclipse/plugins/org.python.pydev.debug_2.0.0.2011040403/pysrc/pydevd.py", line 918, in run
pydev_imports.execfile(file, globals, locals) #execute the script
File "/root/workspace/pyserial/src/examples/miniterm.py", line 120, in <module>
console.setup()
File "/root/workspace/pyserial/src/examples/miniterm.py", line 101, in setup
self.old = termios.tcgetattr(self.fd)
termios.error: (22, 'Invalid argument')
I had this problem on OSX, and the problem ended up being the baud rate was not supported. Changed the baud rate to something more common and it worked!

Categories

Resources