serial port not responding - c

I wrote a program that communicate with serial port, using termios, this program will read serial port in non-blocking mode and write response to serial port once it read data. If there is no data read from serial port, the program will do other thing, on next loop, the program read serial port again.
now the question is, after sometimes gone, maybe several minutes, or maybe several hours, the serial port don't respond to my program any more. Even I execute echo 'HB\n' > /dev/ttyUSB0(then the serial port should respond 'HACK'), it doesn't respond any more..
I even don't known when the serial port is 'dead', I don't have any clue.. it 'dead' untimed.
here is my configuration:
/// set local mode options
//tminfo.c_lflag |= ICANON;
tminfo.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
/// set control mode options
tminfo.c_cflag |= (CLOCAL | CREAD);
tminfo.c_cflag |= HUPCL;
// set hardware flow control
tminfo.c_cflag &= ~CRTSCTS;
// set how many bits in a character
tminfo.c_cflag &= ~CSIZE;
tminfo.c_cflag |= CS8;
// set parity mode (default to odd validation), this option (PARENB) will both enable input and output parity checking
tminfo.c_cflag &= ~PARENB; // we don't need prity checking now
/// set input mode options
// set input parity checking
tminfo.c_iflag &= ~INPCK;
tminfo.c_cflag &= ~CSTOPB;
/// set output mode options
tminfo.c_oflag &= ~OPOST;
tminfo.c_cc[VMIN] = 1;
tminfo.c_cc[VTIME] = 1;
/// set line speed, defaults to 38400bps, both for input and output
// this call will set both input and output speed
cfsetspeed(&tminfo, B38400);
It's hard to debug the serial in this situation. I really can't figure out what cause the serial port 'dead' on earth. I'm nearly crazy...
what the possible reason? Any help will be appreciated!
update:
I have wrote some code to the other end of the serial port, when it receive 'HB', it will respond 'HACK'

Related

Can't communicate with certain baud rates

My application is able to communicate with baud rates like 4800, 9600 and 115200 but can't with 14400 or 38400. I have to include asm/termios because I need struct termios2 since I'm going to use c_ispeed and c_ospeed members for any buad rate.
Also the second problem I encounter is that read function doesn't return afterVTIME. Do you know why this happens? Any help is appreciated. Thanks.
#include <asm/termios.h>
int serialDevice = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_SYNC);
serialSettings.baudRate = 4800;
serialSettings.dataBits = 8;
serialSettings.hardwareFlowControl = 0;
serialSettings.parity = 0;
serialSettings.parityOdd = 0;
serialSettings.stopBits = 1;
serialSettings.xonxoff = 1;
setSerialSettings(serialDevice, &serialSettings);
//-------------------------------------------------------
int8_t setSerialSettings(int serialDevice, Serial_Params_t *settings)
{
struct termios2 tty;
memset(&tty, 0, sizeof tty);
// get current serial settings
if (ioctl(serialDevice, TCGETS2, &tty) == -1)
{
sendLog("Can't get serial attributes | setSerialSettings", LOG_TYPE_ERROR);
return FALSE;
}
// baudrate
tty.c_cflag &= ~CBAUD;
tty.c_cflag |= BOTHER;
tty.c_ispeed = MAX(110, MIN(settings->baudRate, MAX_BAUDRATE));
tty.c_ospeed = MAX(110, MIN(settings->baudRate, MAX_BAUDRATE));
// enable input parity check
tty.c_iflag |= INPCK;
// data bits: CS5, CS6, CS7, CS8
tty.c_cflag &= ~CSIZE;
switch (settings->dataBits)
{
case 5:
tty.c_cflag |= CS5;
break;
case 6:
tty.c_cflag |= CS6;
break;
case 7:
tty.c_cflag |= CS7;
break;
case 8:
default:
tty.c_cflag |= CS8;
break;
}
// stop bit
switch (settings->stopBits)
{
case 1:
default:
tty.c_cflag &= ~CSTOPB;
break;
case 2:
tty.c_cflag |= CSTOPB;
}
// parity
if (settings->parity == 1)
tty.c_cflag |= PARENB;
else
tty.c_cflag &= ~PARENB;
// odd/even parity
if (settings->parityOdd == 1)
tty.c_cflag |= PARODD;
else
tty.c_cflag &= ~PARODD;
// flow control
// XON/XOFF
if (settings->xonxoff == 1)
tty.c_iflag |= (IXON | IXOFF | IXANY);
else
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
// enable RTS/CTS
if (settings->hardwareFlowControl == 1)
tty.c_cflag |= CRTSCTS;
else
tty.c_cflag &= ~CRTSCTS;
tty.c_cc[VMIN] = 1; // return read function when receive 1 byte
tty.c_cc[VTIME] = 10; // 1 seconds read timeout (deciseconds)
tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
// non-canonical mode
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL);
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty.c_oflag &= ~OPOST;
// flush port & apply attributes
tcflush(serialDevice, TCIFLUSH);
if (ioctl(serialDevice, TCSETS2, &tty) == -1)
{
sendLog("Can't set serial attributes | setSerialSettings", LOG_TYPE_ERROR);
return FALSE;
}
return TRUE;
}
My application is able to communicate with baud rates like 4800, 9600 and 115200 but can't with 14400 or 38400.
There is a pretty nice writeup for how custom serial speed setting works here: https://github.com/npat-efault/picocom/blob/master/termios2.txt.
In brief, given a struct termios2 identified by tty, to set both input and output speed to custom values, you must
ensure that tty.c_cflag & CBAUD == BOTHER. You appear to do this correctly.
set the desired output speed in tty.c_ospeed. You do this, too.
either
ensure that (tty.c_cflag >> IBSHIFT) & CBAUD == B0, in which case the output speed will also be used as the input speed, or
ensure that (tty.c_cflag >> IBSHIFT) & CBAUD == BOTHER, in which case tty.c_ispeed will be used as the input speed.
You do not do either of those. I'm uncertain why this would cause incorrect communication for some speeds and not others, but the driver is reputed to play some interesting games with speed settings, and maybe you've stumbled across one.
As for
read function doesn't return after VTIME
I think you have incorrect expectations. You are setting VMIN and VTIME both to nonzero values. In this case, VTIME is the maximum inter-character time, not an overall read timeout. With these settings, a blocking read will wait indefinitely for the first character, then will keep reading subsequent characters, up to the requested number, as long as each one arrives within VTIME deciseconds of the previous one.
If you want an overall timeout on every read() call, then set VMIN to 0, and be prepared for some read() calls to read 0 bytes. As always, read() may also read a positive number of bytes but fewer than requested. That may be more likely to happen in this configuration than in the one you're presently using, depending on your choice of VTIME and the behavior of the peer.

serial aplication writtes "0D" as "0A"

I have a serial app on C that receives data and writes it onto a binary file. The problem is that all the data is the same but when i have 0A on sending side, i have 0D on receiving side. I have set the serial port on raw mode and opened the file with wb option. Any clue how to avoid this? If some code is needed, i'll post it.
thanks
EDIT--------------------------
File opening:
FILE *fout;
fout = fopen(file,"wb");
Serial options:
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
/* To disable software flow control simply mask those bits: */
options.c_iflag &= ~(IXON | IXOFF | IXANY);
options.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
tcsetattr(fd, TCSANOW, &options);
success = 1;
return success;
Writting on the file:
fwrite(buffer,1,n,fout);
----------FIX----------------------
Setting this option fixes the problem :
options.c_oflag &= ~OPOST;
You need to mask off the ICRNL mode which translates the enter (carriage return) key to the newline character. This is on the receiving end, not in your program. There's also a corresponding output mode that might be set on your end, but less likely.

What does this operator do?

I am writing a Linux driver for my chinese arduino. At one moment I need to change the baud rate. I looked for examples and found that listing:
Listing 2 - Setting the baud rate.
struct termios options;
/*
* Get the current options for the port...
*/
tcgetattr(fd, &options);
/*
* Set the baud rates to 19200...
*/
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);
/*
* Enable the receiver and set local mode...
*/
options.c_cflag |= (CLOCAL | CREAD);
/*
* Set the new options for the port...
*/
tcsetattr(fd, TCSANOW, &options);
The next to last line of code has the |= operator. What does it do? I've never seen it before.
options.c_cflag |= (CLOCAL | CREAD);
is generally equivalent to
options.c_cflag = options.c_cflag | (CLOCAL | CREAD);
except options.c_cflag is evaluated only once, which doesn't matter in the above expression, but it would matter if options.c_cflag had any side effects (for example, if it were *options.c_cflag++)

Default settings for IrDA discovery (baud, bits, stop, parity, flow control, etc)

I'm attempting to write a tool that uses IrDA to communicate with Uwatec dive computers...on a Mac. The USB IrDA device I'm using provides a serial device (/dev/cu.IrDA-IrCOMM0 and /dev/tty.IrDA-IrCOMM0) that can be used to send and receive data. Unfortunately, the Mac does not provide an IrDA socket layer.
I've confirmed using the command line tool that shipped with the device driver that it can listen and receive IrDA communications from other devices. However, while the command line tool tells me it is communicating at 9600 baud, the rest of the settings (bits, stop bit, parity, flow control, etc) are not presented back to me.
I've tried writing my own program to listen to the data, but it is unable to receive any data and I believe the reason is because these settings are not correct. So, assuming that I'm just trying to listen to the 9600 baud IrDA discovery packets that are being sent around, what are the other settings I need to use?
If it helps, here's the snippet of code I'm currently using to set the communication parameters -- which doesn't work:
#define DEVICE "/dev/cu.IrDA-IrCOMM0"
int main(void) {
FILE *device;
struct termios ttystate;
device = fopen(DEVICE, "rw");
//get the terminal state
tcgetattr(fileno(device), &ttystate);
//turn off canonical mode and echo
ttystate.c_lflag &= ~(ICANON | ECHO);
//minimum of number input read.
ttystate.c_cc[VMIN] = 1;
cfsetspeed(&ttystate, B9600); // Set 9600 baud····
ttystate.c_cflag |= (CS8 | // Use 8 bit words
PARENB | // parity enable
PARODD | // odd parity
CCTS_OFLOW | // CTS flow control of output
CRTS_IFLOW);// RTS flow control of input
//set the terminal attributes.
tcsetattr(fileno(device), TCSANOW, &ttystate);
return EXIT_SUCCESS;
}
Below is the IrDA initialization code I used for EXAR XR17C15 on Linux.
Additionally you have to set baudrate as its not set in this example.
I hope this helps.
//
// Set up IrDA hardware interface through UART
bool CeIrDA::SetupIrDA()
{
struct termios termios;
tcgetattr(hComPort, &termios);
cfmakeraw(&termios);
termios.c_iflag = IGNPAR;
termios.c_oflag = 0;
termios.c_cc[VTIME] = 1; // timeout in 0.1s between 2 characters
termios.c_cc[VMIN] = 1; // min # of characters
tcsetattr(hComPort, TCSANOW, &termios);
xrioctl_rw_reg input;
struct timespec delay = {0, 500};
struct timespec delayrem;
//EFR: Allowing Enhanced Functions and RTS/DTR flow control
input.reg=0x09;
input.regvalue=0x50;
ioctl(hComPort,WRITE,&input);
nanosleep(&delay, &delayrem);
//MCR: Flow control RTS enabled
input.reg=0x04;
input.regvalue=0x40;
ioctl(hComPort,WRITE,&input);
nanosleep(&delay, &delayrem);
//MCR: RTS pin high
input.reg=0x04;
input.regvalue=0x00;
ioctl(hComPort,WRITE,&input);
nanosleep(&delay, &delayrem);
//MCR: RTS pin low
input.reg=0x04;
input.regvalue=0x02;
ioctl(hComPort,WRITE,&input);
nanosleep(&delay, &delayrem);
//MCR: Infrared Mode
input.reg=0x04;
input.regvalue=0x42;
ioctl(hComPort,WRITE,&input);
nanosleep(&delay, &delayrem);
//EFR: Allowing Enhanced Functions and RTS/DTR flow control
input.reg=0x09;
input.regvalue=0x40;
ioctl(hComPort,WRITE,&input);
nanosleep(&delay, &delayrem);
//LCR: Allowing changes of DLL and DLM
input.reg=0x03;
input.regvalue=0x80;
ioctl(hComPort,WRITE,&input);
nanosleep(&delay, &delayrem);
//DLL: Speed configuration
input.reg=0x00;
input.regvalue=0x02;
ioctl(hComPort,WRITE,&input);
nanosleep(&delay, &delayrem);
//DLM: Speed configuration
input.reg=0x01;
input.regvalue=0x00;
ioctl(hComPort,WRITE,&input);
nanosleep(&delay, &delayrem);
//LCR: configuration for parity, word length....
input.reg=0x03;
input.regvalue=0x03;
ioctl(hComPort,WRITE,&input);
nanosleep(&delay, &delayrem);
return true;
}

Read data off USB at 31250 baud rate

I've got an Arduino board and want to read the data it's spitting off using USB at a custom baud rate. Hacking up some of the code Arduino suggest, I get this C code:
int serialport_init(const char* serialport, int baud)
{
struct termios toptions;
int fd;
printf("init_serialport: opening port %s # %d bps\n", serialport,baud);
fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
serialPortPointer = fd;
if (fd == -1)
{
printf("Unable to open port when initialising hardware'n");
return -1;
}
if (tcgetattr(fd, &toptions) < 0)
{
printf("Couldn't get term attributes when initialising hardware\n");
return -1;
}
speed_t brate = baud; // let you override switch below if needed
switch(baud) {
case 4800: brate=B4800; break;
case 9600: brate=B9600; break;
case 14400: brate=B14400; break;
case 19200: brate=B19200; break;
case 28800: brate=B28800; break;
case 38400: brate=B38400; break;
case 57600: brate=B57600; break;
case 115200: brate=B115200; break;
}
cfsetispeed(&toptions, EXTA);
cfsetospeed(&toptions, EXTA);
// 8N1
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
// no flow control
toptions.c_cflag &= ~CRTSCTS;
toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
toptions.c_oflag &= ~OPOST; // make raw
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
toptions.c_cc[VMIN] = 0;
toptions.c_cc[VTIME] = 20;
if(tcsetattr(fd, TCSANOW, &toptions) < 0)
{
printf("Couldn't set term attributes when initialising hardware\n");
return -1;
}
return fd;
}
The problem is that the termios.h file doesn't support 31250 (MIDI) baud rate... If I try entering 31250 as the baud rate, this function returns -1 and says "Couldn't set term attributes when initialising hardware" (it fails right at the end).
So - how can I write a program, in C, or any other language, that reads the data off at the baud rate I want? Does termios.h support custom baud rates?
I literally just want to read the data on the serial port - nothing else.
This is a library that enables MIDI I/O communications on the Arduino serial ports. You need an Arduino capable of minimum 2 serial ports (like this one). One serial will be used for communication to MIDI devices (31250bps), and the other one to PC (for example 115200bps). If you have only one serial port on your Arduino board then you can also experiment with a software serial library like this.
The termios.h API simply does not have a way to express user-defined baud rates, though depending on your operating system there may be an extension to do so. Can you try setting up the Arduino to use the more standard 38400 instead?

Resources