ReadCom

Com port management function. Function not available anymore since Windows XP.

The ReadCom function reads the specified number of characters from the FIFO buffer of the specified Com port. Not available in WinTask Lite.

Syntax

ret=ReadCom(<num_port>,<numbytes>,<char$>)

Parameters

<num_port>: Com port number from which to read (from 1 to 8, Com1 to Com8).

<numbytes>: number of bytes to be read from the Com port buffer.

<char$>: character string variable used to receive the characters.

Return value

Ret, numeric return code. If the specified Com port has not yet been opened, the function returns 8; you can use this return code for Error Handling.

If <numbytes> is 0 or is equal to the number of characters available in the buffer, all the characters in the buffer are read and the buffer is emptied. The return code is then 0.

If no characters are in the buffer or if less than <numbytes> characters are available in the buffer, the return code is 13.

If numbytes < number of characters available in the buffer, return code is 12.

See also

OpenCom, WriteCom, CloseCom

Example

 

result$=""

Ret=OpenCom(1,9600,"n",8,1,"n")

Ret=ReadCom(1,3,result$) ' Reads the first 3 characters of Com1 buffer

Example code

For this example, you must connect two PCs with a NULL MODEM cable (using COM1).

The sending program installed on one of the PCs sends a 20 character string. The receiving program installed on the other PC waits until the sender sends a 20-character string to it.

Settings for the Com port are :
No flow control, COM1, Speed 9600, no parity, 8 data bits, 1 stop bit.

 

'******************************************************

'**************** Sending program *********************

'******************************************************

 

res=OpenCom(1,9600, "n",8,1, "n" )

 

SendVar$="SERIAL PORT TEST"

 

' the string SendVar$ will be sent to the serial port.

res=WriteCom(portnum, SendVar $)

 

res=CloseCom(1)

 

 

'******************************************************

'************* Receiving program **********************

'******************************************************

 

sub main(ByteNb)

 

var$=""

res = ReadCom(1, ByteNb,var$)

 

Select Case res

Case 0

' we emptied the buffer, we received the 20 characters

 

message$=message$+var$

out=1

 

Case 13

'it there are not 20 characters in the buffer, it is necessary to make another
' read but with the number of characters equal to the number of characters still
' in the buffer

message$=message$+var$

i=i-len(var$)

Case Else

msgbox("unexpected characters"+str$(res))

stop

EndSelect

 

Endsub

 

res=OpenCom(1,9600, "n",8,1, "n" )

 

i=20

out=0

message$=""

 

repeat

main(i)

until out=1

 

res=CloseCom(1)

 

msgbox(message$,32,"Received")