From: Bernhard Kuemel on 30 May 2010 19:15 Scott Lurndal wrote: > As for the OP, whether you write one byte or 512k bytes, the write > may block (for example, if you've conditioned the serial port to use > hardware (RTS/CTS) flow control, or DSR drops). Not if I open ttyS0 with O_NONBLOCK, hm? I don't use control lines. ATM I assume the uC will be fast enough to receive and process bytes at 115200 bps, but that remains to be tested. Bernhard
From: Jasen Betts on 31 May 2010 04:39 On 2010-05-30, Bernhard Kuemel <bernhard(a)bksys.at> wrote: > Hi clc! > > Can I write(2) much data (512K) to /dev/ttyS0 on Linux without blocking? > I want to be able to abort/flush the write if the microcontroller which > receives the data reports an error [2]. > > At first I planned to write byte wise, but select_tut recommends against > this [1] ... although at 115200 bps this might be acceptable. Actually, > I don't care if the program completes in 47s or 53s and uses a little > more or less user/system time. But I wouldn't want all 512K be > transmitted if an error occurs somewhere in the middle although I have > tried to prevent any harm if it still happened. > > If I write byte wise, how long could write() block once the buffer > filled up? Until a character has been transmitted, or is there a hysteresis? these are software and hardware buffers on the serial ports hardware buffers are usually sized 16B to 1KB software buffers in if enabled in units of 4K AIUI software buffers work in chunks, the hardware buffer works in bytes. > Also I could try to abort/flush the write if a signal handler receives > the error. I would, however, like to keep the program simple, for the > moment. yes, but not from inside the handler, If you haven't already: man 4 tty_ioctl > Would these issues be different on other Unices? possibly --- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Jasen Betts on 31 May 2010 04:44 On 2010-05-30, Bernhard Kuemel <bernhard(a)bksys.at> wrote: > Rainer Weikusat wrote: >> Bernhard Kuemel <bernhard(a)bksys.at> writes: >>> Can I write(2) much data (512K) to /dev/ttyS0 on Linux without blocking? >>> I want to be able to abort/flush the write if the microcontroller which >>> receives the data reports an error [2]. >> >> The simple way to do this is to let someone else block, eg fork a > > Ok. > > Out of curiosity ... what's the way to use nonblocking writes? > select_tut(2) recommends not to write single bytes at a time. So I try > to write 512K. Doesn't work, because the transmit queue only fits 8K (or > whatever) and I get EAGAIN. select(2) says the write will not block, but > it still won't work, because the queue is still only 8K. So either by > trial and error or by finding out (how?) how big the queue is, I write > 8K. After one byte is sent, select tells me I can write again, but > writing 8K fails because there is only 1 byte free in the queue. So I > either keep busy trying to write 8K until the queue is empty or I write > only 1 byte each time. Doesn't sound so great. you know the bit rate and the buffer size, divide the buffer size by the bit rate and nanosleep for that long. --- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Sergei Organov on 31 May 2010 09:55 Bernhard Kuemel <bernhard(a)bksys.at> writes: > Hi clc! > > Can I write(2) much data (512K) to /dev/ttyS0 on Linux without blocking? > I want to be able to abort/flush the write if the microcontroller which > receives the data reports an error [2]. Try to configure tty as non-blocking and wait for read and write on select(). When you write, try to write whatever was left from previous write, subtracting the result of write() call (the number of bytes written) from the size left every time (don't forget to move pointer or index into the buffer accordingly). Don't forget to check the return value of write() for not being negative before using it as number of bytes written. After first few Kb, when system is not loaded, you may end up writing a few bytes at a time (the actual value should mostly depend on the size and configuration of thresholds of hardware FIFO), but when system gets loaded, you will automatically write in larger chunks. When read, check for the errors from microcontroller. Consider flushing transmit buffers on error reception (tcflush(fd, TCOFLUSH)). -- Sergei.
From: Grant Edwards on 31 May 2010 12:22 On 2010-05-31, Jasen Betts <jasen(a)xnet.co.nz> wrote: >> Can I write(2) much data (512K) to /dev/ttyS0 on Linux without blocking? >> I want to be able to abort/flush the write if the microcontroller which >> receives the data reports an error [2]. >> >> At first I planned to write byte wise, but select_tut recommends against >> this [1] ... although at 115200 bps this might be acceptable. Actually, >> I don't care if the program completes in 47s or 53s and uses a little >> more or less user/system time. But I wouldn't want all 512K be >> transmitted if an error occurs somewhere in the middle although I have >> tried to prevent any harm if it still happened. >> >> If I write byte wise, how long could write() block once the buffer >> filled up? Until a character has been transmitted, or is there a hysteresis? > > these are software and hardware buffers on the serial ports > hardware buffers are usually sized 16B to 1KB > > software buffers in if enabled in units of 4K > > AIUI software buffers work in chunks, the hardware buffer works in > bytes. If the FIFO is enabled, then the hardware buffer will also work in "chunks" (at least from an application point of view). >> Also I could try to abort/flush the write if a signal handler receives >> the error. man tcflush Here's a good reference: http://www.easysw.com/~mike/serial/serial.html -- Grant
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Our customer websites are modern, fresh and accessible. Next: We are here for all your events! |