Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 11


On a timeout, the STR array contains any bytes that arrived. If you need to
know how many bytes were received, initialize the STR array with a value that
the remote computer doesn’t transmit. For example, if receiving text data, ini-
tialize the array with null characters (zeros) before calling herserin:
for index = 0 to 9
received_text[index] = 0
next index
Another way to read multiple bytes is to read one byte at a time and store the
bytes in an array. A task loop can call the routine below to read a specified num-
ber of bytes into an array:
index var byte

receive_serial_data:

received_data var byte[8]
bytes_to_read var byte

bytes_to_read = 8

if (PIR1.5 = 1) then

' A byte is available to read.

hserin [received_data[index]]

index = index + 1

if index >= bytes_to_read then

' Do something with the received bytes.

' Reset the index for the set of received bytes.

index = 0
endif
endif
return
Free download pdf