Waldek Hebisch wrote:
Mathias Uhle wrote:
My problem: I want to open a serial device in a GP-Program to read and write to it in changing order. I look for a way that allowes to do (non blocking) read AND write to one single file-variable assigned to eg /dev/ttySx. So far I have found the "CanRead-function" in the manual which serves perfect for the non blocking job!
Indeed. Of course, there's also CanWrite for non-blocking writing, and the more powerful (but also more complicated to use) IOSelect, to check for both things at once, possibly on several files at once, and optionally with a timeout (in order to avoid busy waiting).
But still I have to use two file-variables, one opened with reset, one opened with rewrite, and both symbolically linked to the desired serial file eg. /dev/ttySx. And it works, reading from the first when "CanRead" is true and writing to the latter whenever it is necessary.
Having read almost all portions of the GNU Pascal Manual nevertheless could not find a solution to use only one file-variable.
The following should work (I works on ordinary file):
program do_rw; var f : file of char; c : char; begin reset(f, '/dev/ttyS0'); write(f, 'b'); read(f, c); writeln(c); end .
The crucial point is that I use "file of char" (one can use other typed files, but for serial port using anything bigger then byte is going to loose). AFAICS bidirectional access to non-disk text files is not supported.
You could try the TextMode variable. The (BP compatible) default FileMode_Reset_ReadWrite enables the behaviour as Waldek explained. Adding FileMode_Text_Reset_ReadWrite should do the same for text files, but due to the special handling of line breaks in text files, you might really prefer file of char (or BP compatible untyped files with a block size of 1 and using a 4th argument to BlockRead/BlockWrite).
Frank