Get device or serial port use count - linux

Is there any way to get the number of programs that have opened a file descriptor (in this case serial port)?
I want to know if somebody is connected to the serial port, and if yes, how many programs.
I want to do so in linux.
TA

I believe fuser should do the trick.

Related

How to know which ttyS* file is linked to which serial port?

I'm no expert of Linux or serial programming, and my understanding of Linux serial port communication is: the system links certain /dev/ttyS* file to a certain physical serial port, then the system or other procedures can talk with any device that is attached to that serial port via the /dev/ttyS* file. And the /dev/ttyS* file would be assigned/linked to the serial port no matter there's any device attached.
If I'm correct about this, then is there any way, in C, that I can get all such /dev/ttyS* files which are linked to physical serial ports?
Have already searched all over Google and SO, nothing really helpful found, plz halp!
PS, I can find such files by using:
dmesg | grep ttyS
but I need a more precise way to get these info, things like libudev can do this ??
I'm not absolutely sure what is being asked here, but the way /dev/ttyS* are mapped has not changed in ages and the first serial port, sometimes called with DOS name COM1, is still accessible as /dev/ttyS0, second being /dev/ttyS1 and so on.
From kernel documentation, namely from file Documentation/devices.txt you can still find some useful information:
4 char TTY devices
0 = /dev/tty0 Current virtual console
1 = /dev/tty1 First virtual console
...
63 = /dev/tty63 63rd virtual console
64 = /dev/ttyS0 First UART serial port
...
255 = /dev/ttyS191 192nd UART serial port
UART serial ports refer to 8250/16450/16550 series devices.
If your question how to find all serial ports on your system, see /dev/serial which should have (unless you're using a kernel really ancient) entries by-id and by-uuid.

How to write binary data into a serial port (RS232) device file in linux?

I'm doing a project in linux for which i need to write binary data into the device file of serial port (RS232) port. when i write the file into serial port (RS232), a RF transmitter connected to this port must send signal to RF receivers situated at different places.
The data to written is in a user created file.I have to just copy the data from that file and write into the device file whenever user wants(say a button click in java interface)
I have googled but couldn't get much of the information and some i couldn't understand.What is the simplest way to do this using shell script.(C program will work too)
From the command line, for example:
cat file.bin > /dev/ttyPS0
file.bin is binary file,
/dev/ttyPS0 is device representing serial port.
Note: use stty to configure serial port appropriately.

Access denied when trying to access serial port for the second time in Cygwin

I am trying to get data from a USB serial port that is connected to an Arduino. I am using Cygwin and I write
cat /dev/ttyS4
to output the data in the shell.
When I stop the process, I am given
Access Denied
when I try to access it again. I have to close Cygwin, open it again and type in the same
command to get the output to the shell.
I have noticed that I am able to read the serial port from only one program. For example, if I read the data from the serial port in the Arduino Software, I can't access it in Cygwin.
Is there a way I can access the serial port data as many times as I want in Cygwin without having to have to close the program, open it again and write in the same command?
It appears that the statement cat /dev/ttyS4 would echo characters from the serial port until the end of the file is reached. Only, by nature, a serial port never reaches the end. So, you would need to arrange for the input to "end". One way would be to have the Arduino put an end of file character (control-D) into the output stream. The other way would be to use the so-called "heredoc" by which you tell it to look for a string to end on, as detailed in this question.
There are still a number of problems with this, though. One is, it seems wrong that control-C wouldn't close the access to the serial port. The other is, I tried this on my machine, and I can't get it to produce the problem you asked about. So, that's as much as I can offer.

Serial port routing in Linux

After reading about serial ports, virtual serial ports and such, I need a little advice to see if this is even possible. I've set up two serial ports on a Linux machine (running Ubuntu). I'd like to route the two serial ports together. Is this even possible?
The two serial ports are automatically started through the /etc/init/ttyXXX.conf getty scripts. I'd like it so that when the first serial port receives a character, it outputs that character straight away to the second serial port, and also the vice versa.
Any easy way to do this through a program or bash scripts?
The idea is that both serial ports should be able to access the linux machine with commands. However, it would be nice to be able to see the outputs of the commands regardless of which port you are attached to. For example, if port 1, logged on as root, sends "echo testing", I'd like for port 2 to see the output, but also able to see that port 1 sent the command.
Thanks
A small Perl script like this might do what you're hoping, though I'm not quite sure what you're asking, so please comment if it's not working the way you'd hope. I've only got it going one way because I think they'd just keep sending the same character back and forth if it were two way. You might also need to change the port paths near the top to whatever yours are.
Just save it as serial.pl or similar, make it executable and run it.
#!/usr/bin/perl
use strict;
use warnings;
use Device::SerialPort;
my $port1_path = '/dev/tty1';
my $port2_path = '/dev/tty2';
my $port1 = Device::SerialPort->new($port1_path);
$port1->databits(8);
$port1->baudrate(19200);
$port1->parity("none");
$port1->stopbits(1);
my $port2 = Device::SerialPort->new($port2_path);
$port2->databits(8);
$port2->baudrate(19200);
$port2->parity("none");
$port2->stopbits(1);
while ($in = $port1->input) {
$port2->write($in);
}
It is possible to connect two serial ports between each other, with a crossover cable (so that the input of one port is connected to the output of the other port).
Assuming that you ports are correctly configured (drivers installed and loaded) and that your crossover cable is connected between your ports, you can type the following commands in two terminals:
Terminal 1: listen in the output port
$ tail -f /dev/ttyXXX
Terminal 2: write to the input port
$ echo "Hello!" > /dev/ttyYYY
If the two ports are correctly connected, the message "Hello!" will be displayed in terminal 1.
The hardest part is often to know which hardware port correspond to which device file.
If you just wanted to connect the two serial ports, you could use
socat /dev/ttyS0,raw,echo=0,crnl /dev/ttyS1,raw,echo=0,crnl
(see http://technostuff.blogspot.com/2008/10/some-useful-socat-commands.html)
But, since you want to interact with the command interpreter, I think you'll need to write a Perl script that
Opens both serial ports
Uses select to wait until one of the ports has some input for you
Pass that input to the shell
Write the output of the shell command back to both serial ports

How can I monitor data on a serial port in Linux?

I'm debugging communications with a serial device, and I need to see all the data flowing both directions.
It seems like this should be easy on Linux, where the serial port is represented by a file. Is there some way that I can do a sort of "bi-directional tee", where I tell my program to connect to a pipe that copies the data to a file and also shuffles it to/from the actual serial port device?
I think I might even know how to write such a beast, but it seems non-trivial, especially to get all of the ioctls passed through for port configuration, etc.
Has anyone already built such a thing? It seems too useful (for people debugging serial device drivers) not to exist already.
strace is very useful for this. You have a visualisation of all ioctl calls, with the corresponding structure decoded. The following options seems particularly useful in your case:
-e read=set
Perform a full hexadecimal and ASCII dump of all the data read from
file descriptors listed in the
specified set. For example, to see all
input activity on file descriptors 3
and 5 use -e read=3,5. Note that this
is independent from the normal tracing
of the read(2) system call which is
controlled by the option -e
trace=read.
-e write=set
Perform a full hexadecimal and ASCII
dump of all the data written to file
descriptors listed in the specified
set. For example, to see all output
activity on file descriptors 3 and 5
use -e write=3,5. Note that this is
independent from the normal tracing of
the write(2) system call which is
controlled by the option -e
trace=write.
I have found pyserial to be quite usable, so if you're into Python it shouldn't be too hard to write such a thing.
A simple method would be to write an application which opened
the master side of a pty and the tty under test. You would then
pass your tty application the slave side of the pty as the 'tty device'.
You would have to monitor the pty attributes with tcgetattr() on the pty
master and call tcsetattr() on the real tty, if the attributes changed.
The rest would be a simple select() on both fd's copying data bi-directionally and copying it to a log.
I looked at a lot of serial sniffers. All of them are based on the idea of making a virtual serial port and sniff data from that port. However, any baud/parity/flow changes will break connection.
So, I wrote my own sniffer :). Most of the serial ports now are just USB-to-serial converters. My sniffer collects data from USB through debugfs, parse it and output to the console. Also any baudrate changes, flow control, line events, and serial errors are also recorded. The project is in the early stage of development and for now, only FTDI is supported.
http://code.google.com/p/uscmon/
Much like #MBR, I was looking into serial sniffers, but the ptys broke the parity check. However, his sniffer was not helping me, as I'm using a CP2102, and not a FT232. So I wrote my own sniffer, by following this, and now I have one that can record file I/O on arbitrary files: I called it tracie.

Resources