WriteCom

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

The WriteCom function writes the specified string to the specified Com port. Not available in WinTask Lite.

Syntax

ret=WriteCom(<port_num>,<char$>)

Parameters

<port_num>: Com port number to write to (from 1 to 8, Com1 to Com8).

<char$>: character string to be written.

Return value

Ret, numeric return code. If the string is successfully written, the function returns 0, otherwise use this return code for Error Handling.

See also

OpenCom, CloseCom, ReadCom

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")