热度 20
2009-5-15 23:36
6747 次阅读|
2 个评论
四、 RS-232 Library 串口函数库 1 . int OpenCom (int portNumber, char deviceName 串口名称。 portNumber deviceName under Windows 1 "COM1" 2 "COM2" 3 "COM3" 4 "COM4" 返回值:如果返回为负值表示有错误!否则成功打开! 2 . int OpenComConfig(int portNumber, char deviceName 串口名称,一般为空字符串; long baudRate 波特率; int parity 奇偶校验; int dataBits 数据位数; int stopBits 停止位; int inputQueueSize, int outputQueueSize 输入输出队列最大长度。 返回值:为“ 0 ”开启成功,否则失败。 3 . int CloseCom (int portNumber); 函数说明:关闭串口。 4 . int ComRd (int portNumber, char buffer , int count, int terminationByte); 函数说明:从串口中读取数据,当读到的数据与 terminationByte 一样时,或是读到的数据个数达到 count 个时,停止读。 返回值:返回读取的个数。 7 . int ComToFile (int portNumber, int fileHandle, int count, int terminationByte); 函数说明:从串口中读取数据,然后将数据写入文件中。当数据个数达到 count 个,遇到 terminationByte 时或是超时或错误发生时停止读数据。 返回值:数据的个数。 8 . int ComWrt (int portNumber, char buffer ); 函数说明:从串口中接受一个信息包,然后写到文件 filename 中。必须遵循 Modem 协议。 返回值:为负值表示错误,否则操作成功。 12 . int XModemSend (int portNumber, char fileName[]); 函数说明:将文件中的数据传输到串口中。 返回值:为负值表示错误,否则操作成功。 13 . int XModemConfig (int portNumber, double startDelay, int maximumNumOfRetries,double waitPeriod, int packetSize); 函数说明: Modem 设置函数。 14 . int SetComTime (int portNumber, double timeoutSeconds); 函数说明:设置串口的超时时间。 返回值:为负值表示错误,否则操作成功。 15 . int SetXMode (int portNumber, int mode); 函数说明: 在传输和接收的数据时间,启用或禁用 XON / XOFF ,或启用或禁用软件握手。 int portNumber 串口号; int mode 选择串口的模式(“ 0 ”禁止“ 1 ”启用); 返回值:为负值则有错误发生,否则设置成功。 16 . int SetCTSMode (int portNumber, int hardwareHandshaking); 函数说明:设置硬件握手模式。 int portNumber 串口号; int hardwareHandshaking 握手方式 Value Defined Constant Description 0 LWRS_HWHANDSHAKE_OFF Hardware handshaking is disabled. The library ignores the CTS line, and raises the RTS and DTR lines the entire time the port is open. 1 LWRS_HWHANDSHAKE_CTS_RTS_DTR Hardware handshaking is enabled. The library monitors the CTS line and uses the RTS and DTR lines for handshaking. 2 LWRS_HWHANDSHAKE_CTS_RTS Hardware handshaking is enabled. The library monitors the CTS line, uses the RTS for handshaking, and raises the DTR line the entire time the port is open. 返回值: 为负值则有错误发生,否则设置成功。 17 . int FlushInQ (int portNumber); 函数说明:清空串口输入队列的数据。 返回值: 为负值则有错误发生,否则清除成功 18 . int FlushOutQ (int portNumber); 函数说明:清空串口输出队列的数据。 返回值: 为负值则有错误发生,否则清除成功 19 . int ComBreak (int portNumber, int breakTimeMsec); 函数说明:产生中断信号。 int portNumber 串口号 int breakTimeMsec 产生中断信号的的周期。多少毫秒产生一次。 返回值:如果没有打开串口,或设置了一个错误的参数时,函数返回一个错误。 20 . int ComSetEscape (int portNumber, int escapeCode); 函数说明:设置串口扩展命令, int escapeCode 表示扩展函数代码 21 . int GetComStat (int portNumber); 函数说明:得到串口的状态。 返回值:状态代码。如下表 Hex Value Mnemonic Description 0001 INPUT LOST Windows Input queue filled and input characters lost; you did not remove characters fast enough. 0010 PARITY 奇偶检测错误。 0020 OVERRUN Windows and RT Overrun error detected; a character was received before the receiver data register was emptied. 0040 FRAMING Framing error detected; stop bits were not received when expected. 0080 BREAK Break signal detected. 1000 REMOTE XOFF Windows Transmission is in a suspended state because an XOFF character was received from the remote device. No characters will be removed from the output queue and sent to the device until an XON character is received. Note that this status is only returned if the port has enabled XON/XOFF handshaking. Refer toSetXMode for more information about handshaking. 4000 LOCAL XOFF Windows The driver sent an XOFF character and is waiting for the input queue to fall to 10% of its capacity. XOFF is transmitted when the input queue reaches 90% of its capacity. If the other device is sensitive to XON/XOFF protocol, it transmits no more characters until it receives an XON character. You use this process to avoid the INPUT LOST error. 22 . unsigned int GetComLineStatus (int portNumber); 函数说明:得到 modem 控制寄存器的内容。 返回值: Name Value Description kRS_CTS_ON 0x10 The CTS (clear-to-send) signal is on. kRS_DSR_ON 0x20 The DSR (data-set-ready) signal is on. kRS_RING_ON 0x40 The ring indicator signal is on. kRS_RLSD_ON 0x80 The RLSD (receive-line-signal-detect) signal is on. 23 . int GetComConnectionState (int portNumber); 函数说明:得到串口的连接状况。 返回值: V.alue Description 0 No connection on this port. 1 Valid connection on this port. 24 . int GetInQLen (int portNumber); 函数说明:得到串口输入队列中的数据长度。 返回值:数据的长度。 25 . int GetOutQLen (int portNumber); 函数说明:得到串口输出队列中的数据长度。 返回值:数据的长度。 26 . int ReturnRS232Err (void); 函数说明:返回错误代码。 27 . char *GetRS232ErrorString (int errorNum); 函数说明:用于得到串口错误码的字符串描述; 其中errorNumber为ReturnRS232Err得到的错误码。 28 . int InstallComCallback (int portNumber, int eventMask, int notifyCount, int eventCharacter, ComCallbackPtr callbackPtr, void *callbackData); 函数说明: 安装一个同步回调函数为特定的 COM 端口。 int portNumber 串口号 int eventMask 如下表: Bit Hex Value COMPortEvent Constant Name Description 0 0x0001 Any character received. LWRS_RXCHAR Set when a character is received and placed in the input queue. 1 0x0002 Received certain character. LWRS_RXFLAG Set when the event character is received and placed in the input queue. The event character is specified in eventCharacter . 2 0x0004 Transmit queue empty. LWRS_TXEMPTY Set when the last character in the output queue is sent. 3 0x0008 CTS changed state. LWRS_CTS Set when the CTS (clear-to-send) line changes state. 4 0x0010 DSR changed state. LWRS_DSR Set when the DSR (data-set-ready) line changes state. 5 0x0020 RLSD changed state. LWRS_RLSD Set when the RLSD (receive-line-signal-detect) line changes state. 6 0x0040 BREAK received. LWRS_BREAK Set when a break is detected on input. 7 0x0080 Line status error occurred. LWRS_ERR Set when a line-status error occurs. Line-status errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY. 8 0x0100 Ring signal detected. LWRS_RING Set to indicate that a ring indicator was detected. 15 0x8000 notifyCount bytes in input queue. LWRS_RECEIVE Set to detect when at least notifyCount bytes are in the input queue. Once this event has occurred, it does not trigger again until the input queue falls below and then rises back above notifyCount bytes. int notifyCount 在发送 LWRS_RECEIVE 事件到回调函数中时,输入队列中至少有多少个字节数。 int eventCharacter 触发 LWRS_RXFLAG 事件时的字或字节的值。( 0-255 ) ComCallbackPtr callbackPtr 回调函数; void *callbackData 一个 4 字节的值。 返回值: 例子: notifyCount= 50; /* Wait for at least 50 bytes in queue. */ eventChar = 10; /* Wait for LF. */ eventMask = LWRS_RXFLAG | LWRS_TXEMPTY | LWRS_RECEIVE; InstallComCallback (portNumber, eventMask, notifyCount, eventChar, ComCallback,NULL); /* Callback Function */ void ComCallback(int portNumber, int eventMask, void *callbackdata) { if(eventMask & LWRS_RXFLAG) printf("Received specified character\n"); if(eventMask & LWRS_TXEMPTY) printf("Transmit queue now empty\n"); if(eventMask & LWRS_RECEIVE) printf("50 or more bytes in input queue\n"); } 29 . int GetSystemComHandle (int portNumber, int *systemHandle); 函数说明:得到一个串口和系统的一个句柄。