简体   繁体   English

仅当从另一侧发送数据时才从串行端口读取(C ++)

[英]Read from serial port only when data is send from other side (c++)

I am doing serial communication in win32. 我正在用win32进行串行通讯。 I have used separate thread to write and read. 我使用了单独的线程来读写。 This is because i have to continuously send the data till the application is running without affecting other part of the program and I have to continuously read the data from the serial port. 这是因为我必须连续发送数据,直到应用程序运行为止,而又不影响程序的其他部分,而且我必须不断地从串行端口读取数据。

The main function (WINAPI WinMain) has 主要功能(WINAPI WinMain)具有

_beginthread(serialFunctionSend,0,(void*)12); // start new thread for send (write)
_beginthread(SerialFunctionReceive,0,(void*)10);//start new thread for receive(read)

the send function is continuously sending the data. 发送功能不断发送数据。 My problem is in the receive function. 我的问题是在接收功能。 It can receive the data. 它可以接收数据。 But I am wandering how to check whether data has been received or not . 但是我在徘徊如何检查是否已接收到数据。 In other words, how to check our system has received any data. 换句话说,如何检查我们的系统是否已收到任何数据。 I have to perform some task only when something is received not when we donot receive anything at the port. 我仅在收到某些内容时才执行某些任务,而不是在端口未收到任何内容时执行。 So i have to exclude the condition when program has not received anything. 因此,当程序未收到任何内容时,我必须排除条件。

My "SerialFunctionReceive" code is 我的“ SerialFunctionReceive”代码是

void SerialFunctionReceive(void * arg)
{
char inBuffer[BUF_SIZE];
while (statusRead ==true)
{
DWORD nBytesRead = serialObj.ReadTest(inBuffer, sizeof(inBuffer));
}
}

Can we do this by checking the value of inBuffer as read data is stored in it. 我们可以通过在读取的数据存储在其中时检查inBuffer的值来做到这一点吗? How can we check that inBuffer has some value or not. 我们如何检查inBuffer是否有价值。 Or is there is other options to check whether read event has taken place . 还是有其他选项可以检查是否发生了读取事件。

you can poll the buffer. 您可以轮询缓冲区。

DWORD nBytesRead = serialObj.ReadTest(inBuffer, sizeof(inBuffer));
if (nBytesRead == 0)
{
   //no data
}
else
{
   //do something
}

I guess you need to do this in a while loop since you never know when you get new data. 我猜您需要在while循环中执行此操作,因为您永远不知道何时获取新数据。

First of all, you have to make sure your serial port is opened as an Overlapped I/O in order to send and to receive at the same time. 首先,您必须确保将串行端口作为重叠的I / O打开,以便同时发送和接收。 If you did so, the WaitForSingleObject() is useful for you. 如果这样做,则WaitForSingleObject()对您很有用。

OVERLAPPED ov = {0};
ov.hEvent = CreateEvent(NULL, true, false, NULL);
DWORD dwEvtMask;
WaitCommEvent(hSerialPort, &dwEvtMask, &ov);
WaitForSingleObject(ov.hEvent, INFINITE);

Where hSerialPort is the return value of CreateFile() who opened your serial port. 其中hSerialPort是打开串行端口的CreateFile()的返回值。 The program will be blocked at the last line before any data comes in. So you don't have to poll it all the time. 在输入任何数据之前,该程序将在最后一行被阻止。因此,您不必一直轮询它。

See CreateFile for more detail. 有关更多详细信息,请参见CreateFile

And you may be interested in this page . 您可能对此页面感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM