新闻  |   论坛  |   博客  |   在线研讨会
LabWindows学习笔记(3串口)
mayer | 2009-05-18 18:51:31    阅读:3984   发布文章

LabWindows学习笔记(3串口)

st1\:*{behavior:url(#ieooui) }

四、RS-232 Library串口函数库

1.int OpenCom (int portNumber, char deviceName[]);

函数说明:串口开启。

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

函数说明:开启串口,并设置参数。

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

函数说明:从串口中读取count个数据存储在buffer中。int portNumber为串口号。

返回值:返回读取的字节个数。即一般出现错误时为count。

5.int ComRdByte (int portNumber);

函数说明:从串口中读一个字节。int portNumber为串口号。

返回值:返回的是一个整型数据,它的低8位为读到的字节。

6.int ComRdTerm (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[], int count);

函数说明:向串口写count个字节。

返回值:写的数据个数。

9. int ComWrtByte (int portNumber, int byte);

函数说明:向串口写一个字节。写的数据虽然是整型的,但是实际写到串口中的是byte的低8位。

返回值:写的数据的个数。

10.int ComFromFile (int portNumber, int fileHandle, int count, int terminationByte);

函数说明:将文件中的数据写如串口,当数据个数达到count个,遇到terminationByte时或是超时或错误发生时停止读数据。

返回值:写的数据的个数。

11.int XModemReceive (int portNumber, char fileName[]);

函数说明:从串口中接受一个信息包,然后写到文件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);

 

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 to SetXMode 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);

函数说明:得到串口的连接状况。

返回值:

Value

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

28.int InstallComCallback (int portNumber, int eventMask, int notifyCount, int eventCharacter, ComCallbackPtr callbackPtr, void *callbackData);

函数说明:安装一个同步回调函数为特定的COM端口。

int portNumber串口号

int eventMask如下表:

Bit

Hex Value

COM Port Event

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

函数说明:得到一个串口和系统的一个句柄。

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客