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()
Related
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'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)
So I'm trying to create a python script that will allow multiple TCP connections from different computers and allow talking over the serial from the TCP client. Here is what I was playing with
import sys
import os
import time
import fileinput
import socket
import serial
import thread
serialData = serial.Serial('/dev/ttyS0',9600)
import argparse
def on_new_client(clientsocket,addr):
while True:
msg = clientsocket.recv(1024)
#do some checks and if msg == someWeirdSignal: break:
print addr, ' >> ', msg
serialData.write(msg)
#msg = raw_input('SERVER >> ')
#Maybe some code to compute the last digit of PI, play game or
anything else can go here and when you are done.
clientsocket.send(msg)
clientsocket.close()
def on_Send_client(clientsocket,addr):
while True:
x = serialData.readline()
clientsocket.send(x)
clientsocket.close()
s = socket.socket()
host = '' #ip of raspberry pi
port = 12345
s.bind((host, port))
s.listen(5)
serialData.isOpen()
serialData.write("This is a test")
while True:
c, addr = s.accept()
thread.start_new_thread(on_new_client,(c,addr))
#thread.start_new_thread(on_Send_client,(c,addr))
s.close()
Right now I can connect with multiple TCP clients and eco their data. Their data also gets sent out the serial port. How should I go about buffering the serial data and sending it back out any and all connections that are active? Seems like nothing I look for online can do this correctly and Socat command even fails for multiple connections to serial.
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)
I have downloaded ceSerial and successfully communicate with serial ports. I have successfully write the data to the port but I am not able to read the data from the ports. It is getting connected to the port but it is not able to read the values. Below is my code snippet for reading the data from the port.
from time import sleep
import ceserial
ser = ceserial.Serial(port="COM1:",baudrate=9600,bytesize=8,stopbits=ceserial.STOPBITS_ONE,parity=ceserial.PARITY_EVEN)
print("connected to: " + ser.portstr)
#data = ''
while True:
data = ser.read(9999)
if len(data) > 0:
print 'Got:', data
sleep(0.5)
#print 'not blocked'
ser.close()