seriallib
These commands use a serial device of the system, normally
serial.device, for a modem, another computer or a MIDI interface.
OpenSerial opens a unit of the device with a baud rate and returns 0
if that fails; the other commands then name the unit. All units still
open are closed when the program ends.
If OpenSerial("serial.device",0,9600,0)
WriteSerialString 0,"ATZ"+Chr$(13),3
Repeat
c.w=ReadSerial(0)
Until c<>-1
CloseSerial 0
EndIfUsing a unit that is not open is the error “Serial Device Not Open”,
also without runtime error checking (CloseSerial and SerialEvent
do not check).
Based on the seriallib guide by Acid Software.
Library number 90.
Commands
CloseSerial
Closes a unit opened with OpenSerial.
Statement. Help text: unit#
Arguments: .w
A write that is still going on is stopped first.
OpenSerial
Opens unit unit# of the serial device device$ at the baud rate baud, with the flags io_serflags, and returns 0 if it cannot be opened, else the address of the unit’s data in the library.
Function, result .l. Help text: device$,unit#,baud,io_serflags
Arguments: $, .l, .l, .w
The flags are the sum of these values:
| Value | Effect |
|---|---|
| 1 | parity on |
| 2 | odd parity (else even) |
| 4 | seven wire handshake |
| 8 | a break waits until the buffer is empty |
| 16 | high speed mode |
| 32 | shared use of the port |
| 64 | end of file checking |
| 128 | no xON/xOFF |
A unit that is already open is left as it is,
and OpenSerial returns 0 for it, also with shared use.
ReadSerial
The next byte that came in on the unit, or -1 if none is waiting; with the flag 1 it waits for a byte. Use a word variable, as in c.w=ReadSerial(0), to tell -1 from 255.
Function, result .w. Help text: (unit#[,flags 1=wait on]) returns -1 if nothing waiting
2 forms:
.w.w, .w
The flag 2, which the original took for waiting, waits as well.
ReadSerialMem
Reads Length bytes from the unit into memory at Address and waits until they have come.
Statement. Help text: unit#,Address,Length
Arguments: .w, .l, .l
ReadSerialString
All bytes waiting on the unit, as a string; an empty string if none is waiting.
Function, result $. Help text: (unit#) returns null if nothing waiting
Arguments: .w
It reads at most what fits in the string buffer; the rest stays waiting for the next read.
SerialEvent
Whether a signal of the unit came at the last Wait, for a program that waits for several things at once; 2 for its read signal, 1 for its write signal, 0 if none has.
Function, result .l. Help text: (unit#)
Arguments: .w
SetSerialBuffer
Sets the size in bytes of the unit’s read buffer.
Statement. Help text: unit#,bufferlength
Arguments: .w, .l
SetSerialLens
Sets the number of bits of a byte read and written and the number of stop bits of the unit.
Statement. Help text: unit#,readlen,writelen,stopbits
Arguments: .w, .b, .b, .b
SetSerialParams
Passes the settings of the unit’s request to the device again, for example after changing the request directly.
Statement. Help text: unit#
Arguments: .w
WriteSerial
Sends one byte to the unit, for a character for example Asc("b"), and waits until it is sent.
Statement. Help text: unit#,byte
Arguments: .w, .w
WriteSerialMem
Sends Length bytes from memory at Address to the unit and waits until they are sent.
Statement. Help text: unit#,Address,Length
Arguments: .w, .l, .l
WriteSerialString
Sends a string to the unit. With the flag 1, the command waits until the string is sent; without it, the program goes on at once. With the flag 2, the whole string is sent; without it, the string is sent up to its first zero byte. 3 sets both.
Statement. Help text: unit#,string[,Flags 1=DoIO 2=TrueString]
2 forms:
.w, .l.w, $, .w
The original took both meanings from the flag 2; a program written
for it that passes 2 to wait passes 3. Before the next command that
writes to the unit, a program waits for a write that does not wait,
with Wait and SerialEvent. The form without flags takes the
address of a string ending in a zero byte, and does not wait.