[fpc-pascal]Serial Port Programming under LINUX...
Stefan Becker
Stefan at YukonHo.de
Fri Apr 26 15:39:56 CEST 2002
Hi again,
what are you doing with the seriel port? If it's something like modem
communications or Terminal then just let the operating system do the work
and "talk" to the device through STD IN/OUT. I rewrote one program that
was a terminal driver and now I just use /usr/sbin/mgetty to do the work!
Now a real question for the linux programmers:
I use and open the device using : "fdOpen('/dev/ttyS0'....);"
reading the device with: "FD_SET (PortHdl,FDS);"
and "Select(PortHdl+1, at FDS, nil,
nil, 50);"
and "FD_ISSET(PortHdl,FDS)
and
"fdRead(PortHdl,TermInStr[1],sizeof(TermInStr)-1);
(see examples below)
I would say that the operating system would be doing all then I/O buffering?
Is this correct? Or would I loss charater input from the device /dev/ttyS0
if
there would be more then 16 char (whatever the hardware UART holds)
waiting?
I POLL my input procedure fairly infrequently and have never lost any
seriell input that
I know of!
to you knowledgable linux Gurus...
my regards,
Stefan
EXAMPLES:
function StartInput:boolean;
const
ScalePort = '/dev/ttyS0'; {S0 = com1 S1 = com2}
begin
StartInput:=false;
PortHdl:=fdOpen(ScalePort,Open_RdWr or Open_NonBlock or Open_Excl);
if PortHdl>0 then
begin
{Attempt to Lock the port, I'm not sure this is strictly nessecary}
FLock(PortHdl,LOCK_EX);
IOCtl(PortHdl,TCGETS, at tios);
tios.c_cflag := B2400 or CS8 Or CREAD Or CLOCAL;
tios.c_lflag := 0;
tios.c_oflag := 0;
tios.c_iflag := IGNPAR;
IOCtl(PortHdl,TCSETS, at tios);
DtrOn;
RS485RX;
StartInput:=true;
end;
end;
procedure ScaleInput;
begin
FD_Zero (FDS); {Clear File Descriptors Array}
FD_SET (PortHdl,FDS); {Input from Serial Port}
{index+1 Readfds,Writefds,exceptfds,TimeOut_ms}
{Will Wait 50ms or input from above}
InAct:=Select(PortHdl+1, at FDS, nil, nil, 50);
if InAct= -1 then
begin
WriteLn('Error in "Select"',LinuxError);
TagQuit:=true;
end else
begin
if InAct>0 then
begin
if FD_ISSET(PortHdl,FDS) then {Have we data waiting in UART ? }
begin
InLen:=fdRead(PortHdl,TermInStr[1],sizeof(TermInStr)-1);
if InLen>0 then
begin
for Loop:=1 to Inlen do
begin
poin:= (ord(TermInStr[Loop]) and $7F);
case poin of
10:begin
do_StrIsIn;
inScaleStr:="'';
end;
13:begin
end;
else
InScaleStr:=InScaleStr+chr(poin);
end;
end; {case}
end;
end;
end; {no input}
end; {no error}
end;
>Thanks!
>
>I had a look on this code right now, but I am afraid that this code will
not
>supply me a buffered input. (I mean: If I currently do NOT read the
arriving
>data, the data must be buffered in some kind of FIFO, and I must be able to
>check if there are characters available before(!) doing a read operation.
>(important, because otherwise my program will hang until something comes
in!)
>
>Is this done with this code?
>
>
>On Fri, 26 Apr 2002, Stefan Becker wrote:
>
>> Hi,
>> I don't know if there is a fossile interface (unit) for linux but
>> Brad Campbell's (bcampbel at omen.net.au) unit helped me
>> alot.
>> I put it on http://home.t-online.de/home/yukonho/seriell.pp
>>
>> Stefan
>>
>>
>> -----Ursprüngliche Nachricht-----
>> Von: Rainer Hantsch <rainer at hantsch.co.at>
>> An: Free Pascal Group <fpc-pascal at lists.freepascal.org>
>> Datum: Donnerstag, 25. April 2002 10:26
>> Betreff: [fpc-pascal]Serial Port Programming under LINUX...
>>
>>
>> >Hello, all!
>> >
>> >I do really hope that there is somebody here who has real EXPERIENCE
with
>> >programming serial communications in FPC and for Linux. - I am totally
>> unhappy
>> >because I find no WORKING solution for this. Please, if you can help,
leave
>> me
>> >a message. -- THANKS!
>> >
>> >What I had in the past:
>> >-----------------------
>> >When I was programming in TP/BP for DOS, I used the FOSSIL/X00
TSR-driver
>> to
>> >communicate with serial devices. This TSR driver did all low level stuff
>> >for me, including a ring buffer, handshaking (both, XON/XOFF and/or
>> RTS/CTS),
>> >..., so I only had to write a relatively simple unit, to make "Fossil"
more
>> >user friendly.
>> >With this simple task I had the possibility to send/receive _buffered_,
>> >flush/purge the buffer contents, rise/lower DTR, ask if chars were
received
>> >_without_ actually reading them in,... -- EXCELLENT.
>> >
>> >What I am looking for:
>> >----------------------
>> >Since my change to Linux and FreePascal, I am really unhappy because
there
>> >seems to be nothing similar available here. Well, There are some units
>> >available for FPC, but most of them are for DOS or WIN32. The only one
>> which
>> >appears to be very useful, is OBJCOM.
>> >But this unit has an unacceptable BUG inside: It stops any further
>> >communication when the DCD is dropped by the modem when the connection
>> >breaks. It seems that this causes a total loss of the internal file
handle!
>> >Sadly to say, but it appears as if nobody is working on this ObjCOM any
>> >longer, including M.Kiesel who contributed ObjCom, so this appears to be
a
>> >dead end...
>> >
>> >
>> >If somebody has a unit which works with FOSSIL (DOS), WIN32 *AND* LINUX
and
>> >gives me an acceptable control to the data communication, I will be
happy
>> to
>> >know about that, even if this is a commercial unit.
>> >
>> >
>> >Thanks in advance,
>> >
>> >mfg
>> >
>> > Ing. Rainer Hantsch
>> >
>> >--
>> > \\|// Ing. Rainer HANTSCH - Hardware + Software
>> > (o o) Forget Windoze! -- We focus on L-I-N-U-X...
>> >--oOOo-(_)-oOOo--------------------------------------------------
>> >Ing. Rainer HANTSCH | e-Mail: office at hantsch.co.at
>> >Khunngasse 21/20 | www : http://www.hantsch.co.at
>> >A-1030 Vienna | Tel. : ++43 - 1 - 7988538 0
>> >---------------------| Fax : ++43 - 1 - 7988538 18
>> >** AUSTRIA ** | Mobile: ++43 - 664 - 9194382
>> >-----------------------------------------------------------------
>> >
>> >
>> >
>> >
>> >_______________________________________________
>> >fpc-pascal maillist - fpc-pascal at lists.freepascal.org
>> >http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>> >
>>
>>
>>
>> _______________________________________________
>> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
>> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>>
>
>
>mfg
>
> Ing. Rainer Hantsch
>
>--
> \\|// Ing. Rainer HANTSCH - Hardware + Software
> (o o) Forget Windoze! -- We focus on L-I-N-U-X...
>--oOOo-(_)-oOOo--------------------------------------------------
>Ing. Rainer HANTSCH | e-Mail: office at hantsch.co.at
>Khunngasse 21/20 | www : http://www.hantsch.co.at
>A-1030 Vienna | Tel. : ++43 - 1 - 7988538 0
>---------------------| Fax : ++43 - 1 - 7988538 18
>** AUSTRIA ** | Mobile: ++43 - 664 - 9194382
>-----------------------------------------------------------------
>
>
>_______________________________________________
>fpc-pascal maillist - fpc-pascal at lists.freepascal.org
>http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
More information about the fpc-pascal
mailing list