We use cookies to ensure that we give you the best user experience on our website.If you want to find out more about the cookies we use, you can access to our Privacy Policy here.By continuing your visit to our website, we'll assume you consent to our use of cookies.

DENSO DENSO WAVE

Close

Technical Information

Waiting for and reading incoming data

Function

Wait for and read incoming data.

Description

Use the following procedure to process the data received.

  1. Specify the timeout interval.
  2. Use a WAIT statement to wait for data input (receive buffer) or a timeout.
  3. Use the LOC function to check the number of bytes in the receive buffer and read any data available with the INPUT$ function.

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.

Sample Program


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

Contact us

QBdirect

If you have not registered

The services on this member site are available only for registered customers.

New member reg.