Open and configure Serial port - c

I wrote the following code in which I open and configure my serial port Device
int open_port(void)
{
int fd; // file description for the serial port
fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1) // if open is unsucessful
{
//perror("open_port: Unable to open /dev/ttyAMA0 - ");
printf("open_port: Unable to open /dev/ttyAMA0. \n");
}
else
{
fcntl(fd, F_SETFL, 0);
printf("port is open.\n");
}
return(fd);
} //open_port
And configure port
int configure_port(int fd) // configure the port
{
struct termios port_settings; // structure to store the port settings in
cfsetispeed(&port_settings, B9600); // set baud rates
cfsetospeed(&port_settings, B9600);
port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
return(fd);
} //configure_port
My question (maybe is easy) is what I have to change exactly in these 2 functions to have
FILE *fd;
fd=fopen("/dev/ttyUSB0","r");
instead of fd= open(...) etc?

Can you not just convert the file descriptor (fd) to a file pointer (fp) with the next call after your initialization?
FILE* fp = fdopen(fd,"w")

Related

read() is blocked forever on raspberry pi 3 UART

The C read() function frequently get blocked, especially if there is nothing connected on the Gpio pins (tx / rx), but I just hoped it would stop for itself when there is no conection, the same when there is wire conection but no data to read, but it just get blocked until I force it to finish.
Open
fd = open("/dev/serial0", O_RDWR | O_NDELAY | O_NOCTTY | O_NONBLOCK);
Read
n = read( fd, value, 1 );
if (n < 0) {
printf ( "Error = %s\n", strerror( errno ) );
}
else if (n == 0) {
printf ( "Read Nothing...\n");
}
Set attribs
int setAttr(int fd)
{
//Read the configureation of the port
struct termios options;
tcgetattr( fd, &options );
//Set Baud Rate
cfsetispeed( &options, B9600 );
cfsetospeed( &options, B9600 );
//Setting other Port Stuff
options.c_cflag &= ~PARENB; /*Make 8n1 */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag |= CS8; /* Select 8 data bits */
options.c_cflag &= ~CRTSCTS; /* No flow control */
options.c_cc[VMIN] = 0; /*READ doesn't block */
options.c_cc[VTIME] = 1; /* 0.1 seconds read timout */
options.c_cflag |= CREAD | CLOCAL; /* Turn on READ & ignore crtl lines */
//Make raw
cfmakeraw(&options);
//Flush port, then applies attributes
tcflush(fd, TCIOFLUSH);
return tcsetattr( fd, TCSANOW, &options );
}
For a UART port with nothing but tx/rx pins, there is no distinct "nothing connected" status. For the functionality you want, the port would need DCE/DTR pins and the CLOCAL flag (ignore modem control lines) would have to be removed from the termios settings.
I just add the code below, after open() and it's not blocked anymore when tx/rx pins are not connected. Probably the port was going blocked after open().
if (fd == -1)
{
/*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/serial0 - ");
}
else fcntl(fd, F_SETFL, O_NONBLOCK);

serial communication through rs232 in linux c

I am new in Linux. I am facing problem in serial communications.
I have two computers, one is running a hyper terminal and on another I am writing a C program to communicate with hyper terminal. Both PC are connected through RS232.
I have written some code and I am able to send data to hyper terminal, but I am not sure how I can read data from hyper terminal with a program written in C on a Linux PC.
Any suggestions?
Thanks in advance.
Below is my code:
int main(int argc, char **argv)
{
struct termios options;
int fd;
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1)
{
printf("Could not open port /dev/ttyS0\n");
return 1;
}
tcgetattr(fd, &options); //get port options
cfsetispeed(&options, B9600); //set input baud rate
cfsetospeed(&options, B9600); //set output baud rate
options.c_cflag |= (CLOCAL | CREAD); //enable receiver and set local mode
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS; //disable hardware flow control
options.c_cflag &= ~(ICANON | ECHO | ISIG); //set raw input
options.c_cflag |= IXOFF; //disable software flow control
tcsetattr(fd, TCSANOW, &options); //set new port options
sleep(1);
int rc,count;
int size = 8;
unsigned char buf[10];
FILE *fp = NULL;
char ch;
int i=0;
printf("enter the data you want to send");
while((ch=getchar())!='\n')
{
write(fd,&ch,1);
}
close(fd);
printf("Finished.\n");
return 0;
}

Reading and writing binary data over serial port

So I searched around, and couldn't exactly find what I needed. I need help reading and writing binary data over a serial port, and would appreciate any advice you may have. Please note, I asked a question similar to this earlier when I was at a different stage of this project.
Below are programs. The first program opens a file "test.jpg", reads it in binary mode and stores the result in a buffer. It then closes the file, and is supposed to send that file over a serial port.
The second program creates a file called "testout.jpg", and is supposed to read in the data sent from the previous program.
I have a hunch that the problem in my code lies in the second program. Perhaps I need to use fread for that too? I tried, but I cannot figure out how to implement it for a serial port as I am relatively new to programming.
Many thanks for your time.
Serial write:
#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 */
#include <stdlib.h>
int main()
{
//writing
int writeport = open_port("/dev/ttyUSB0");
//open file
FILE *file;
char *buffer;
int fileLen;
file = fopen("test.jpg", "rb");
//get file size
fseek(file, 0, SEEK_END);
fileLen = ftell(file);
fseek(file, 0, SEEK_SET);
buffer = (char *)malloc(fileLen + 1);
//read file contents
fread(buffer, fileLen, 1, file);
fclose(file);
int n = write(writeport, buffer, fileLen + 1);
if (n < 0)
fputs("write() of bytes failed!\n", stderr);
//closing ports
close(writeport);
}
int open_port(char str[])
{
int fd = open(str, O_RDWR | O_NOCTTY | O_NONBLOCK); // ?? NDELAY or NONBLOCK?
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
struct termios options;
tcgetattr(fd, &options); //this gets the current options set for the port
// setting the options
cfsetispeed(&options, B9600); //input baudrate
cfsetospeed(&options, B9600); // output baudrate
options.c_cflag |= (CLOCAL | CREAD); // ?? enable receicer and set local mode
//options.c_cflag &= ~CSIZE; /* mask the character size bits */
options.c_cflag |= CS8; /* select 8 data bits */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // choosing raw input
options.c_iflag &= ~INPCK; // disable parity check
options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable software flow control
options.c_oflag |= OPOST; // ?? choosing processed output
options.c_cc[VMIN] = 0; // Wait until x bytes read (blocks!)
options.c_cc[VTIME] = 0; // Wait x * 0.1s for input (unblocks!)
// settings for no parity bit
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &options); //set the new options ... TCSANOW specifies all option changes to occur immediately
return (fd);
}
Serial read:
#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 */
int main()
{
//reading
int readport = open_port("/dev/ttyUSB1");
//open resultant file
FILE *file;
//system("rm testout.jpg");
file = fopen("testout.jpg", "wb");
//trying to read one character at a time
char buff;
int n = 1;
while (n > 0)
{
n = read(readport, &buff, 1);
//printf("%c", buff, buff);
**//I tried these three methods, with little success**
//fprintf(file, "%c", buff);
//fwrite(&buff, 1, 1, file);
//write(file, &buff, 1);
}
//closing ports
close(readport);
fclose(file);
}
int open_port(char str[])
{
int fd = open(str, O_RDWR | O_NOCTTY | O_NONBLOCK); // ?? NDELAY or NONBLOCK?
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
struct termios options;
tcgetattr(fd, &options); //this gets the current options set for the port
// setting the options
cfsetispeed(&options, B9600); //input baudrate
cfsetospeed(&options, B9600); // output baudrate
options.c_cflag |= (CLOCAL | CREAD); // ?? enable receicer and set local mode
//options.c_cflag &= ~CSIZE; /* mask the character size bits */
options.c_cflag |= CS8; /* select 8 data bits */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // choosing raw input
options.c_iflag &= ~INPCK; // disable parity check
options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable software flow control
options.c_oflag |= OPOST; // ?? choosing processed output
options.c_cc[VMIN] = 0; // Wait until x bytes read (blocks!)
options.c_cc[VTIME] = 0; // Wait x * 0.1s for input (unblocks!)
// settings for no parity bit
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &options); //set the new options ... TCSANOW specifies all option changes to occur immediately
return (fd);
}
file = fopen( "zname.jpg", "wb" );
while (1) {
n = read(readport, &buff, 1);
if (n == -1) switch(errno) {
case EAGAIN: /* sleep() */
continue;
...
default: goto quit;
}
if (n ==0) break;
fputc(buff, file);
}
quit:
fclose (file);
...
Even better than sleep() and loop, would be to use select/poll. (You'd still have to check for EAGAIN)

Clear data at serial port in Linux in C?

I am testing the sending and receiving programs with the code as
The main() function is below:
#include "lib.h"
int fd;
int initport(int fd) {
struct termios options;
// Get the current options for the port...
tcgetattr(fd, &options);
// Set the baud rates to 19200...
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// Set the new options for the port...
tcsetattr(fd, TCSANOW, &options);
return 1;
}
int main(int argc, char **argv) {
fd = open("/dev/pts/2", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open /dev/pts/1 - ");
return 1;
} else {
fcntl(fd, F_SETFL, 0);
}
printf("baud=%d\n", getbaud(fd));
initport(fd);
printf("baud=%d\n", getbaud(fd));
char sCmd[254];
sCmd[0] = 0x41;
sCmd[1] = 0x42;
sCmd[2] = 0x43;
sCmd[3] = 0x00;
if (!writeport(fd, sCmd)) {
printf("write failed\n");
close(fd);
return 1;
}
printf("written:%s\n", sCmd);
usleep(500000);
char sResult[254];
fcntl(fd, F_SETFL, FNDELAY);
if (!readport(fd,sResult)) {
printf("read failed\n");
close(fd);
return 1;
}
printf("readport=%s\n", sResult);
close(fd);
return 0;
}
The lib.h contains read and write code as at:
Parse and read data frame in C?
and got the issue:
In order to test with serial port, I used the socat (https://help.ubuntu.com/community/VirtualSerialPort ) to create a pair serial ports on Linux and test my program with these port.
The first time the program sends the data and the program receives data is ok. However, if I read again or even re-write the new data into the serial port, the return data is always null until I stop the virtual serial port and start it again, then the write and read data is ok, but still, only one time.
(In the real case, the sending part will be done by another device, I am just taking care of the reading data from the serial port. I wrote both parts just to test my reading code.)
Does anyone have any ideas?
Either your comment or your code is wrong:
// Set the baud rates to 19200...
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
This says it will be setting the baud rate to 19200, but it really sets it to 9600. Maybe you want this:
// Set the baud rates to 19200...
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);

Serial Device: Reading 8N1 works, but writing a single byte fails

In my program I read from the serial device (Linux, 8N1) without any problem. But in the case I want to write out a single byte, I get nothing out on the interface. I assume that my serial output settings are wrong. But there aren't that many ways how to set c_oflag...
My code:
#define TTYDEVICE "/dev/ttyS0"
#define BAUDRATE B9600
int openSerialDevice(const char* devTTY, struct termios oldTio) {
//----< Open serial device >----------------------------------
int fileDescriptor;
// fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY);
//fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY /*| OPOST*/);
if (fileDescriptor == -1) {
perror("Error while opening serial interface occurred!");
return -99;
}
// set new parameters to the serial device
struct termios newtio;
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
// set to 8N1
newtio.c_cflag &= ~PARENB;
newtio.c_cflag &= ~CSTOPB;
newtio.c_cflag &= ~CSIZE;
newtio.c_cflag |= CS8;
newtio.c_iflag = IGNPAR;
// output mode to
//newtio.c_oflag = 0;
newtio.c_oflag |= OPOST;
/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 10; /* inter-character timer 1 sec */
newtio.c_cc[VMIN] = 0; /* blocking read disabled */
tcflush(fileDescriptor, TCIFLUSH);
if (tcsetattr(fileDescriptor, TCSANOW, &newtio)) {
perror("could not set the serial settings!");
return -99;
}
//----< / Open serial device >----------------------------------
return fileDescriptor;
}
int ACK[1] = { 6 };
int main() {
// old termios to restablish
struct termios oldTIO;
// filedescriptor
int fd;
fd = openSerialDevice(TTYDEVICE, oldTIO);
if ((fd == -1) | (fd == -99)) {
perror("Could not open TTY-Device. Exit on failure!");
return EXIT_FAILURE;
}
write(fd, ACK, 1); // Problem !!
return 0:
}
Now, if I use
screen /dev/ttyS1 9600 8n1
to verify what's coming out on /dev/ttyS0. I can't see anything. Same if I sniff with Docklight 1.8.
Any suggestions? thanks
How do you verify nothing is coming out ?
You can try to drop the RTSCTS, and try again. Infact, if you want minimal interference from the tty layer, you should set your terminal to raw, using this :
cfmakeraw(&newtio);
You're giving write() the data argument of ACK, which is a pointer to int. This is probably not what you mean. Depending on the endianness of the computer you're on, this means write() will "see" a buffer containing the chars { 6, 0, 0, 0 } (little-endian) or { 0, 0, 0, 6 } (big-endian). This assumes that sizeof (int) == 4 is true, adjust for other sizes as needed, the problem remains.
You should very probably make the buffer unsigned char instead. Also, if you had made the call like this:
int wrote = write(fd, ACK, sizeof ACK);
printf("Wrote %d bytes\n", wrote);
You would have gotten direct feedback. You should test something like this, to see that the write actually succeeds.
The activated Hardware-Flow-Control (CRTSCTS) was the reason why write() blocked and finally nothing has appeared on the serial output.
thanks!
Code snap which works:
int openSerialDevice(const char* devTTY, struct termios oldTio) {
//----< Open serial device >----------------------------------
int fileDescriptor;
// fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY);
//fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY /*| OPOST*/);
if (fileDescriptor == -1) {
perror("Error while opening serial interface occurred!");
return -99;
}
// set new parameters to the serial device
struct termios newtio;
fcntl(fileDescriptor, F_SETFL, 0);
// set everything to 0
bzero(&newtio, sizeof(newtio));
// again set everything to 0
bzero(&newtio, sizeof(newtio));
newtio.c_cflag |= BAUDRATE; // Set Baudrate first time
newtio.c_cflag |= CLOCAL; // Local line - do not change "owner" of port
newtio.c_cflag |= CREAD; // Enable receiver
newtio.c_cflag &= ~ECHO; // Disable echoing of input characters
newtio.c_cflag &= ~ECHOE;
// set to 8N1
newtio.c_cflag &= ~PARENB; // no parentybyte
newtio.c_cflag &= ~CSTOPB; // 1 stop bit
newtio.c_cflag &= ~CSIZE; // Mask the character size bits
newtio.c_cflag |= CS8; // 8 data bits
// output mode to
newtio.c_oflag = 0;
//newtio.c_oflag |= OPOST;
// Set teh baudrate for sure
cfsetispeed(&newtio, BAUDRATE);
cfsetospeed(&newtio, BAUDRATE);
newtio.c_cc[VTIME] = 10; /* inter-character timer */
newtio.c_cc[VMIN] = 0; /* blocking read until */
tcflush(fileDescriptor, TCIFLUSH); // flush pending data
// set the new defined settings
if (tcsetattr(fileDescriptor, TCSANOW, &newtio)) {
perror("could not set the serial settings!");
return -99;
}
//----< / Open serial device >----------------------------------
return fileDescriptor;
}

Resources