#include <SERIAL.bas>                                  ' source in /Program Files/Coridium/BASIClib

FUNCTION SERIN (pin, baud, posTrue, INcnt, BYREF INlist as string)


SERIN receives INcnt bytes into the INlist string as asynchronous serial data on pin at a baudrate.  Data is positive TRUE PosTrue if set to 1, else the data is inverted.

If INcnt is 0, then the string will be filled with bytes until a 0, CR or LF character is received.  Note that no bounds checking is performed on the input, and if a 0, CR, or LF is never received then this routine will hang.    As there is no bounds checking its possible to overwrite other variables, if less than 256 bytes have been allocated for the InputList string.

SERIN will timeout after 0.5 seconds and return -1 and place 255 in the next item in the INlist before the timeout.  These routines are "bit-banged" by the processor, so the processor is consumed during these operations.  Interrupts are also disabled during each byte for these operations.  The hardware UART0 can be used see RXD0 or DEBUGIN .  The timeout can be changed with SERINtimeout.

Baudrates can be up to 115.2 Kbaud for all pins on transmit.  Receive rates to 57Kb


#include <SERIAL.bas>

DIM Bytes(10) as STRING

' Read serial stream for 1 byte from pin 1 saving to MyByte, negative true
SERIN (1, 19200, 0, 1,Bytes)
#include <SERIAL.bas>

' In this case we are reading an open loop device
' that is continuously sending CR terminated strings on the serial line
' to ensure we read a complete line first sync up by looking for a CR character

DIM Astr(20) as STRING

io(15)=0 ' flag that we are sync'ing up
while 1
  serin (3,19200, 1, 1, Astr)
  if Astr(0) = 10 then exit

io(15)=1 ' and that sync is complete
while 1
  serin (3,19200,1, 0, Astr)
  print Astr

Differences from other BASICs

See also