Using Pyserial to read and write data - python

I am trying to understand how Pyserial is interfacing with my windows machine. I am using this basic code from the pyserial website.
import serial
ser = serial.Serial('COM1') # open serial port
print(ser.name) # check which port was really useds
ser.write(b'hello') # write a string
ser.close() # close port
I am using python 2.7 and IDLE as the IDE for this code, I am wondering should I be able to read the string hello from my console after I run the code? My output as of right now only show COM1, while not printing hello to my console. Is the hello message being sent to my COM1 port? If so, how would I be able to read the message I sent?
Also as a bonus question, I sometimes randomly have this error happen to me.
SerialException: could not open port 'COM1': WindowsError(5, 'Access is denied.'). Sometimes when I re-run the code it goes away, other times I have to restart my computer. I appreciate any help, thanks.
COM1 is not connected to anything currently.

You should open the connection first with ser.open(). You can write the command like you are doing, but to read it, you need to use ser.readlines(). Then you can close the connection. This page has a lot of good info! What is connected to the COM1 port?

Related

How to make python sniffs on serial port that is used by another program

I have an accounting program in windows 10 that I want to extract data from it using python code.
I want to do that by sniffing on the serial port that the accounting program writing data to it.
I have two problems:
1-I used process manager to see the internal serial port that the programs are using but I can't find the serial port that the accounting program is using to write data to it.
2- I used virtual ports to try to sniff on it using python serial library, when I try to access the red/write com ports I have PermissionError(13, 'Access is denied.', None, 5).
I'm expecting that the accounting program only start using serial port after some trigger like start printing order or something else.
and after I find the serial port that the program is using how to sniff on it using python?
I would appreciate it if someone can make the idea clear.

python pyserial won't pull data from port

Having trouble with reading from a serial port.
i have a bit of hardware that shows a display with date and time and other details and on the PC you through some software you can bring up a live mirror of the display. what i want to do is log some data at intervals.
using pyserial its trivial to connect to the port, but when reading it using the simplest settings with timeout=none and read size =1 and leaving the port listening indefinitely there is no output from the equipment.
ive tested using virtual port pairs and i can read and write no problem.
Any ideas why there is no output from the serial port?
And i know its the right port because when i open it then it blocks the mirrored display on the PC.
Cheers

How to read data sent to an XBee on Windows?

How can I read the data I am sending to my XBee connected to my Windows machine?
I want to see if the data is being sent correctly, because my code is compiling correctly in IDLE, but if I try to read the serial console in XCTU it says the port is currently being occupied. Any ideas on how to read the data I'm sending?
import serial
i = 'A'
ser = serial.Serial('com3',9600,timeout =1)
ser.write(i)
ser.close()
You have both XBees connected, while communicating to one in the python code, you have the other in the serial console in XCTU. Writing to the port will display the message.
Are you trying to open COM3 in XCTU? You won't be able to do that since you have it open in Python. Is that XBee module paired with one on another serial port where you'd be able to see the output?
You might want to add a delay between the ser.write() and ser.close() calls to ensure you're giving it time to send the data. It's possible that the first call just queues the data to send.
Have you considered using the Python-Xbee library also? It makes decoding packets easier:
https://github.com/nioinnovation/python-xbee
This library also has support for Zigbee.
Jim

Using pySerial to connect to a non-COM port

In Hyperterminal I am able to connect to a serial port called "X64-CL_iPro_1_Serial_0" where I am able to send/receive ASCII text to a camera. However when I try to connect to the same port with pySerial, it throws an exception:
SerialException: could not open port X64-CL_iPro_1_Serial_0: [Error 2] The system cannot find the file specified.
I don't understand why Hyperterminal can detect the port and communicate with it, but Python can't. I downloaded this script from the pySerial website that displays a list of serial ports, and the only ports it came up with was COM1 and COM2, neither of which I can connect to.
My code is very simple, and looks like this:
import serial
port = "X64-CL_iPro_1_Serial_0"
ser = serial.Serial(port)
Am I doing anything wrong? Is there a way to work around this? Thanks ahead of time.
Edit:
It should also be noted that this port does not show up in the device manager, and neither does COM1 or COM2.
The problem lies in the enumeration code you linked. It is wrong in two regards:
It uses a fixed GUID_CLASS_COMPORT to enumerate. It should instead ask the GUID through SetupDiClassGuidsFromName, passing "Ports" as description of the class for which it is asking for names.
The code insists of asking for the friendly name of the port. But if the only goal is to open the device (instead of displaying to an user), it should directly access the DevicePath element, which is a weird-looking-but-perfectly-valid port name to pass to pySerial. The friendly name might even be totally missing.
aside
I'm not clear the question is about non-serial-port use through pyserial, or serial port that is not a COMX port in enumeration.
This may be a bit OT or too hard code for your use, but here goes first, using some other file in a pyserial object:
foo = serial.Serial()
peer = serial.Serial()
foo.fd, peer.fd = posix.openpty()
try: foo._isOpen = peer._isOpen = True # depending on pyserial version
except: pass
foo._reconfigurePort()
peer.setTimeout(timeout=0.1)
peer._reconfigurePort()
Regarding second, remember that ports beyond COM9 use weird windows notation \\.\COM10, perhaps that's what Hyperterminal does for you. pyserial doesn't do it, so perhaps you need to open the port something like this:
s = serial.Serial("\\\\.\\X64-CL_iPro_1_Serial_0") # also remember to escape backslash

Programmatically talking to a Serial Port in OS X or Linux

I have a Prolite LED sign that I like to set up to show scrolling search queries from a apache logs and other fun statistics. The problem is, my G5 does not have a serial port, so I have to use a usb to serial dongle. It shows up as /dev/cu.usbserial and /dev/tty.usbserial .
When i do this everything seems to be hunky-dory:
stty -f /dev/cu.usbserial
speed 9600 baud;
lflags: -icanon -isig -iexten -echo
iflags: -icrnl -ixon -ixany -imaxbel -brkint
oflags: -opost -onlcr -oxtabs
cflags: cs8 -parenb
Everything also works when I use the serial port tool to talk to it.
If I run this piece of code while the above mentioned serial port tool, everthing also works. But as soon as I disconnect the tool the connection gets lost.
#!/usr/bin/python
import serial
ser = serial.Serial('/dev/cu.usbserial', 9600, timeout=10)
ser.write("<ID01><PA> \r\n")
read_chars = ser.read(20)
print read_chars
ser.close()
So the question is, what magicks do I need to perform to start talking to the serial port without the serial port tool? Is that a permissions problem? Also, what's the difference between /dev/cu.usbserial and /dev/tty.usbserial?
Nope, no serial numbers. The thing is, the problem persists even with sudo-running the python script, and the only thing that makes it go through if I open the connection in the gui tool that I mentioned.
/dev/cu.xxxxx is the "callout" device, it's what you use when you establish a connection to the serial device and start talking to it. /dev/tty.xxxxx is the "dialin" device, used for monitoring a port for incoming calls for e.g. a fax listener.
have you tried watching the traffic between the GUI and the serial port to see if there is some kind of special command being sent across? Also just curious, Python is sending ASCII and not UTF-8 or something else right? The reason I ask is because I noticed your quote changes for the strings and in some languages that actually is the difference between ASCII and UTF-8.

Categories