Serial port on Raspberry Pi disconnects after several minutes and cannot be reconnected (IOError, pyserial) - serial-port

I have a device with a RS-232 DB9 port connected to my Raspberry Pi over a USB-to-serial cable.
Initially, I am able to communicate with the device without issue using pyserial.
However, after some variable length of time, I can no longer receive messages from the device.
If I close the serial port and attempt to open a new one, using the same code I normally use:
import serial
ser = serial.Serial(
serial_path,
9600,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE
)
I get:
File "/opt/venv/lib/python3.8/site-packages/serial/serialposix.py", line 713, in _update_dtr_state
fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str)
OSError: [Errno 5] Input/output error
I have no way to get around this error and re-establish communication with the device unless I physically unplug and replug the USB port.
In case it's relevant, the RS-232 device is connected to my RaspBerry Pi via an intermediate USB Hub and a USB extension cable.
I also tried setting rtscts=True and dsrdtr=True when creating the pyserial object. The IOError no longer occurs; however the serial device remains uncommunicative.
What might be the underlying issue here? Any guidance would be appreciated. Is there any way to detect the issue and reset the serial port via the shell, without requiring physical intervention?

Related

Unable to connect to xbee device in xctu

I have 3 xbee receiver devices connected to Ubuntu on port ttyUSB0, ttyUSB1, ttyUSB2. Out of these 3, device on ttyUSB2 stops working after every 2-3days. When I open xctu, it shows below error:
Could not find device on port /dev/ttyUSB2. Error initializing xbee device parameters. Connection timeout. Could not read device information.
When I type ls /dev or lsusb, I can see all 3 devices connected and all the 3 serial ports but the device on /dev/ttyUSB2 doesnt respond. When I unplug it and plug it back, it starts operating normally. But again after 2-3 days, it stops responding.
Can anyone please suggest what could be the issue.
I had the same problem. Turned out another program (Cura / Creality Slicer, in my case) was intermittendly checking the serial ports for connected devices, which (I assume) interrupted serial communication for XCTU.
Closing the other program solved the issue.

Modbus TCP/IP on RaspberryPi wit PyModBus

I need to communicate with several modules and devices using the ModBus Protocol and the Raspberry Pi.
I am using PyModBus on Raspberry Pi to read/write Modbus function codes. (https://pymodbus.readthedocs.io/en/latest/readme.html)
I was able to communicate Modbus RTU over RS485 with the device but now that I am trying to communicate ModbusTCP over a Ethernet cable and keep running into the following error:
import pymodbus
from pymodbus.client.sync import ModbusTcpClient
client = ModbusTcpClient('127.0.0.1')
connection = client.connect()
Output:
ERROR.pymodbus.client.sync: Conection to (127.0.0.1, 502) failed: [Errno 111] connection refused
Any tips or explanation for the error?
127.0.0.1 is a loopback address; this means that ModbusTcpClient('127.0.0.1') will attempt to establish a connection to the Pi iteslf. Unless there is a Modbus server running on the Pi the error you received is to be expected.
"I am trying to communicate ModbusTCP over a Ethernet cable" indicates you are communicating with another device which should have it's own IP address. You need to work out what that address is and use that when attempting to connect (as well as ensuring your network setup is valid). The method used to set/determine a devices address varies from device to device so you would need to check the documentation (you did not specify what the device is).

Implementing I2C slave with FT201XB via USB

I've been trying to test a FT201XQ USB-I2C breakout board: UMFT201XB-01, so I can connect it to a master device such as an Arduino and sniff what that device is sending through I2C.
To see the output of the slave device I have successfully configured a Virtual COM PORT by installing the D2XX drivers provided by ftdchip.com. I can open the serial port through puTTY and everything seems fine in that regard.
Then, i've loaded the "master_writer" example on my Arduino, which sends 1 byte at a time to an address (0x22 is specified in the UM201XB-01 datasheet as the default address).
Nothing seems to happen in the COM port that i've earlier opened. Do I need to configure/program the FTDI device in some way? In that case, how can I do it, in a general way?
UMFT201XB-01 board http://www.ftdichip.com/Support/Documents/DataSheets/Modules/DS_UMFT201_220_230XB.pdf
Thank you in advance, and sorry if this is a "noob" question : P

Serial port shown in device manager but unable to access/found?

I've installed the Arduino virtual USB serial port driver, it's appeared as COM4 on my device manager. I double checked by listing all available serial ports in powershell
> [System.IO.Ports.SerialPort]::getportnames()
COM1
COM4
However, when I try to do an echo to the serial port, it says it does not exist
> echo helloworld > COM4
The system cannot find the file specified.
I am trying to upload some firmware in to my arduino, but I kept getting denied by the port. Trying a different USB gives me the same result. I wrote a small program to test the serial port and I get the error:
The serial port "COM4" does not exist.
The device name of a serial port on Windows is \\.\COMn where n is the port number. A device driver can emulate the DOS device name, like "COM4" if it chooses to do so, very simple to do in the device driver code. But that's an increasingly rare thing to do, especially for port numbers larger than 2 and extra especially for port numbers larger than 4. So seeing your echo command fail is not unusual.
A serial port cannot be shared, it can only be opened once by a process and everybody else that tries to open it will get the fail-whale ERROR_ACCESS_DENIED error code. Necessarily so, serial ports sit at the very bottom layer of the OSI model, there is no protocol that arbitrates access to the port. Or to put it another way, the operating system has no guidance on who should get the data that the port receives. There can be only one candidate, the process that opened the port first. So the error message you got is not unusual, you have to make sure that nobody else is using the port. Including the Arduino serial port monitor.

Is is possible to simultaneously use Arduino serial monitor while receiving data from Processing?

I am attempting to send some data from Processing to my Arduino over the serial connection so that the Arduino can control an LED strip. Could I view the serial monitor while this transfer was taking place?
It is irking me that I cannot use any Serial.println statements (for debugging) while Processing is communicating with the Arduino. Everytime I try I get
Serial port 'COM3' already in use. Try quitting any programs that may be using it.
Is there a way for this serial communication to take place while I view the serial monitor at the same time?
The fact that the error message mentions COM3 suggests you are running on Windows. Unfortunately Windows doesn't allow multiple processes to simultaneously connect to the same serial port. This is different from Unix-based systems which do allow simultaneous serial port connections.
Using a Linux host, I have used a Python script send commands whilst monitoring results on the serial monitor. I seem to recall I had to open the serial monitor first and then run the other program.
Unfortunately, I can't help you with how to achieve that in Processing.
There is an application called Portmon that will allow you to monitor the serial communications on your PC.
Try 2 Serial communication,
Imagine you have already given USB as COM3 have a bluetooth device connected to your arduino Tx & Rx port, Let that be COM8. Now view COM3 in Arduino and COM8 in Processing.I could do this.

Resources