Serial port /dev/ttyS0 and ttys* input output error - python

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.

Related

i am sending commands through serial port in python but they are sent multiple times instead of one

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.

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

python sockets and a serial to IP device

Using a Lantronix UDS-1100 serial to IP converter. The goal is to write a small proof of concept piece in Python to capture serial data output by this device over IP.
I've done a couple test projects using sockets in python, but they were all done between python processes (python > python): listen() on one end, and connect(), sendall() etc on the other.
I think I can use sockets for this project, but before I invest a bunch of time into it, wanted to make sure it is a viable solution.
Can python sockets be used to capture IP traffic when the traffic is originating from a non-python source? I have full control over the IP and port that the device sends the serial data to, but there will be no python connect() initiated by the client. I can pre-pend then serial data with some connect() string if needed.
If sockets won't work, please recommend another solution...guessing it will be REST or similar.
Of course. TCP/IP is supposed to be cross-platform and cross-language, so in theory you should be able to communicate with every kind of device as long as you manage to process and send the expected protocol.

Need to connect a serial port that's attached to a modem to a virtual com port

I need to be able to connect to a raw serial connection that's connected to a modem.
The modem has an ip address and port.
This program works for one instance but is expensive for the amount of licenses I'd need and I'd prefer to code something:
http://www.serial-port-redirector.com/
It connects a remote ip to a local virtual com port.
I think it should be possible to do the same thing using pyserial but I'm struggling to understand how to do it.
This page kind of half explains it:
http://pyserial.sourceforge.net/examples.html#miniterm
But I'm still lost. If anyone could help me understand how to use rfc2217 in python it'd rock.
Thanks very much!
+anything for either linux/windows would be good.

Writing and listening to the same serial port in python

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.

Categories