Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

LabWindows/CVI Serial Communication

Serial Communication Programming

The RS232 library contains many functions that handle all aspects of serial communication. The most important functions are the open, close, read, and write. These functions are described below.

  • Opening and Closing COM Ports
    Before using a serial function to communicate with your instrument, you must open a session to your COM port using OpenComConfig:

    	int OpenComConfig
    	(int COM_Port, char Device_Name[],long Baud_Rate,
    		int Parity, int Data_Bits,
    		int Stop_Bits, int Input_Queue_Size,
    		int Output_Queue_Size);
    

    This function specifies the baud rate, data bits, stop bits, parity, and buffer size. CVI allows you to specify a buffer in memory to store contents of the input queue and output queue. It is not necessary to use the OpenComConfig function. By using OpenCom, the default parameters in the portÂ’s configuration utility are used.

    Use the CloseCom function to release computer resources used by your serial port session. To guarantee that all bytes were written before closing the port, monitor the output queue with the GetOutQLe function. When the queue length equals zero, it is safe to close the port.

  • Writing to COM Port
    Write data to a device from a buffer using:

    	int ComWrt (int COM_Port, char Buffer[], int Count);
    

    ComWrt writes data from memory to the serial output queue of the determined COM Port. The count specifies the number of bytes to write. Strlen (ANCI C Library) determines the size of a buffer. The value returned from strlen can be used to specify the number of bytes to be written.

  • Reading from COM Port
    Read data from device using the function:

    	int ComRd (int COM_Port, char Buffer[], int Count);
    

    ComRd reads a specified number of bytes from the determined COM Port. Use the function GetInQLen to obtain the number of bytes present at the serial input queue. This value can provide input for the number of bytes to read.

  • Using InstallComCallback with Serial Reads
    Consider a situation in which an instrument generates a large amount of data, or takes a long time to obtain a data point. The instrument needs to send data to the computer as soon as the data is acquired. Because you do not want to tie up computer resources waiting for ComRd to complete, and you do not want to make the serial input buffer extremely large, you must be notified of when data is present at the port. This keeps you from waiting on data, and it enables you to periodically read from the port.

    InstallComCallback allows you to install a callback function for a particular COM port:

    	int InstallComCallback
    		(int COM_Port, int Event_Mask, int Notify_Count,
    		 int Event_Character,
    		 ComCallbackPtr Callback_Function,
    		 void *Callback_Data);
    

    The callback function is called whenever any of the events specified in the EventMask parameter occur on the COM port.



Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality?   Answered Your Question? 
  1 2 3 4 5
Please Provide Additional Feedback below
Please visit Request Support for product support inquiries. submit