I want to emulate an Arduino serial communication in Windows. I wrote this pySerial script to represent the connection:
from serial import Serial
from time import sleep
serial_conn = Serial(<some port>)
serial_conn.baudrate = 9600
for i in range(1,10):
serial_conn.write(<dummy data>)
sleep(1)
The problem is the following: I tried to use available serial ports (like COM1 or COM3 for example) but I can't sniff the port with a serial monitor tool. It's because I need hardware to open the port? Or maybe the problem are the tested ports? (maybe Windows uses the COM1 for comm as Linux uses the first serial too). Should I try with a virtual serial ports tool? If that's the point, can you recomend me someone and the usage?
In Windows hardware and virtual serial ports have the same enumeration scheme, so they will be COM.
The problem is that only one program at time (theorically) can use a Serial port, so if the port is used by your Python program, it won't be available to the terminal.
You should setup a fake COM, and this mean a custom driver... too much difficult.
Socket, file and standard input can be read/written one byte a time, so you can test your parser using them.
Related
In short, I have written a Python program to write a series of commands to a serial device I'm working with, and I want to make sure they're working in the way I want them to.
For sure, no device connected means no response/nothing to read...
Did you consider trying a serial port sniffer, if you google for serial port sniffer, you'll find more than you need.
Connect any serial device and sniff what you send.
I'm trying to test writing messages to serial port using pyserial and reading it again using two usb to serial adapters connected back to vack "USB-Serial --> Serial-USB" to verify it is writing to the serial port as it is meant for communication with hardware,
so I have open console witch is reading all the time
import serial
port = 'COM6'
read_ser = serial.Serial(port)
while True:
x=read_ser.read()
print(x)
and for writing I use
import serial
port = 'COM5'
ser = serial.Serial(port)
# ser.write(str.encode('$GPRMC,081117.24,A,5042.988302,N,1058.376556,E,14.7,,230813,0.0,E*74'))
ser.write('$GPRMC,081117.24,A,5042.988302,N,1058.376556,E,14.7,,230813,0.0,E*74'.encode())
And nothing is shown in the reading console
I tried both adapters and read from external serial hardware successfully.
I'm using python 3.7 on windows 10
I figured it out
The problem was with the pin layout as I used gender changer to connect the two serial adapters
using serial cable instead solved the problem.
I am using a telit he910g card. it is connected to my PC directly using a miniPCI slot.
I am using it for 3G internet connection and A-GPS/GPS services.
My system is running linux mint 17.1, the 3G connection is handled using the network manager APP and works great. The 3G connection is started and handled using a module that is part of a program I am writing.
The code I am using in order to connect to the serial port is this:
def _connect_to_device(self):
""" Connect to a serial port """
try:
self._device = serial.Serial(self._filename, baudrate=self._baud_rate)
except StandardError, e:
raise StandardError("Couldn't connect to GPS device. Error: %s" % str(e))
When I use the python program alone it works great. But when I try and use it while the 3G is on i cant connect to the serial device. The wierd thing is that if I try to connect to it using a program like "minicom" while 3G is turned on it DOES work.
So my question is: how can I make both run and work together? since now they are mutually exclusive.
thanks to all who help. :)
Glad you found a way round your problem. Just for completeness:
Normally, serial ports can be opened by multiple processes.
If one of them does ioctl(,TIOCEXCL) on the open file then further opens will return EBUSY until everyone closes the device. Only root can get past this and open the device at all times.
If root opens the device and does an ioctl(,TIOCNXCL), then other processes can open the device too.
In python, TIOCNXCL isnt defined anywhere, but you can do the ioctl (eg on stdin) with:
import fcntl
TIOCEXCL = 0x540c # from /usr/lib64/perl5/asm-generic/ioctls.ph
TIOCNXCL = 0x540d
print fcntl.ioctl(0, TIOCNXCL)
Ok, so it is solved.
the issue was that the telit module has 2 ports /dev/ttyACM0 (high speed) and /dev/ttyACM3 (lower speed).
I tried to connect to the high speed one, but apparently the 3G uses that one and it causes contentions.
So moving to use the lower speed port in my script solved the issue.
I am relatively new to Python. I wrote a script and need to add triggers that need to be send via an usb serial port to another pc. The problem is that the triggers (in this code example the 2) never show up on the software on the other pc. When I check it with the print() command, it does print a value, but the printed value is the same number for two different triggers. I have read other posts, I searched the internet, and I tried various things, but I didn't manage to resolve this iusse. This is the code I use for interfacing with the serial port (COM3).
#this part of code is defined at the beginning:
import serial
ser = serial.Serial(port=2, baudrate=9600)
ser.close()
#this part of the code later on to interface with the serial port:
ser.open()
ser.write(chr(2))
ser.close()
Maybe anyone here has any suggestions on where the problem could be? Thanks!
If you haven't already, check that the port settings are correct, 9600,8,N,1 for example. These must match the settings of the remote serial port.
It might be useful to check that the serial connection does work with a terminal emulation program such as minicom (linux), or PuTTY (Windows). Once you have verified that a connection can be made and data transferred, you can be certain that you are connecting to the correct local port, that the port settings are correct, and that your serial cable is working properly.
I am working on a multi threaded server application for processing serial/USB ports.
The issue is that if a cable gets unplugged, pyserial keeps reporting that the port is open and available. When reading I only receive Empty exceptions (due to read timeout).
How do I find out that a port has been disconnected so that I can handle this case?
Edit: OS is Ubuntu 12.04
Edit 2: Clarification - I am connecting to serial port devices via Serial to USB connector, thus the device being disconnected is an USB device.
A Serial port has no real concept of "cable connected" or not connected.
Depending on the equipment you are using you could try to poll the DSR or CTS lines, and decide there is no device connected when those stay low over a certain time.
From wikipedia:
DTR and DSR are usually on all the time and, per the RS-232 standard
and its successors, are used to signal from each end that the other
equipment is actually present and powered-up
So if you've got a conforming device, the DSR line could be the thing you need.
Edit:
As you seem to use an USB2Serial converter, you can try to check whether the device node still exists - you don't need to try to open it.
so os.path.exists(devNode) could suffice.