arduino,python,pyserial,serial problem error - python

I'm doing an arduino project. And I need to make that through python on Arduino the text on Serial was transferred. But there is a problem in the python: (Sorry for the problem with English) (Google Translate)
import serial
import time
arduino = serial.Serial('COM5', 9600)
time.sleep(1)
while (True):
print(arduino.readline())
time.sleep(1)

This is how you read an Arduino serial:
import serial
ser = serial.Serial('COM5', 9600)
ser.flushInput()
while True:
try:
ser_bytes = ser.readline()
decoded_bytes = str(ser_bytes[0:len(ser_bytes)-2].decode("utf-8"))
print(decoded_bytes)
except:
print("Keyboard Interrupt")
break

In the picture in line 1 you have serial.Serial as ser:. That should be with serial.Serial as ser:. That should fix the Syntax Error.

Related

Python Serial Communication script issues (in Visual Studio)

I'm currently trying to set up communication between an Arduino UNO and a PC via a serial/USB connection using Python on a Windows PC.
The goal is to start a video on a laptop when the Arduino tells it to.
I found a guide online which lead me to the code below but Visual Studio 2017 is just showing errors seemingly at random everywhere. A lot of it seemed to come from the imports and VS's IntelliSense messing up (which I believe is fixed now).
So currently when I'm running the program it's getting stuck at the ser = serial.Serial(port, 9600, timerout=0) Line with the error that "name 'port' is not defined" Any idea why that is happening?
I'm unsure if that is the core issue since every time I'm making changes a lot of "unexpected token" and "indent" errors appear for port, newline, print and ser (completely at random)
import serial, os, time, serial.tools.list_ports
cmd = '"C:\\Users\\Josef\\Desktop\\PYTHON_PlayVideo\\PYTHON_PlayVideo\\Video1.mp4" -f --fullscreen --play-and-exit'
for p in serial.tools.list_ports.comports():
sp = str(p)
#if (sp.find('COM5') != -1):
if (sp.find('Arduino') != -1):
flds = sp.split()
port = flds[0]
print(port)
ser = serial.Serial(port, 9600, timeout=0)
while 1:
try:
line = ser.readline()
if (line):
print (cmd)
os.system(cmd)
except:
pass
time.sleep(1)
It seems that you donĀ“t find any COM port, so port is never defined.
I rework your code and this version works for me (replace USB with your keyword).
import serial, os, time, serial.tools.list_ports
# Get the COM port
Ports = serial.tools.list_ports.comports()
if(len(Ports) == 0):
print("[ERROR] No COM ports found!")
exit()
TargetPort = None
for Port in Ports:
StringPort = str(Port)
print("[INFO] Port: {}".format(StringPort))
if("USB" in StringPort):
TargetPort = StringPort.split(" ")[0]
print("[INFO] Use {}...".format(TargetPort))
if(TargetPort is None):
print("[ERROR] No target COM port found!")
exit()
# Open the COM port
ser = serial.Serial(TargetPort, 9600, timeout = 0)
if(ser.isOpen() == False):
ser.open()
while(True):
try:
line = ser.readline()
if (line):
print (cmd)
#os.system(cmd)
except:
pass
time.sleep(1)

python3 sending serial data to Nextion Display

I'm trying to control a Nextion Display via a Python3 script.
Using Windows Terminal I'm able to control it.
For example to change the text of a control I send the following:
t0.txt="test" followed by 3 time 0xFF via "send hex"
My Python3 script using PySerial is the following:
import serial
s = serial.Serial(port='COM5',baudrate=9600)
escape='\xff'.encode('iso-8859-1')
test=b't0.txt="test"'
s.write(test)
s.write(escape)
s.write(escape)
s.write(escape)
s.close()
but it is not working.
Any ideas?
Thank you so much
Read bytes from nextion EEPROM on raspberry pi3 on gpiopins.
#import time
import serial
ser = serial.Serial(
port='/dev/ttyS0',
baudrate =9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1)
commandToSend = b'rept 0,32\xff\xff\xff' #read 32 bytes of hex data from EEPROM to UART, start address is 0
while True:
ser.write(commandToSend)
#time.sleep(0.5)
x=ser.readline()
print (x)
You may try the following code below:
import serial
import time
import struct
ser = serial.Serial("/dev/ttyAMA0")
print ser
time.sleep(1)
i=1
k=struct.pack('B', 0xff)
while True:
ser.write(b"n0.val=")
ser.write(str(i))
ser.write(k)
ser.write(k)
ser.write(k)
print " NEXT"
time.sleep(1)
i=i+1
import time
import serial
ser = serial.Serial(
port='/dev/ttyAMA0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
while 1:
EndCom = "\xff\xff\xff"
write('t0.txt="Hello World"'+EndCom)

Read Serial Port in Windows CE using Python

I am trying to read from serial port using python 2.5 in windows CE 5.0.
What is happening is It gets connected to serial port but nothing gets read from the port.
Below is my Code
import ceserial
from time import sleep
ser = ceserial.Serial(port="COM1:",baudrate=9600,bytesize=8,stopbits=ceserial.STOPBITS_ONE,parity=ceserial.PARITY_EVEN,
timeout=0)
ser.open()
print("connected to: " + ser.portstr)
while True:
data = ser.read(size=50)
if len(data) > 0:
print 'Got:', data
sleep(0.5)
ser.close()

A timeout is missing in XBee API Python library

I have a problem with this library:
https://code.google.com/p/python-xbee/
There is the xbee.wait_read_frame() function without a timeout.
I am testing all serial ports for a signal, but if i cannot timout a try there is no way to do it.
Is there a posibility in Python to upgrade this function without editing the library?
Or with small changes inside the library?
ports_available = []
for port in range(0,20):
try:
ser = serial.Serial(port, 9600)
ports_available.append(port)
ser.close()
except:
pass
print(ports_available)
for port in ports_available:
ser = serial.Serial(port, 9600)
bee = ZigBee(ser)
bee.at(command=b"MY")
print(bee.wait_read_frame()) #<----------
ser.close()
Looks like you need to use the asynchronous mode described on page 3 of the documentation. It might be tricky unless the data frame includes a parameter for the serial port that received it. If it doesn't, you won't be able to connect the data to port that received it.
import serial
import time
from xbee import XBee
serial_port = serial.Serial('/dev/ttyUSB0', 9600)
def print_data(data): """
This method is called whenever data is received
from the associated XBee device. Its first and
only argument is the data contained within the
frame.
"""
print data
xbee = XBee(serial_port, callback=print_data)
while True:
try:
time.sleep(0.001)
except KeyboardInterrupt:
break
xbee.halt()
serial_port.close()

Python: Writing to and Reading from serial port

I've read the documentation, but can't seem to find a straight answer on this.
I have a list of all COM Ports in use by Modems connected to the computer. From this list, I try to open it, send it a command, and if it says anything back, add it to another list. I'm not entirely sure I'm using pyserial's read and write functions properly.
i=0
for modem in PortList:
for port in modem:
try:
ser = serial.Serial(port, 9600, timeout=1)
ser.close()
ser.open()
ser.write("ati")
time.sleep(3)
print ser.read(64)
if ser.read(64) is not '':
print port
except serial.SerialException:
continue
i+=1
I'm not getting anything out of ser.read(). I'm always getting blank strings.
a piece of code who work with python to read rs232
just in case somedoby else need it
ser = serial.Serial('/dev/tty.usbserial', 9600, timeout=0.5)
ser.write('*99C\r\n')
time.sleep(0.1)
ser.close()
ser.read(64) should be ser.read(size=64); ser.read uses keyword arguments, not positional.
Also, you're reading from the port twice; what you probably want to do is this:
i=0
for modem in PortList:
for port in modem:
try:
ser = serial.Serial(port, 9600, timeout=1)
ser.close()
ser.open()
ser.write("ati")
time.sleep(3)
read_val = ser.read(size=64)
print read_val
if read_val is not '':
print port
except serial.SerialException:
continue
i+=1

Categories