[fpc-pascal]Serial Port Programming under LINUX...
brad at seme.com.au
Mon Apr 29 11:43:33 CEST 2002
Rainer Hantsch wrote:
> Well, I am afraid that this is a completely different thing, because I am
> orginating the communications rather than being passive and being called from
> I want to communicate to/with Microcontrollers at a very low level. This means
> that I "mis-use" DTR as an "Attention" line.
> This is an easy "Packet protocol": I lower DTR for some time (roughly 1/2
> second now) to give all external PIC(s) enough time to complete their current
> task and enter a "communication mode", After rising DTR again, I send out the
> data (this are some bytes only, but full 8 Bit!) immediately and receive then
> the answer.
> This happens at a relatively low data rate of 1200-2400 bps, but it is timing
> sensitive, because the PICs have absolutely no data buffering. So I must give
> them even between particular bytes enough time to process them!
The example code you looked at is used to do exactly that..
I use it to talk to pics at 38400 baud and I can recieve a message
and turn it around in less than 10mSec (I modify the kernel HZ value to 400)
If you open the port in non-blocking mode then you can simply do a recv
on the port and if you get 0 bytes there is no data for you.
OR, you can open in blocking mode and ask for x bytes and it will hang up
waiting for that data to arrive.
I do a select() on the port and it sits there and waits for some data to
arrive. I use multiple ports in the select to allow me to do many things
while waiting for the data.. It's very easy if you think about it a little.
In fact, I used to poll my remotes. Now I have an RS232-RS485 interface
that self times TX and also set's RTS to indicate bus busy, I now talk to
many devices in a "collision bus" type of setup and can return messages
in less than 10mSec. My Pics have a retry timer of 100mSec and I have never
failed to turn around a message in this time. Even on a 386SX25.
For your application do this..
Delay 1/2 Sec
RXDataLength = 0
TimeOut = False
While (RXDataLength < Wanted) and (Not Timeout) do
Data := Data + Recieve(Port,GotLength)
RXDataLength := RXDataLength + GotLength
Check For Timeout() <-- use system msec timer or something
Here you either have the data you want or it has timed out..
Clear as mud ?
If you have to pace the data going to the pics.. then send it a byte
at a time with a delay in a loop.. Easy..
Save the Forests \ / ASCII RIBBON CAMPAIGN
Burn a Greenie. X AGAINST HTML MAIL
More information about the fpc-pascal