pyserial - I can't read from port - python

thanks in advance.
I'm trying to read from a serial device using future technology devices international ltd bridge(i2c/spi/uart/fifo).
The device is designed to only send data. It has been tested in a Windows environment and is working correctly.
I'm using Ubuntu 16.04.2 LTS with the pyserial python module as follow:
import serial
ser = serial.Serial(
port ='/dev/ttyUSB0',
baudrate=38400,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=5
)
buff = ser.read(128)
print buff
ser.close()
When I run the script I can't read anything from the device. Do you have any idea or suggestion?
Thank you very much for the help.

Related

Python connection with a Device through serial port

I need help with a python code to connect with a Router or a Switch via serial port. I use pycharm from a Windows terminal.
This is what I tried:
import keyboard
ser = serial.Serial(port="COM1", baudrate=9600, bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE)
while True:
ser.write('You got connected\r\n'.encode('Ascii'))
receive = ser.readline()
print(receive.decode('Ascii'))
if keyboard.is_pressed('q'):
print('User need to quit the app.')
break
ser.close()
Tried and I got errors.
What error do you get ?
This would be very helpful if you could copy/paste your errors in here.

Interfacing Nextion with Raspberry Pi 3b+

I am having trouble interfacing my Nextion display via Raspberry Pi. I have a Nextion project that contains one text field called t0. And I want to change the text displayed from Raspberry.
Here is what I tried:
import serial
import time
import struct
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate =9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1)
k=struct.pack('B', 0xff)
time.sleep(1)
command = 't0.txt=\"hello\"'
ser.write(command.encode())
ser.write(k)
ser.write(k)
ser.write(k)
I am certain that connections are OK hence it is not hardware related.
Thank you for support.
Instead of this:
command = 't0.txt=\"hello\"'
I changed the command line to this:
command = 't0.txt="hello"'
Works for me good luck.

Write ASCII command with Pyserial

I am using a kunbus RevPi connect + to control a pump with a USB serial converter. To control it, I need to send ASCII command so I try to use the Pyserial library.
import serial
import time
ser = serial.Serial('/dev/ttyUSB2', baudrate = 9600, bytesize=8, parity='N', stopbits=1, timeout=1)
print (ser.portstr)
ser.write(b"1H[CR]")
I haven't error message but the pump doesn' start.
Can you help me please?

Bluetooth serial connection between Raspberry Pi and Windows 10 times out after a short while

I want to set up serial Bluetooth communication between a Raspberry Pi 3 with integrated Bluetooth module and a Windows 10 machine. The raspi is the server and runs a python script, that handles the connection and the data transmission. I have set it all up, following several tutorials in the internet and its working really good on the server side. I have tested the server with a serial terminal on my android phone and it was working as expected: All the messages were transferred correctly and it stayed connected as long as i wanted. Testing with my windows machine, on the other hand, caused a lot of problems. I have solved most of them and now i can connect my windows machine, over a serial terminal, to the raspi. My problem now is, that, after a short while, the connection dies, usually after 10 to 40 seconds, and i could not find a fix for that. The error message on windows is: cannot open COM4 and the error message on the raspi is: bluetooth.btcommon.BluetoothError: [Errno 110] Connection timed out.
Here is my python script, id that is actually the problem:
import serial, bluetooth, subprocess, select
serialPort0 = serial.Serial(
port = '/dev/serial0',
baudrate = 115200,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1
)
server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
server_sock.bind(("", bluetooth.PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
bluetooth.advertise_service(server_sock, "bluetoth_server", service_id=uuid,
service_classes=[uuid, bluetooth.SERIAL_PORT_CLASS],
profiles=[bluetooth.SERIAL_PORT_PROFILE],
# protocols=[bluetooth.OBEX_UUID]
)
print("Waiting for connection on RFCOMM channel", port[1], ", addr", port[0])
client_sock, client_info = server_sock.accept()
print("Accepted connection from", client_info)
client_sock.setblocking(0)
while True:
if not serialPort0.isOpen:
raise Exception("Serial port closed unexpected")
break
ready = select.select([client_sock], [], [], 0.5)
if ready[0]:
blu_data = client_sock.recv(4096)
if not blu_data:
break
serialPort0.write(blu_data)
ser_data = serialPort0.readline().decode("utf-8")
client_sock.send(ser_data)
client_sock.close()
server_sock.close()
It reads data from a serial port and from a serial Bluetooth port and sends it to the other one respectively.
I am at a point, where i don't know what else to try and I would really appreciate some help. Thank you in advance.
Your description related to timeout would be pointed to
/etc/bluetooth/main.conf
Bluetooth configuration file.
You will need to uncomment the pairabletimeout=0 that will prevent the timeout problems.
You may want to see Raspberry Bluetooth configuration (recent answer) for details.
Bluetooth Serial Configuration

Error in working with pyserial module of python

import time
import serial
# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
port='/dev/ttyS0',
#port='/dev/ttyACM0',
baudrate=115200,
parity=serial.PARITY_ODD,
#stopbits=serial.STOPBITS_TWO,
#bytesize=serial.SEVENBITS
)
ser.isOpen() # returns true
time.sleep(1);
ser.write("some_command \n")
ser.close()
I have a embedded board. It has a serial port which is connected to my computer. I am running above script to access this serial port and run some board specific commands.
My problem
I open my serial port (using minicom in linux) separately and then run above script, It works. If I don't open serial port separately, Script doesn't work.
try
ser.write("some_command \n".encode())
alternatively try
ser.write(bytes(b"some_command \n"))

Categories

Resources