
Chapter 18. Bluetooth
461
18.2.4.2 Reading data received in serial communications
We recommend that user programs always follow the approach shown below, setting a
timeout and only reading data with INPUT$ functions and the like when there is actual data in
the receive buffer because there is every possibility of the direct approach hanging, waiting
for data, due to disconnection of the remote device or motion out of communications range.
Note that extended function BT.FN3 function number .fcBTGetStt is available for reading
the connection status for the remote device.
(Example)
Connect via virtual serial port to the remote device and receive.
DIM recvbuff$[255] ' Allocate receive buffer
OPEN "COM4:M,SPP" AS #4 ' Open Bluetooth communications device file
recvbuff$ = "" ' Clear receive buffer
TIMEA = 50 ' Receive wait timer: 5 seconds
WAIT 0,&h18 ' Wait for data or timeout
IF LOC(#4) > 0 THEN ' If data received,
WHILE LOC(#4) > 0 ' read data received
recvbuff$ = recvbuff$ + INPUT$(LOC(#4), #4)
TIMEA = 5 : WAIT 0,&h10 ' Consider 500 ms with no input
' as indicating end of receive operation
WEND
PRINT "Receive ";recvbuff$ ' Display data received
ELSE ' If no data received,
PRINT "Receive timeout" ' timeout
CALL "BT.FN3" .fcBTGetStt STATUS% ' Check current connection status
IF STATUS%=2 OR STATUS%=3 THEN ' If connected, receive again
' Retry receive
ELSE ' If disconnected, close and connect again
CLOSE #4
' Retry open and connect to remote device
ENDIF
ENDIF
CLOSE #4
Kommentare zu diesen Handbüchern