Write and then read to serial port in C - c

I have a USB to serial adapter attached to /dev/ttyUSB0 which connects to a rotation stage. I can communicate to that rotation stage by simply sending ASCII commands over the serial port. i.e. to request the current position of that rotation stage just:
in window 1: echo -e "1tp\r" > /dev/ttyUSB
in window 2: cat /dev/ttyUSB which returns the current position: "1TP-0.00000"
Now I want to be able to do exactly this at a certain trigger inside a C program.
I've browsed this amazing archive and found a few examples of how it can be done. For example
how to open, read, and write from serial port in C
Linux C Serial Port Reading/Writing
Serial port loopback/duplex test, in Bash or C? (process substitution)
So based on those, I wrote this very ugly code to simply try writing "1tp\r" to the serial port and then reading the returned value ( the rotation stage position) back. Here is the code below.
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int main(int argc, char *argv[])
{
char line[1024];
int chkin;
int chkout;
char input[1024];
char msg[1024];
char serport[24];
// argv[1] - serial port
sprintf(serport, "%s", argv[1]);
int file= open(serport, O_RDWR | O_NOCTTY | O_NDELAY);
printf("file descriptor: %d\n",file);
if (file == 0)
{
sprintf(msg, "open_port: Unable to open %s.\n", serport);
perror(msg);
}
else
fcntl(file, F_SETFL, FNDELAY); //fcntl(file, F_SETFL, 0);
while (1)
{
usleep (1000000);
// printf("enter input data:\n");
//scanf("%s",&input[0]);
// printf("You entered %s\n",&input);
//input= "1tp\r";
chkin = write(file,"1tp\r",5);
printf("chkin: %d\n",chkin);
if (chkin<0)
{
printf("cannot write to port\n");
}
fcntl(file, F_SETFL, FNDELAY);
//chkin=read(file,line,sizeof line);
usleep (100000);
//while ((chkin=read(file,line,sizeof line))>=0)
//printf("chkin: %d\n",chkin);
chkout = read(file, line ,sizeof line);
//{
if (chkout<0)
{
printf("cannot read from port\n");
}
else
{
printf("bytes: %d, line=%s\n",chkout, line);
}
//}
/*CODE TO EXIT THE LOOP GOES HERE*/
if (input[0] == 'q') break;
}
close(file);
return 0;
}
Well, it doesn't work. When I run it as: ./sertest /dev/ttyUSB0, I just get the error "cannot read from port". In reality, the problem seems to be that the "1tp\r" string does not seem to get written to the serial port. In addition, once I am done with this, the serial port settings seem to be all messed up because I can't communicate at all with the stage via simple terminal commands and need to reset the serial port using minicom.
I should also note that I know I can successfully read something from the serial port with the second part of that code because I have managed to send the "1tp\r" string with a terminal command and just read the output of the stage with the relevant snippet of code in the example below.
So, I would appreciate some input on how to successfully write this simple string "1tp\r" ( or a similar one) to the serial port and alternate to reading the serial port.
I am not a programmer so I apologize in advance for the poor style of the piece of code here.

Related

How to make C program communicate with Python program using Linux's virtual serial ports?

I would like to send data from a C program into a Python program that will visualize this data. Development environment is a Linux (Ubuntu 18.04LTS) computer. To be clearer, both of the programs are running on the same computer.
I am using termios for opening the serial port in C program, and pySerial in the Python side. As for the serial port, I am using "ttyS0". The problem is that, when I send "Hello" from C program to the Python program and print it on the terminal what I see is space character, basically I am gettin this " ".
My question is, can I use the "ttyS0" serial port (I guess that is a virtual port) for this purpose?
Here is the C code:
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
// Termios init functions are not posted because the configuration
// is correct and proved that they are working.
int main()
{
char *portname = "/dev/ttyS0";
int fd;
int wlen;
unsigned char writeBuffer[] = "Hello!";
fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
printf("Error opening %s: %s\n", portname, strerror(errno));
return -1;
}
/*baudrate 115200, 8 bits, no parity, 1 stop bit */
set_interface_attribs(fd, B115200);
do{
wlen = write(fd, writeBuffer, sizeof(writeBuffer));
printf("Sent data is: \"%s\"\n", writeBuffer);
delay(500);
} while(1);
}
The Python code:
import serial
from time import sleep
port = "/dev/ttyS0"
ser = serial.Serial(port, 115200, timeout=0.5)
while True:
data = ser.readline()
print(str(data.decode('utf-8')))
ser.close()
ttyS0 is your computer's serial port -- there's nothing "virtual" about it. Writing to this device will attempt to transmit data out of the computer using that port, and reading from the device will attempt to receive data from an external device connected to the port. There is no way for two programs on the same computer to usefully communicate using a serial port.
What I think you're looking for here is either a pipe, a socket pair, or a pty. Which one is most appropriate will depend on your specific requirements.

Linux : Creating a simple non-blocking server and client to communicate over named pipes

I am trying to create a simple example of named pipes (FIFO). Here, the server will listen for message from the client, which writes on the named pipe, common to both of them. The special thing to be implemented is that the FIFO should be non-blocking (usage of O_NONBLOCK).
By non blocking, I mean that the writer should return immediately after writing, if there is no reader. Similarly, reader should return immediately if there is no message(no writer).
I have created the blocking version though and its working fine. I am then trying to convert it to non-blocking.
Here's the client :
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#define FIFO "/tmp/myFIFO"
/*
This acts as the client, writing to the FIFO
*/
int main(int argc, char *argv[])
{
FILE *fp;
int fifo_fd;
if(argc != 2)
{
printf("Usage : ./fifo_client <message> \n");
exit(1);
}
fifo_fd = open(FIFO, O_WRONLY | O_NONBLOCK);
if(fifo_fd < 0)
{
perror("Error while open call");
exit(1);
}
fp = fdopen(fifo_fd, "w");
if(fp == NULL)
{
perror("Error while opening fd");
exit(1);
}
fputs(argv[1],fp);
/* Close the fp */
fclose(fp);
return 0;
}
Here's the server :
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#define FIFO "/tmp/myFIFO"
/*
This acts as a server waiting for strings to be written by the client, over the FIFO
*/
int main()
{
FILE *fp;
int fifo_fd;
char buf[1024];
/* Create a FIFO */
umask(0);
if(mkfifo(FIFO,0666) < 0) /* 0666 is read and write permission*/
{
perror("Error creating FIFO");
exit(1);
}
while(1) /*endless wait, keep reading strings and print*/
{
fifo_fd = open(FIFO, O_RDONLY | O_NONBLOCK);
if(fifo_fd < 0)
{
perror("Error while open call");
exit(1);
}
fp = fdopen(fifo_fd, "w");
if(fp == NULL)
{
perror("Error while opening fd");
exit(1);
}
if(!fgets(buf,1024,fp))
printf("Nothing to read\n");
else
printf("Message Recieved : %s\n", buf);
fclose(fp);
sleep(1);
}
return 0;
}
I run the server first.
Secondly, on second terminal, when I run the client, I get the error :
Error while open call: No such device or address
What am I missing? I did man, and the parameters seem to be correct.
EDIT
Moving the open and close calls out of the while loop, did the job. But now if the client is started without starting the server, throws the following error :
Error while open call: No such device or address
The file /tmp/myFIFO exists on file-system from previous execution of the server, must be used by the client.
The main problem is opening and closing the file in a loop. This makes no sense. There is a very short time interval between opening and closing, and your client must hit it. It has little chance to do so. The "No such device or address" message happens exactly because the client misses the instant when the file is open. This is the main problem. Try moving open, fopen and fclose out of the server loop.
You also opening for reading but fopening for writing, but I suppose it's just a typo. This combination will not run. You need to change the mode of fopen to "r".
There are other, smaller issues.
You are not checking errors in the client. In your program the client will fail to open most of thee time, but sometimes open will succeed and write will fail.
It makes little sense to use stdio for the pipe in this program. read and write would do just fine.
Last but not least, sleep is an indication of a design issue. Indeed, in this program blocking I/O would make more sense. It's OK to use sleep if you just want to experiment with non-blocking I/O, but in real programs it should be avoided.

C RbPi UART Remote Control Sockets

I want to remote control sockets in my room manual without an extra library on my raspberry pi. I want to use the UART interface in C. The socket has 433 Mhz receiver and I use a 433 Mhz transmitter. In other librarys you type something like this: send 11111 1 1. (socket code, socket number, condition). But how to format this command in C with the write() function? The 10 is for number of characters. I use this code below. I tested the output via minicom, that works fine. But how the receiver now knows that it was adressed?
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int main(int argc, char ** argv) {
int fd;
// Open the Port. We want read/write, no "controlling tty" status, and open i$
fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open /dev/ttyAMA0 - ");
return(-1);
}
// Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that
fcntl(fd, F_SETFL, 0);
// Write to the port
int n = write(fd,"11111 1 1",10);
if (n < 0) {
perror("Write failed - ");
return -1;
}
// Don't forget to clean up
close(fd);
return 0;
}
Do I get it right: you send a command via UART to transmit a message via radio trasmitter. Sniffing the UART output proves your code OK in the sense that TX line sends what you want it to send in the software, and the actual question is "how the rx module gets that message/how to make the rx module get it?"
If so, the main question is what kind of radio TX/RX or TRXs do you actually use (I mean chip/module codes, like CC1120, NRF2401 etc.)? There are plenty of 433MHz radiomodules available, I suppose consulting their datasheet first or at least posting the part number here is the right way to go.

init, read and write for linux serial device with C

I'm working on a new project where I want to make a connection with an FTDI which is connected to my debian machine. I am intending to write the code with C, not C++. Here lies my problem. All the examples I find are incomplete or are made for a c++ compiler in stead of the GCC compiler.
The goal is to talk to my microcontroller which is connected to the FTDI. For debugging i want to start building a linux application which is able to:
initialize a serial connection on startup with ttyUSB1
send a character string
display character strings when they are received by the pc
save the communication to a .txt file
Is there any example code or tutorial to make this?
If I succeed I will defenetly place the code here so that new viewers can use it to!
Edit:
Like I said I would post the code if I had it, and this is what worked for me:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#define MODEM "/dev/ttyUSB0"
#define BAUDRATE B115200
int main(int argc,char** argv)
{
struct termios tio;
struct termios stdio;
struct termios old_stdio;
int tty_fd, flags;
unsigned char c='D';
tcgetattr(STDOUT_FILENO,&old_stdio);
printf("Please start with %s /dev/ttyS1 (for example)\n",argv[0]);
memset(&stdio,0,sizeof(stdio));
stdio.c_iflag=0;
stdio.c_oflag=0;
stdio.c_cflag=0;
stdio.c_lflag=0;
stdio.c_cc[VMIN]=1;
stdio.c_cc[VTIME]=0;
tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // make the reads non-blocking
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
if((tty_fd = open(MODEM , O_RDWR | O_NONBLOCK)) == -1){
printf("Error while opening\n"); // Just if you want user interface error control
return -1;
}
cfsetospeed(&tio,BAUDRATE);
cfsetispeed(&tio,BAUDRATE); // baudrate is declarated above
tcsetattr(tty_fd,TCSANOW,&tio);
while (c!='q'){
if (read(tty_fd,&c,1)>0){
write(STDOUT_FILENO,&c,1); // if new data is available on the serial port, print it out
printf("\n");
}
if (read(STDIN_FILENO,&c,1)>0){
write(tty_fd,&c,1);//if new data is available on the console, send it to serial port
printf("\n");
}
}
close(tty_fd);
tcsetattr(STDOUT_FILENO,TCSANOW,&old_stdio);
return EXIT_SUCCESS;
}
Most of the code came from http://en.wikibooks.org/wiki/Serial_Programming/Serial_Linux but i also used a bit from the code posted below.
Handling with serial ports ( for linux OS ) :
- To open communication, you will need a descriptor which will be the handle for your serial port.
- Set the flags to control how the comunication will be.
- Write the command to this Handle ( make sure you're formatting the input correctly ).
- Get the answer. (make sure you're to read the amount of information you want )
- Close the handle.
It will seem like this:
int fd; // file descriptor
int flags; // communication flags
int rsl_len; // result size
char message[128]; // message to send, you can even dinamically alocate.
char result[128]; // result to read, same from above, thanks to #Lu
flags = O_RDWR | O_NOCTTY; // Read and write, and make the job control for portability
if ((fd = open("/dev/ttyUSB1", flags)) == -1 ) {
printf("Error while opening\n"); // Just if you want user interface error control
return -1;
}
// In this point your communication is already estabilished, lets send out something
strcpy(message, "Hello");
if (rsl_len = write(fd, message, strlen(message)) < 0 ) {
printf("Error while sending message\n"); // Again just in case
return -2;
}
if (rsl_len = read(fd, &result, sizeof(result)) < 0 ) {
printf("Error while reading return\n");
return -3;
}
close(fd);
Note that you have to care about what to write and what to read.
Some more flags can be used in case of parity control, stop bits, baud rate and more.
Since gcc is a C/C++ compiler, you don't need to limit yourself to pure C.
Sticking to pure C is OK if you enjoy writing lots of boilerplate code, and if you really know what you're doing. Many people use Unix APIs incorrectly, and a lot of example code out there is much too simplistic. Writing correct Unix C code is somewhat annoying, to say the least.
Personally, I'd suggest using not only C++, but also a higher-level application development framework like Qt. Qt 5 comes bundled with a QtSerialPort module that makes it easy to enumerate the serial ports, configure them, and get data into and out of them. Qt does not force you to use the gui modules, it can be a command line application, or a non-interactive server/daemon.
QtSerialPort is also usable from Qt 4, but it doesn't come bundled with Qt 4, you have to add it to your project. I suggest starting out with Qt 5, it's nicer to use with its C++11 support.
The code using Qt can be pretty simple, not much longer than your plain-English description. The below is a Qt console application using Qt 5 and C++11. It uses the core and serialport modules. It also handles the SIGINT signal so that the output file gets flushed before the process would terminate due to a ^C. I'm using QLocalSocket in place of raw Unix API to communicate from the Unix signal handler, the functionality is the same.
Only the code within main is strictly required, the rest is just to make it properly wrap things up when you hit ^C.
#include <QCoreApplication>
#include <QSerialPort>
#include <QFile>
#include <QTextStream>
#include <QLocalServer>
#include <QLocalSocket>
#include <cstdio>
#include <csignal>
QLocalSocket * xmit;
static void signalHandler(int)
{
xmit->write(" ");
xmit->flush();
}
static bool setupSignalHandler()
{
QLocalServer srv;
srv.listen("foobarbaz");
xmit = new QLocalSocket(qApp);
xmit->connectToServer(srv.serverName(), QIODevice::WriteOnly);
srv.waitForNewConnection();
QLocalSocket * receive = srv.nextPendingConnection();
receive->setParent(qApp);
qApp->connect(receive, &QLocalSocket::readyRead, &QCoreApplication::quit);
struct sigaction sig;
sig.sa_handler = signalHandler;
sigemptyset(&sig.sa_mask);
sig.sa_flags = SA_RESTART;
return ! sigaction(SIGINT, &sig, NULL);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
setupSignalHandler();
QSerialPort port("ttyUSB1");
QFile file("file.txt");
QTextStream err(stderr, QIODevice::WriteOnly);
QTextStream out(stdout, QIODevice::WriteOnly);
if (!file.open(QIODevice::WriteOnly)) {
err << "Couldn't open the output file" << endl;
return 1;
}
if (!port.open(QIODevice::ReadWrite)) {
err << "Couldn't open the port" << endl;
return 2;
}
port.setBaudRate(9600);
QObject::connect(&port, &QSerialPort::readyRead, [&](){
QByteArray data = port.readAll();
out << data;
file.write(data);
});
out << "Use ^C to quit" << endl;
return a.exec();
}

How do I read data from serial port in Linux using C?

I am new to serial programming in Linux using C. I have found a small piece of code to write data on serial port which I am sharing here. After running this code I may assume that data has written on a specific port. Now I would like to open another terminal and using separate code want to read the data written on that specific port - how do I do that?
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
int
open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
/* Could not open the port. */
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
n = write(fd, "ATZ\r", 4);
if (n < 0)
fputs("write() of 4 bytes failed!\n", stderr);
return (fd);
}
The code above will write the data on a specific port.
In theory, all you have to do is open the relevant port for reading, and use read() to get the data.
int
read_port(void)
{
int fd = open("/dev/ttyS0", O_RDONLY | O_NOCTTY);
if (fd == -1)
{
/* Could not open the port. */
perror("open_port: Unable to open /dev/ttyS0 - ");
}
char buffer[32];
int n = read(fd, buffer, sizeof(buffer));
if (n < 0)
fputs("read failed!\n", stderr);
return (fd);
}
There are differences; notably, the read needs a buffer to put the data in. The code shown discards the first message read. Note that a short read simply indicates that there was less data available than requested at the time when the read completed. It does not automatically indicate an error. Think of a command line; some commands might be one or two characters (ls) where others might be quite complex (find /some/where -name '*.pdf' -mtime -3 -print). The fact that the same buffer is used to read both isn't a problem; one read gives 3 characters (newline is included), the other 47 or so.
The program posted makes a lot of assumptions about the state of the port. In a real world application you should do all the important setup explicitly. I think the best source for learning serial port programming under POSIX is the
Serial Programming Guide for POSIX Operating Systems
I'm mirroring it here: https://www.cmrr.umn.edu/~strupp/serial.html

Resources