Wait for and read incoming data.
Use the following procedure to process the data received.
Do not use the INPUT# and LINE INPUT# statements to read in data. They suspend the user application program until they receive a carriage return (0Dh) or comma (,), so can appear to hang if communications circuit errors produce data dropout or the circuit is broken.
const ComFile = 3 'file number of communication device file '+---------------------------------------------------------------------------+ '| | '| Function: Wait for receiving and read the data received | '|f.no% file number | '| para$ communications parameters | '| timeout% timeout | '| chartout% timeout interval characters | '| esc$ escape key | '| received data | '| | '| Description | '| Wait for receiving data | '| End this function under following condition: | '| -CR code is received | '| -Designated time as timeout% elapses | '| -Designated time as chrtout% elapses after receiving one or more | '| character | '| -Escape key is pressed | '| | '+---------------------------------------------------------------------------+ function rcvcom$(f.no%, para$, timeout%, chartout%, esc$) private r.dat$ private r.len% private wk$ open para$ as #f.no% r.dat$ = "" timea = timeout% 'specify timer while 1 wait 0,&h19 'Wait until the timeout expires 'or data is stored in the receiving buffer 'or any key is pressed r.len% = loc(#f.no%) if r.len% > 0 then while 1 timea = chartout% 'specify timer wait 0,&h19 'Wait until the timeout expires 'or data is stored in the receiving buffer 'or any key is pressed r.len% = loc(#f.no%) if r.len% > 0 then r.dat$ = r.dat$ + input$(r.len%, #f.no%) if right$(r.dat$,1) = chr$(&h0d) then rcvcom$ = r.dat$ close #f.no% exit function endif else wk$ = inkey$ if (wk$ = esc$) or (timea = 0) then 'when the escape key is pressed 'or designated time elapses rcvcom$ = r.dat$ close #f.no% exit function endif endif wend else wk$ = inkey$ if (wk$ = esc$) or (timea = 0) then 'when the escape key is pressed 'or designated time elapses rcvcom$ = "" close #f.no% exit function endif endif wend end function '=============================================================================== ' Main '=============================================================================== private rcvdata$ key 30,"m" while 1 print "receiving..."; rcvdata$ = rcvcom$(ComFile, "com2:115200,n,8,1", 100, 10, "m") 'timeout:10 seconds 'escape key : M1 key print rcvdata$ wend
If you have not registered
The services on this member site are available only for registered customers.