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)
Related
I have a weight scale
Which is connected to the serial port, and I'm trying to know the weight is currently reading.
This is the code I'm using in Python.
import serial
s = serial.Serial(port="COM3")
s.read(10)
It makes the connection but it just keeps loading and doesn't give any output.
I also Tried:
ser = serial.Serial()
ser.baudrate = 9600
ser.port = 'COM3'
print(ser)
and this is the output:
Serial<id=0x192eaed4c40, open=True>(port='COM3', baudrate=9600, bytesize=8, parity='N',
stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
Thank you.
If the device you are connected to, doesn't write 10 bytes, your read call will block until it has gotten all those 10 bytes.
Typically, you as a reader have to say to the device that "hey, im here, could you give me data", and only then they will return you something. Also, you can check ser.in_waiting property to see if there is any data that can be read (and how much of data there is)
import serial
ser = serial.Serial(
port = "COM2",
timeout = 1,
baudrate=9600,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.SEVENBITS,
)
ser.write(str.encode("W"))
weight = ser.read(8)
print(weight.decode('ascii'), weight)
You want to get the input from the weight scale device. You should follow the following steps:
Firstly, check the connection between the device and computer is normal (cable, USB port, computer). You can use the terminal software.
If the connection is ok. Now let's start with the following code:
port='COM8',
baudrate = 2400,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.SEVENBITS,
timeout=None
)
while 1:
x = ser.readline()
print(x)
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.
Inputs to relay must be a 8 bit number (something like 00000001 or 00000010) which will be pass on the serial port communication to switch on and off the relays, but the following code is not working ...
import serial
ser = serial.Serial( port='/dev/ttyUSB0', baudrate=9600,\parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS, timeout=0)
if ser.isOpen()==True:
print("device opened")
print("connected to: " + ser.portstr)
command = b'\x1'
ser.read()
while True:
ser.write(command)
I have written some basic python code to allow connection over com port. I would like to create a function to pass any com port into the function.
I have written some basic python code to allow connection over com port. I would like to create a function to pass any com port into the function.
Original code
#Import Serial
import serial
#Import Time
import time
# Set COM Port.....
ser = serial.Serial('COM12', 115200, timeout=0, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, rtscts=0)
Attempted function code
def my_function(port):
# Import Serial
import serial
# Import Time
import time
# Set COM Port.....
ser = serial.Serial('COM + "port"', 115200, timeout=0,
parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, rtscts=0)
myfunction(COM12)
You need to pass 'COM12' as a string to the function ie. with quotation marks:
import serial
import time
def my_function(port):
# Set COM Port.....
ser = serial.Serial(port, 115200, timeout=0,
parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, rtscts=0)
my_function('COM12')
When you pass 'COM12' to the function, it's essentially setting port = 'COM12' inside the function.
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()