Sequential write in Linux lock, timeout solution?

It seems to me that this should be a general error condition for serial communications, but I don't see a good solution yet.

I have set up a serial port that is not blocking and I have disabled flow control. Then I use Select to determine when I can write the port. I am trying to write large buffers (over the page size, 4K, which I think is the amount of software buffering provided to the port). The first part of the write will go through, but then I block trying to write the rest if the serial connection is broken or the end consumer is disconnected / rebooted. I thought about writing smaller chunks of data, but it seems to me that eventually I will fill the buffer and tackle this case again. Is there a way to request the available buffer before writing? A way to resize the buffer without recompiling the kernel?

My goal is to have a way to timeout the failed write so that I can clear and notify the user. I don't want the tail of the faulty message to go straight when the device comes back. Sequential settings (baud, stop bit, etc.) can also change over time, and I don't want them to change while I can have a record.

I figured out that I could send a sigusr1 signal to the stream and that would undo the current write, but it doesn't look so graceful. Perhaps this is the preferred method? Also I looked into using aio_write, but I have never used it before and I am not aware of its limitations.

If anyone has a good solution for timing failed recordings, I'd be interested to hear them. Thank.

EDIT: I did more research and my question was not entirely correct. I was using psuedo serial ports and writing to them behaved differently than real serial ports. The question will still matter if you enabled hardware flow control or had to use psuedo serial ports.


source to share

1 answer

Sending a signal stream is the best way to do this, although I would do it using timer

to set a timeout; if I remember correctly, this is what I have done in the past (pesudo-code):

int value = write( fd, buffer, buff_len );
if( value < 0 && errno == EINTR ) {
    /* We were interrupted by a signal - write failed. */
}else if( value < 0 ){
    /* other error */
}else if( value != buff_len){
    /* did not write out entire buffer */
    /* Write should be good */





All Articles