ComWrt
- Updated2023-02-21
- 3 minute(s) read
int ComWrt (int portNumber, char buffer[], size_t count);
Purpose
Writes count bytes to the output queue of the specified port.
ComWrt returns an integer value that indicates the number of bytes placed in the queue. In an output buffer has been configured for the port, this function returns immediately after buffering the data, without waiting for the bytes to be sent out of the serial port. If the output queue has been disabled, this function does not return until the transfer completes.
ComWrt times out whenever the library does not write any bytes from the output queue to the COM port during an entire timeout period. This action can occur if you enable XON/XOFF, the device sends an XOFF character without sending the follow-on XON character, and the output queue is full. The timeout also can occur if you enable hardware handshaking and the CTS line is not asserted. On a timeout, ComWrt returns the number of bytes actually written and sets rs232err to -99.
ComWrt sends bytes from the output queue to the serial device under interrupt control without program intervention. If you close the port before all bytes are sent, you lose the bytes that remain in the queue. To guarantee that all bytes are removed from the output queue before you close the port, call GetOutQLen. GetOutQLen returns the number of bytes that remain in the output queue.
ComWrt returns an error if you have not opened the port or if you pass an invalid parameter value.
Example Code
/* Place the string "Hello, world!" in the output queue of */
/* COM2 and check if operation was successful. */
if (ComWrt (2, "Hello, World!", 13) != 13)
/* Operation was unsuccessful. */;
or
char buf[100];
Fmt(buf,"%s","Hello, World!");
if (ComWrt (2, buf, 13) != 13)
/* Operation was unsuccessful. */;
Parameters
Input | ||
Name | Type | Description |
portNumber | int | A number that indicates the COM port on which to operate. This number maps to the COM port specified by deviceName in the call to OpenCom or OpenComConfig. The portNumber 1, for example, may not necessarily map to COM1. (Linux) The portNumber 1, for example, may not necessarily map to /dev/ttyS0. Valid Range: 1—1,000 |
buffer | char [] | A buffer containing the data to write to the selected port. |
count | size_t | The number of bytes to write to the selected port. An error is returned if you pass a value greater than INT_MAX. |
Return Value
Name | Type | Description |
BytesWritten | int | The actual number of bytes written to the output queue. If a timeout occurs, the return value is the number of bytes written. If any other error occurs, the return value is a negative error code. |
Additional Information
Library: RS-232 Library
Include file: rs232.h
LabWindows/CVI compatibility: LabWindows/CVI 3.0 and later