Python Function to configure com port - python

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.

Related

Read from serial in Python

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)

How to switch on/off relay using serial port on raspberrry pi?

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)

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()

Categories