IPC pipe through files /dev/ttyS0 and /dev/ttyS1 - c

I want to perform IPC pipe through serial port dev files. here are the requirements
first of all I am trying to use
sudo socat /dev/ttyS0,raw,echo=0,crnl /dev/ttyS1,raw,echo=0,crnl
it is giving an error as follows
2014/12/xx 10:33:19 socat[17848] E tcgetattr(4, 0x7fffe76ecaa0): Input/output error
Once the ttyS0 and ttyS1 are connected, I assume we can perform read/write operation as similar as using a pipe(), I will have two programs peer0.c and peer1.c, peer0.c opens /dev/ttyS0 and peer1.c opens /dev/ttyS1,
so that read write operation should be as follows
peer0=>ttyS0--->---ttyS1=>peer1
peer0<=ttyS0---<---ttyS1<=peer1
and since /dev/ttyS(0/1) are system wide, i can run peer0.c program in one terminal and peer1.c in another,
Basically, I have linux based embedded application program which when ported to target hardware it will be controlled by linux based PC via minicom UART interface. target opens its ttyS0 to read uart data sent from PC, in PC, commands will be sent to target via minicom.
Now I want to run target application in same PC in one terminal and want to send command from another terminal/minicom. Also the communication should be bidirectional
is it possible to achieve this goal?
or is there any other similar way to achieve the same?
thanks in advance

If you want to just transfer files, use a protocol for doing so over serial links, such as ZMODEM (http://en.wikipedia.org/wiki/ZMODEM), if you want full IPC, establish a PPP connection over the link (http://en.wikipedia.org/wiki/Point-to-Point_Protocol)

peer0=>ttyS0--->---ttyS1=>peer1
peer0<=ttyS0---<---ttyS1<=peer1
You seem to want socat to take the part of the --->--- and ---<--- above. This is not possible, because socat had to open ttyS0 and ttyS1 and compete with peer0 for input from ttyS0 as well as with peer1 for input from ttyS1.
To achieve the goal of communicating with an application on one serial port via another port, just connect the two ports with a null modem cable.

Related

File access between mcu and PC through rs232 communication

I'm using a LPC178 development board and I want to read a file present on a Windows PC. My dev board only has a RS2323 interface to communicate with.
How can I go about transferring the file from my PC to my MCU using a RS232 (serial) link? I found a reference which explains how to transfer data between a MCU and PC but it isn't about file transfers.
Afaik there is no easy solution for this like calling something like "copy" or "fopen" over RS232. I would be happy to be proven wrong here.
The fastest solution might be to write a little programm running on your Windows Host, which listens to your RS232 communication and pipes your communication into/out of the file based on your communication protocol. This can be done with standard file operations in the language of your choice, for example C, C++ or Python.
Your problem is one of the oldest in the book. How do you transfer files without fancy operating system abstractions. For RS232 (or any other serial method) there exists many file transfer protocols.
One of them is kermit. This is a protocol from 1981 and can transfer binary and text files. It is able to be embedded in a micrcontroller and there exists programs to transfer/receive using kermit.
alternative old site for reference
In the simplest case you would use a file transfer protocol such as XMODEM, YMODEM, ZMODEM or Kermit - these protocols were designed in the days before networking and the Internet were ubiquitous and deal with simple point-to-point transfers between two computers. They are supported bu most terminal emulator tools such as TeraTerm Pro or PuTTY so no specific PC software need be written, just the microcontroller end.
A more complex but flexible solution is to implement a TCP/IP stack and a PPP driver, and an FTP application layer - probabaly only practical if using a third-party TCP/IP stack and application layer. You can then use any FTP client for the PC end, so again no PC software required. While this may be overkill if all you need to do is transfer files, it has the advantage of allowing you to use the the single serial port concurrently for other data streams and application protocols such as Telnet. The disadvantage perhaps is that while Windows does support PPP it is buried within the dial-up networking and to be frank a pain to get working.
Very first step you have to do is ensure serial communication is working fine.
Send a byte continuously from mcu to PC and display it on some io console (for example: HyperTerminal, Dock light )
Receive a byte to mcu from PC and echo it back to PC.
Once you are sure that serial communication is working fine then select some file transfer protocol and implement it.
While you can select any of the available protocols or write your own protocol and implement it.
For purpose of discussion i select Xmodem protocol.
If you consider some other protocol you may stop reading answer here.
XMODEM is a simple file transfer protocol.
Refer http://web.mit.edu/6.115/www/amulet/xmodem.htm for detailed information.
You may implement Xmodem mcu side by reading protocol. Or may consider using open source also ( if available )
PC side i prefer to use HyperTerminal io console as it is compatible with Xmodem.
In HyperTerminal all that i have to do is configure settings and select file for transfer to mcu.
Now transfer any file to mcu using Xmodem protocol from PC.
What you do with received file in mcu is up to you : )

Linux serial port (tty) redirection

I have a question linked to Linux and serial port.
I want to be able to receive and send messages to a dedicated serial port and to redirect it to another port (/dev/tty).
For the first part, I’m able to dialog with my hardware equipment without any problem, but I’m just wondering if it’s possible to intercept and redirect message coming from a serial port #1 to another port #2.
To give more context, I had used a GPS Antenna and NTP open source software for years.
Since 2018, the new GPS antenna protocol has modified the order of bytes in the message used by NTP to steer and now it’s not working anymore.
So my idea is to put a simple C program (middleware) which fixes this byte ordering; but I’m wondering if I have to build a kernel-specific module or if it can be done in another way. The NTP software uses the symbolic link to dialog.
Thanks for your help.
You can probably use a simple redirect, look here:
Pipe One Serial Port to Another in Linux
If the ports are in different rates you can use stty or perhaps screen to adjust: https://unix.stackexchange.com/a/117064
If you need it to be in c program to manipulate it you can use the following: https://stackoverflow.com/a/6947758/8901188
Using c it will need to run in an infinite loop so it can constantly read, manipulate and write the data.

Termios.h serial communication. Check for device disconnection

I'm using termios.h to communicate with a USB Dongle. I would like to close the file descriptor when I disconnect the dongle. Is there a way to do this. I know that for f.e. a TCP/IP connection you get a EOF if the other party closes the connection. However, with a serial connection, my program basically runs wild (I'm using a loop to read from the descriptor...).
Basically I would like to have some indication that the connection was closed so that i can gracefully shut down the program. Maybe by changing the settings with ioctl?
I Googled a bit using the search phrase how to receive usb disconnect notification in C for windows and came across Detecting USB Insertion / Removal Events in Windows using C++ on stackoverflow.com. If you run on Linux, you can adapt the seach phrase and try those answers.

How to send data over internet through console

I have to develop an application wherein I would receive data from parallel port and send it over to internet. This application is to be developed for embedded device running linux. Please suggest me how I can do that.
Regards
Sounds like a job for netcat. You can just open the device file and bind it straight to a TCP port: cat /dev/whatever | nc -l 2345 reads from a device and writes the results to a socket in case a client connects to port 2345.
If you need security, consider using a SSH tunnel.
Best solution - socat.
It can read from file and send to any socket (tcp, udp, unix, ipv4, ipv6), redirect program output, stdout. Reverse operations also posible.
Local example: read file "test", and send it content to localhost:9999
socat OPEN:test TCP:localhost:9999
If you want monitor file content and make it read only
socat OPEN:test,rdonly,ignoreeof TCP:localhost:9999
in socat you not need bash, in cat|nc some form of shell required.
I recommend sockets using C.
I would suggest either SSH or Telnet.
I would suggest using one of Perl, Python, or Ruby to do it if it has some processing to do.
Otherwise, if it is to use any console command, you can use curl or wget.
If you want to do it in C, perhaps because your embedded Linux doesn't have any of the shell tools and languages that other people have suggested, you need to look at the socket interface. The sequence of events is more or less:
create a socket using socket()
connect to a server using connect()
send your data using send(), or write() and deal with anything that comes back the other way using recv() or read().
close the socket using close().

Simulate serial port

I am writing a C program in Linux which will read/write to/from a serial port. I know the data that needs to be read and written on the port but I don't have a serial port to currently test this with.
Is there any way to simulate a serial port? Would reading/writing to a file be sufficient? I can have one process write to the file while another process reads that data and writes back other data to the file. Or are there others tools that can be used to simulate a port?
Thanks
Serial ports on Linux are terminal devices. A close simulation is to create a pseudo-terminal pair; the program that normally talks to the serial port is instead told to open the slave side of the pseudo-terminal, and the simulator writes and reads from the master side.
The pty(7) man page has more information.
Despite being an old topic, and my answer is not exactly something the OP was looking for, I decided to share my experience, as someone else might come across it like I did. Instead of straightforward simulation, I used the software called Serial to Ethernet Connector to gain access to the specific device I needed to test the app with. Worked nicely for me.
A character device, even something as simple as normal stdin and stdout should work if you don't care about attributes specific to port devices.

Resources