Category Serial I/O include description of functions for
using communication over COM-ports. This function based on
multiplatform library EZV24 http://libezV24.sourceforge.net/.
For using need linking clip-com library:
clip -e test.prg -lclip-com
COM_COUNT() | Counts the number of characters in the input buffer. |
COM_DTR() | Queries/sets the Data Terminal Ready (DTR) status. |
COM_FLUSH() | Clears the receiving buffer. |
COM_HARD() | Turns the hardware handshake (automatic CTS) on/off. |
COM_INIT() | Initializes the port parameters. |
COM_NUM() | Return the number of the highest available serial interface port |
COM_OPEN() | Opens the port and initializes the buffer |
COM_READ() | Reads characters from the receiving buffer. |
COM_RTS() | Queries/sets the RTS status. |
COM_SEND() | Transmits data. |
COM_SFLUSH() | Flush sending buffer. |
COM_SOFT() | Queries or sets the software handshake (automatic XON/XOFF). |
COM_TIMEOUT() | Set timeout for all operations in 1/10 seconds. |
nTotalCharacters := COM_COUNT(<nComPort>)
Return the number of characters in the selected buffer.
This function allows you to determine the number of characters in recieve buffer. This lets you determine how many characters can be read with the COM_READ() function.
If an attempt is made to read the number of characters available in a closed port, the function returns a value of -1.
nCharacter := COM_COUNT(1) // Number of characters-port 1 IF nCharacter > 0 COM_READ(1, 1) // Read 1 character ENDIF
ITK,Paul
No dependies of platform.
lOldDTRStatus := COM_DTR(<nComPort>,[<lNewDTRStatus>])
<nComPort> | - Designates the port (1 to N) for which the number of characters in the input buffer is determined. |
<lNewDTRStatus> | - Designates the status of DTR. .T. designates |
that the DTR signal is active, and .F. designates that the signal is | |
inactive. If the parameter is not specified, does not affect the DTR | |
exit signal. |
The returned value corresponds to the DTR signal status prior to the new setting, or in the absence of <lNewDTRStatus>, the returned value corresponds to the current DTR. A return of .T. indicates that the DTR is active (MCR bit 1 = 1). A return of .F. indicates that the DTR is inactive (MCR bit 1 = 0).
COM_DTR() queries the DTR signal status. If the second parameter is not supplied, the signal status does not change.
The COM_CLOSE() function resets DTR (to inactive). By contrast, COM_OPEN does not automatically make DTR active!
lComOk := COM_OPEN(1) // Open COM1 IF lComOk COM_DTR(1, .T.) // Activate DTR Port 1 * ... ENDIF
ITK,Paul
No dependies of platform.
lClear := COM_FLUSH(<nComPort>)
If the receiving buffer can be cleared successfully, the function returns .T..
This function allows you to flush all the characters in a receiving buffer. This makes it unnecessary to tediously determine how many characters are currently in the buffer in order to remove them with a read procedure.
COM_FLUSH() guarantees a truly empty buffer. However, a new character can be received between the time that you call a COM_COUNT() and a COM_READ().
cHayes := "ATZ" // Hayes modem reset command COM_SEND(1, cHayes) // Issue Hayes command INKEY(1) // Wait one second COM_FLUSH(1) // Reject modem reply messages
ITK,Paul
No dependies of platform.
lOldHandshake := COM_HARD(<nComPort>,<lNewHandshake>,[<lDTR/DSR>])
<nComPort> | - Designates the number of the port (1 to N). |
<lNewHandshake> | - Designates whether the handshake is on (.T.) or off (.F.). |
<lDTR/DSR> | - Designates whether the function uses the DTR/DSR |
handshake or the RTS/CTS handshake. When passed as .T., the function | |
uses the DTR/DSR handshake instead of the RTS/CTS handshake. |
When called only with <nComPort>, the COM_HARD() returns .T. if the hardware handshake is on and .F. if the hardware handshake is off. When <lNewHandshake> is passed, the function returns the status prior to the new setting.
The size of a receiving buffer is always limited. To avoid overflow, and thereby a loss of characters, software or hardware handshakes are usually implemented. During a hardware handshake, the port monitors the RTS signal. If you use this function to switch this handshake on, the signal is activated as soon as the buffer is 75% full. As soon as the buffer content is once again reduced to 50% or less, the port (the RTS signal) is once again released. Some printers use the port signals, DTR (output like RTS) and DSR (entry like CTS) instead of RTS/ CTS, to avoid buffer overflow. In this case, <lDTR/DSR> can be passed with .T..
This function works for data input as well as output.
If you use a hardware handshake, then the RTS signal should no longer be affected by COM_RTS() or COM_MCR().
COM_OPEN(2) // Open COM2 COM_HARD(2, .T.) // RTS/CTS Hardware handshake
ITK,Paul
No dependies of platform.
lInitialized := COM_INIT(<nComPort>,[<nBaudRate>],[<cParity>], [<nDataLength>],[<nStopBits>])
<nComPort> | - Designates the port (1 to N) that is initialized. |
<nBaudRate> | - Designates the selected baud rate for the port. Settings of 300, 600, 1200, 2400, 4800, 9600 and 19200 baud are possible. The default value is 300 baud. |
<cParity> | - Designates parity in the following form: (E)ven, (O)dd, (M)ark, (S)pace, or (N)one. The default value is (N)one. |
<nDataLength> | - Designates the data length; seven or eight bits are possible. The default value is eight bits. |
<nStopBits> | - Designates the number of stop bits, one or two. The default value is one stop bit. |
If the com port is initialized successfully, the function returns .T.
In order to use the serial port after you open it, four operating parameters must have been passed to this function. These operating parameters are the transmission speed (baud rate), the data length, the parity, and the number of stop bits. Tht defaul configuration is "9600 baud, 8N1". This configuration means eight data bits, no parity, and one stop bit. What is important is that all these parameters can be changed while the port is closed. Neither the data buffer or the modem status register (DTR, etc.) are affected.
The port must have been opened previously. It is impossible to initialize a port prior to opening it.
If any or all of the four operating parameters are not designated, or are designated incorrectly, the standard arguments of 9600 baud, no parity, eight data bits, and one stop bit is used.
nBuff_size := 4000 // 4000 character buffer lComOk := COM_OPEN(1, nBuff_size) // Open port IF lComOk lInitOk := COM_INIT(1, 1200, "N", 8, 1) IF .NOT. lInitOk ? "Port cannot be initialized !" ENDIF ENDIF
ITK,Paul
No dependies of platform.
nMaxCom := COM_NUM()
No arguments
Return the number of the highest available serial interface port
This function returns the number of the serial interface port available. This is not to say that a return value of 3 necessarily means that ports 1 through 3 are available; for example, COM2 could be missing. Whether or not a serial port can actually be used can be tested using the COM_OPEN() or COM_INIT() functions.
? COM_NUM() // e.g. 4
ITK,Paul
No dependies of platform.
lStatus := COM_OPEN(<nComPort>,<notused>,<notused>,<notused>, [<lRTSCTS>],[<lXONXOFF>],[<lDebug>])
Returns .T. when the port has been successfully opened and you can select the buffer.
This function allows you to open a serial interface port.
IF COM_NUM() > 0 ComOk := COM_OPEN(1) // Open COM1 IF .NOT. ComOk ? "Port cannot be opened !" ENDIF ENDIF
ITK,Paul
No dependies of platform.
cData := COM_READ(<nComPort>,[<nLength>])
The returned string contains the corresponding characters from the receiving buffer.
All characters arriving at the serial port are stored in the appropriate buffer under interrupt control. It is possible to determine the number of characters in the buffer and to read one, several, or all of the characters. If the <nLength> parameter is not specified, then all the characters in the buffer are read.
Remember that you can receive additional characters between calling COM_COUNT() and COM_READ().
DO WHILE COM_COUNT(1) < 10 // Wait until 10 characters in buffer *... ENDDO cInput := COM_READ(1, 10) // Read in 10 characters ? cInput
ITK,Paul
No dependies of platform.
lOldRTSStatus := COM_RTS(<nComPort>,[<lNewRTSStatus>])
<nComPort> | - Designates the port (1 to N) for which the number of characters in the input buffer is determined. |
<lNewRTSStatus> | - Designates the status of RTS. .T. designates |
that the RTS signal is active, and .F. designates that the signal is | |
inactive. If the parameter is not specified, does not affect the RTS | |
exit signal. |
The returned value corresponds to the RTS signal status prior to the new setting, or in the absence of <lNewRTSStatus>, the returned value corresponds to the current RTS. A return of .T. indicates that the RTS is active (MCR bit 1 = 1). A return of .F. indicates that the RTS is inactive (MCR bit 1 = 0).
COM_RTS() queries the RTS signal status. If the second parameter is not supplied, the signal status does not change.
The COM_CLOSE() function resets RTS (to inactive). By contrast, COM_OPEN does not automatically make RTS active!
lComOk := COM_OPEN(1) // Open COM1 IF lComOk COM_RTS(1, .T.) // Activate RTS Port 1 * ... ENDIF
ITK,Paul
No dependies of platform.
nLength := COM_SEND(<nComPort>,<cString>)
When transmission is successful, a 0 is returned. If transmission is not successful, the number of characters that have not been transmitted or could not be placed in the sending buffer is returned.
Transmits one or more characters from one of four ports.
COM_OPEN(1) // COM_HARD(.T.) // Hardware handshake ACCEPT "Please input name " TO cInput nRest := COM_SEND(1, cInput) // 1st attempt to transmit DO WHILE nRest > 0 cInput := RIGHT(cInput, nRest) // Characters not yet sent nRest := COM_SEND(1, cInput) ENDDO
No dependies of platform.
lDeleted := COM_SFLUSH(<nComPort>)
On success returns .T..
This function remove all characters from sending buffer not yet transmitted.
cHayes := "ATZ" // Hayes modem reset command COM_SEND(1, cHayes) // Issue Hayes command INKEY(1) // Wait one second COM_SFLUSH(1) // Reject modem reply messages
No dependies of platform.
lOldHandshake := COM_SOFT(<nComPort>,[<lNewHandshake>],[<cXONchar>],[<cXOFFchar>])
<nComPort> | - Designates the port (1 to N) for which the software handshake is set. |
<lNewHandshake> | - Designates whether the handshake is on (.T.) or off (.F.). If this parameter is omitted, the function returns the current setting. |
<cXONchar> | - Designates any character you choose as an XON character. The default character is ASCII 19 - Ctrl-S. |
<cXOFFchar> | - Designates any character you choose as an XOFF character. The default value is ASCII 17 - Ctrl-Q. |
The function returns the previously set value.
With modem connections, a hardware handshake is impossible, which is why we are making a software handshake available with this function. When a buffer is 75% full, an XOFF character (Ctrl-Q) is transmitted to the remote station. As soon as the buffer has again been emptied to 50% or less, the XON character (Ctrl-S) is transmitted. You can use characters other than Ctrl-S or Ctrl-Q for individual protocols, which would then be taken into account in the situations described above.
When you want to transmit binary data, the software handshake must always be turned off.
COM_OPEN(1) // Open port COM_SOFT(1, .T.) // Software handshake COM1
No dependies of platform.
nOldTimeout := COM_TIMEOUT(<nComPort>[,<nTimeout>])
Return previous value of operation timeout.
All read functions may use a timeout mechanism while waiting for characters. If this time limit is exceeded the function abort reading.
The timeout duration is specified by the parameter TenthOfSeconds. This value is given as a multiple of a thenth of a second.
COM_OPEN(2) COM_TIMEOUT(6000)
No dependies of platform.