writing to serial port with a shell script - shell

I need to communicate with a serial device by sending it ascii characters. I know that in Linux you can use fopen() to write to a serial port, but I didn't know if you could do that from a shell. If not, is there another simple way to do this?

I believe you can simply redirect/cat data to the serial port from the shell. You'll want to use setserial(8) to configure baud and other information first.

Related

How can I use Ansible of a serial connection instead of SSH?

I have a bunch of Raspberry Pi devices that I only configure over the serial console. I am hoping to configure Ansible to update them for me.
I can't find any obvious way to do it (on this list for example: Plugins)
Do I need to write my own connector? Or is there an obvious way to use Ansible of a serial console that I am missing?
Do I need to write my own connector
Right, currently, there is no serial connection possible out-of-box. You will need a connector like Ansible Serial Unix and enhance em for your needs (if necessary).

The command "cat" wont read my serial port

I have my arduino connected to my mac sending the temperature through /dev/tty.usbserial-A5025XZE serial port. Every second i want my script to save what is coming through the serial port but when i try
cat /dev/tty.usbserial-A5025XZE >log.txt
It won't write anything to the file and it seems like the command dosn't run, I have to use control - z to cancel it.
The command is blocked while it waits for data from the serial port. Your Arduino isn't sending anything.

Generate port number at runtime in Linux [duplicate]

I need to create a program that will communicate with other programs on the same computer via UDP sockets. It will read commands from stdin, and some of this commands will make it to send/receive packets without halting execution. I've read some information out there, but since I'm not familiar with socket programming and need to get this done quickly, I have the following questions:
I need to get a random unused port for the program to listen to, and reserve it so other programs can communicate with this and also the port isn't reserved by another program. I also need to store the port number on a variable for future usage.
Since the communication is across processes on the same machine, I'm wondering if I can use PF_LOCAL.
Also a code sample of the setup of such socket would be welcome, as well as an example of sending/receiving character strings.
Call bind() specifying port 0. That will allow the OS to pick an unused port. You can then use getsockname() to retreive the chosen port.
Answer by Remy Lebeau is good if you need a temporary port. It is not so good if you need a persistent reserved port because other software also uses the same method to get a port (including OS TCP stack that needs a new temporary port for each connection).
So the following might happen:
You call bind with 0 and getsockname() to get a port;
then save it into config (or into several configs) for future uses;
software that needs this port starts and binds the port.
Then you need to e.g. restart the software:
software stops and unbinds the port: the port can now be returned by bind(0) and getsockname() again;
e.g. TCP stack needs a port and binds your port;
software can't start because port is already bound.
So for "future uses" you need a port that is not in ephemeral port range (that's the range from which bind(host, 0) returns a port).
My solution for this issue is the port-for command-line utility.
If it being a random port is actually important, you should do something like:
srand(time(NULL));
rand() % NUM_PORTS; // NUM_PORTS isn't a system #define
Then specify that port in bind. If it fails, pick a new one (no need to re-seed the random generator. If the random port isn't important, look at Remy Lebeau's answer.

How to open busy serial port (share port) with pyserial

Is it possible open serial port which already in use?
and send data to it
Or create new one and share it for few applications?
I'm trying to avoid use of port pairs (created by windows driver)
I guess you could use an intemediate program like Hyperterminal and sendkeys to it from other apps, ao that Hyperterminal sends them from multiple sources.
Other than a workaround like that I've never heard of a port beng open in more than one program simultaneously.

How to detect serial port sniffer?

Is it possible to detect if some serial port is being sniffed by another process running on Windows?
We have an application that receives some sensitive data that cannot be read by other people. So, before opening the serial port, we need to check if the port is being monitored.
We can use the CreateFile Windows API function to open the serial port with exclusive access rights, but if the monitor runs before our call, it can read all the communication (it opens the serial port with shared access rights, so we can open the port at the same time). To avoid this, the attempt is to check if the port is being monitored and raise an exception, warning the user. Is it possible to do this?
Port sniffing requires a filter driver, like SysInternals' PortMon utility. You are taking the wrong kind of approach to secure your application. When somebody can install a filter driver, the attacker has more than enough privileges to completely disable your app and replace it with something else of his own making. Trying to detect and prevent information loss through your app is pointless, the system itself has to be secured. A serial port is probably the first thing you'll have to lose, it is trivial to tap its wires.
Rather than lose the serial port why not encrypt your data. This assumes that both the DTE and DCE are programmable.

Resources