串口读写

sysUartRead

  • 函数

int sysUartRead(int nSerial, char *rxBuf, int len)
  • 描述

从指定串口读取数据。注意,在接收对方设备发送的串口数据时,通常不是一次性的,而是断断续续的,因此通常调用该函数接收数据时,要做缓冲处理。

  • 参数

nSerial                 串口编号,从1开始。
rxBuf                   用于存放从串口接受的数据。
len                     缓冲大小。
  • 返回值

读取的字节数,0表示没有收到数据。

sysUartWrite

  • 函数

int sysUartWrite(int nSerial, char *txBuf, int len)
  • 描述

把数据写入指定串口。

  • 参数

nSerial                 串口编号,从1开始。
txBuf                   用于存放需要发送的数据。
len                     所要发送的数据长度。
  • 返回值

写入数据的字节数,0表示没有写入。

sysUartFlush

  • 函数

void sysUartFlush(int nSerial, int len)
  • 描述

删除串口接收到并存于缓冲中的数据。

  • 参数

nSerial                 串口编号,从1开始。
len                     从缓冲中最早接收到的的数据开始,删除串口接收缓冲的数据长度,
                        如果长度 <= 0,则删除所有该串口缓冲中接收到的数据。
  • 返回值

无。

例子:跟UPS通讯

完整源代码