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
Related
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.
I have a keyence sensor FD-XA2 which is mounted on the communication unit NU-EP1. I connected it to my PC with an Ethernet cable. I thought it would be rather easy to read the values with python by just receiving them from the Ethernet-connection. But I have no idea how to set up the communication between the sensor and PC. I was able to create a client and server with the socket package on my PC, which is working fine. But I have literally no idea if the sensor needs to be a client or server. How do I set up the whole thing correctly?
The manufacturer support told me they just have experience with reading the sensor when it is connected to a PLC. This would be my second option. But I thought maybe I can just read the sensor without a PLC.
Thanks!
i am sending some commands having particular response serially using com port..the commands are kept in a file..i am reading each command through the file line by line and sending it serially over the com port..but when i am seeing it from the receiver end using Magic Terminal(Software)..i found that each command is going multiple times..which i am sending only one time..i have made a code in pycharm..and in the console i am seeing that command is going only once but from the uart receiving end the story is something else..i am stuck with this problem..i have maintain the same baudrate and everything but not able to diagnose the issue..
github link for the code is: https://github.com/AkshatPant06/Akshat-Pant/blob/master/cmd%20list
def recvResponse():
ser.write(serial.to_bytes(intCmd))
time.sleep(1)
data_recv=ser.read(2)
return data_recv
this i have used to receive the 2 byte response..
There seems to be nothing wrong with your code. At least to the extent I could reproduce, it only sends the command once (I tried your function after setting up my serial port in loopback).
I cannot say for sure but it might be that the terminal you're using has two windows, one for input and another one for output and somehow you're getting confused with what is in and out of your port.
One easy way to deal with this kind of issue is to use a sniffer on your port. You can do that combining com0com and Termite on Windows, as I recently explained here.
As you can see there is only one window on this terminal, and after setting up the forwarding you'll everything that comes in and out of your port. That should make it easier to see what your code is writing and reading.
To give you a conventional scenario to apply the sniffer trick you can refer to the following screenshot:
In this case, we have two real serial ports on a computer. On the first (COM9) we are running a Modbus server (you can imagine it as a bunch of memory addresses, each of one storing a 16-bit number). On COM10 we have a client that is sending queries asking for the contents of the first 10 addresses (called registers using the Modbus terminology). In a general use case, we have those ports linked with a cable, so we know (theoretically) that the client on COM10 is sending a data frame asking for those ten registers and the server on COM9 is answering with the numbers stored on those registers. But we are only able to see the contents on the server (left side of the picture) and what the client is receiving (right). What we don't see is what is traveling on the bus (yeah, we know what it is, but we don't know exactly how the Modbus protocol looks like on the inside).
If we want to tap on the bus to see what is being sent and received on each side we can create a couple of virtual ports with com0com and a port forwarding connection with Termite, something like the following screenshot:
Now we have moved our Modbus server to one of the virtual serial ports (COM4 in this case). After installing com0com we got (by default, but you can change names or add more port pairs, of course) a pair of forwarded ports (COM4<-->COM5). Now, if we want to see what is circulating through the ports we open Termite (bottom-right side of the picture) and set up another port forwarding scheme, in this case from virtual port COM5 to the real port COM9.
Finally (and exactly the same as before we were sniffing), we have COM9 connected together with COM10 with a cable. But now we are able to see all data going to and fro on the bus (all those HEX values you see on Termite displayed with the green/blue font).
As you can see, this will offer something similar to what you can do with more professional tools.
I'm new to this kind of things so I need help from you guys. When playing around with serial ports, I figured why not echo from one terminal and cat/listen from the other using one of the /dev/ttyS* serial ports on my ubuntu 15.04 laptop. When i do cat /dev/ttyS0 it gives me input output error. then i tried doing it using pyserial on python. when i do
import serial
ser = serial.Serial('/dev/ttyS0')
it gives me SerialException: Could not configure port: (5, 'Input/output error')
After looking around on stack overflow, I found this related post Pyserial: could not configure port: (5, 'Input/output error)
where one of the guy said you need a physical connection to make this happen. I'm guessing that is to create some kind of loop. So my question is what kind of physical connection is required? Who a usb stick do it, or do i need something dedicated to work with the serial port. And if someone would provide good explanation on how serial port programming works, that would be great.
Feel free to edit the question where you see fit. Guys please no need to complain about how dumb this question sounds and all that. After all lots of people come here to learn, and that is what i am doing
You need to have one or two physical com or serial ports on your pc to achieve this. if you have one port you can create a loopback plug by connecting the Rx and Tx pins together.
If you don't have a physical serial port and want to test it you can use socat to create a virtual serial port.
Physically you need a cable that swaps the transmit and receive pins. This is known as a "null-modem cable" because it can connect two computers together without a modem in between.
You probably also need to add yourself to the "dialout" group in order to use the serial ports.
putty is a really good program for testing with serial ports -- you can have two putty instances talking to each other, and then try to connect putty on one side with Python on the other side.
Finally, sometimes there is a modem daemon that will run and think that it should own all the serial ports. It usually gives up and lets them go after awhile, but you might have to find it and kill it if you don't want random processes polling for modems on your serial connection.
Is there anyway to do this without getting a "COM PORT IN USE" error? I have a a service that listens to gps GPRMC sentences on a com port. But I don't have this device on my testing computer. So I wanted to write a python script to simulate GPRMC sentences on the port while my other python script listens to the same port and parses.
Writing to a serial port does not leave a message on the serial port to be read by the same device. This just isn't how a serial port works and is not how most OSes are written to allow as a buffer behavior. What you really need is a virtual serial port.
Check out this section of a wikipedia article on COM port redirectors and see if any of it will fulfill your needs. Otherwise I recommend searching for COM port emulator, serial port virtualization, etc. until you find software that will work for your use case and operating system. This might be hard, especially if timing is important to your simulations.
Edit: To make this slightly more clear, let's talk about what the pySerial library is actually doing to communicate with python. pySerial is just communicating to the OS's API for the serial port. The OS will, generally, model this as a location to write information to and a location to read information from (buffered in just about all modern computing systems). What's important to understand is that from the point of view of the OS (how the serial port is modeled), the write location can ONLY be written to and the read location can only be read from. This may or may not be how the actual serial hardware interfaces with the machine, in most serial port hardware and interface designs that I've worked with, this is the case for the sake of simplicity and reduced cost. Because of this, you are down to two basic choices.
Give the OS a virtual serial port that you can read to AND write from somehow
Possibly simpler, put a null modem adapter on one of your computer's serial ports and, using a serial cable, connect the two ports. You can now have your service on one port and your simulated device script on the other.