 |  |
RS-232 Lib
Complete Comm Control
TeraTech's RS232 Library lets your Basic program do advanced
serial RS232 ports communication. Potential applications include
sophisticated modem transfers, factory control and scientific
instrument monitoring. Features include:
* Background transmit/receive thousands of characters
* Multiple ports allowed (hardware dependent)
* Transfers at speeds upto 115200 baud
* 16550 chip support for low data loss at high speeds
* Variable size buffer initialization and removal
* Open and close ports; set speed, parity etc
* Complete RS232 input lines status
* Flow control handling for safe buffer management
* Checking for both hardware and software errors
Buffer initialization
Each serial port needs to be allocated a buffer for sending and
receiving characters. These buffers allow data to be transferred>
in the background so that your program can process the data when
it is convenient. Buffer memory is allocated from Basic's far
heap and a CalcRequiredMem function is provided to determine the
amount of buffer memory required. Calling InitCommBuffers
allocates and initializes the buffers. When your program is
finished using the serial ports, you can call DeleteCommBuffers
to deallocate the buffers so the memory can be reused.
Port Settings
Once the receive and transmit buffers are initialized, you can
initialize the port address, port interrupt request line (IRQ),
and the flow control settings by calling the SetPortAddr,
SetIRQ, and SetFlowControl routines, respectively. These
routines only need to be called if they differ from the default
values for those settings. Port settings are initially set when
the port is opened with the OpenCommPort function.
Reading and Writing Comm Data
ReadFromComm and WriteToComm are used to read and write strings
of characters or single characters from a serial port. One
thing to watch out for when using the WriteToComm routine is to
make sure that you don't attempt to write more characters than
are available in the transmit buffer. The WriteToComm routine
will only write up to the number of characters free in the
transmit buffer. Thus, if there are only 5 bytes left in the
transmit buffer but you attempt to write 10 bytes to the buffer,
then only the first 5 bytes will be written. The WriteToComm
routine will report the actual number of bytes written in the
BytesWritten%parameter. In contrast, the ReadFromComm routine
will only read what is available in the receive buffer. Thus,
if there are 5 bytes waiting in the receive buffer and you
attempt to read 10 bytes, then it will only read the 5 bytes and
report back that it only read that many bytes in the BytesRead%
parameter. You can call the GetCharsInBuffer routine to find
out how many bytes are waiting in the receive buffer and the
number of bytes available in the transmit buffer.
Line Control
On a typical RS-232 serial port, there are 9 lines that are
significant.
DCD The data carrier detect line.
RD The receive line.
TD The transmit line.
DTR The data terminal ready line.
DSR The data set ready line.
RTS The request to send line.
CTS The clear to send line.
RI The ring indicator line.
GND The ground line.
Of these lines, the DSR, CTS, RI, and DCD lines are considered
input lines because the status of these lines are dependent on
the machine or device that you are communicating with and will
be affected by that machine or device. In order to obtain the
status of these lines, you can call the LineStatus routine.
Flow control
Flow control is very important when communicating at high speeds
because the data will be coming in faster than the computer can
read them from the receive buffer. If proper flow control is
not used, thendata will be lost when the receive buffer
overflows. Flow control tells the other computer to stop
sending data when the receive buffer is nearly full and to start
sending again when it has been emptied of some of these
characters. The flow control status can be set using the
SetFlowControl.
The number of characters in the transmit and receive buffers can
be determined by calling the GetCharsInBuffer routine. This
routine is useful when you are waiting for a certain number of
characters to be received before reading the data from the
receive buffer. Also it is useful for determining the number of
characters available in the transmit queue before writing to the
port because the WriteToComm routine will only write up to the
number of characters available. If you attempt to write more
characters than are available in the transmit queue, then the
routine will only write up to the number of bytes available. The
extra characters not written will be lost!
Flushing
The buffers can be flushed by calling the FlushBuffers routine.
This routine should normally be called right before the port is
closed or before reading or writing a block of data that you
want to keep separate from data that has been read or written
before. Also, this routine should be called right after the
port is opened because sometimes the comm port thinks that it
has received a character but in reality, this character is
garbage. One thing to keep in mind, when calling this routine,
is that any data that is currently in the buffers will be lost.
Error handling
The CommError routine should be called periodically to determine
what the latest serial port error was. The types of errors that
are reported by this routine are hardware overruns, parity
errors, framing errors, break conditions, and receive buffer
overflows (software overruns). These errors are cleared each
time you call the routine so you should check for the errors
that are important to you and then handle the errors
appropriately for your application each time the routine is
called.
Demos
The demos illustrate how to use these routines in an actual
communications program. One of the programs is an example of
how two computers can be used to test the performance of the
communications routinesby writing and reading random blocks of
data and checking to make sure that they were received properly.
The example is actually split into two programs called
CPUTEST.BAS and HOSTTEST.BAS. The CPUTEST.BAS program is the
one that will write a character to the buffer to tell the
computer running the HOSTTEST.BAS program to send a block of
data. The CPUTEST.BAS routine will wait for the packet to be
received and then it checks that the packet was received
properly. It will time out if the packet was not received in
the time allotted for waiting and then ask for next block to be
sent.
The other demo is a utility that will transfer a file from one
computer to the other using the XMODEM protocol. The two
programs are called XMS.BAS and XMR.BAS. The XMS.BAS program
sends the file to the other computer and the XMR.BAS program
will receive the file.
Routine syntax
The syntax of the routines in the RS232 Library are given below.
CalcRequiredMem(PortSeg%, PortOfs%, RecvSeg%,
RecvOfs%, XmitSeg%, XmitOfs%, NumPorts%,
MemSize&, Ecode%)
CloseCommPort(ComPortNum%, Ecode%)
CommError(ComPortNum%, Ecode%)
DeleteCommBuffers (Ecode%)
FlushBuffers(ComPortNum%, WhichBuffer%, Ecode%)
GetCharsInBuffer(ComPortNum%, RecvQue%,
XmitQue%, Ecode%)
InitCommBuffers(Ecode%)
LineStatus(ComPortNum%, CTS%, DSR%, CD%,
RI%, Ecode%)
OpenCommPort(ComPortNum%, BaudRate&,
DataBits%, Parity%, StopBits%, Ecode%)
ReadFromComm(ComPortNum%, ReadBuff$,
BytesRead%, Ecode%)
SetFlowControl(ComPortNum%, InFlow%,
OutFlow%, InMin%, InMax%, XonChar%,
XoffChar%, Ecode%)
SetIRQ(ComPortNum%, IRQ%, Ecode%)
SetPortAddr(ComPortNum%, PortAddress%, Ecode%)
WriteToComm(ComPortNum%, WriteBuff$,
BytesWritten%, Ecode%)
Demo: - RS232 Serial Communications Library
Price: $149.00 (plus shipping and handling)
|