![]() |
| ||
| reading from serial port way too slow (C++) Hey, I'm having a problem reading from a device attached to my serial port. I have a big application which uses a thread to read from the serial device and writes the data to a log file. The device is set to transmit data at 500Hz, which means that my thread receives data of 14 bytes every 2 miliseconds. The baud rate is set to 115200 and actually everything is working pretty fine accept it takes too long. I'm using a polling method, meaning I check if there are enough bytes in the buffer and I read them. After the device stops transmitting I read from the buffer until it's empty and the thread ends. The problem with this is that I'm constantly polling the buffer which results in 100% CPU usage. Since we're talking about a thread which is a part of a much bigger application this CPU business is out of the question. Thus, I'm forced to use the Sleep() function. And here the problem begins:
Minutes are not the timing I had in mind when I wrote this thread. I tried to change the thread priority but it didn't work. I also tried to use event driven programming but it's too slow for my thread. I could really use your help, any suggestion would be welcomed. Thank You, Gadi |
| ||
| Re: reading from serial port way too slow (C++) I'm not very experienced with threads in C++, but from what you said, it shouldn't make a difference in the amount of time it takes to empty the buffer. Have you tried it on another computer? And how long are you setting it to sleep for? Also, some code might help. Nick |
| ||
| Re: reading from serial port way too slow (C++) Hi, I didn't try on another computer yet. The Sleep() function is set to 1 milisecond. It is the minimum, so I tested to see if a "longer" sleep would help me - it didn't, it took longer. Here's the thread: (I'm using a SerialCom object that somebody else wrote. The function ReadString() uses ReadFile() and returns FALSE if operation didn't succeed. Also, I used AfxBeginThread() to start this thread) unsigned int CRmi_demoDlg::GetADISData(void *Sender) |
| ||
| Re: reading from serial port way too slow (C++) Another development: If I set the following conditions I am able to run the entire application with 62% CPU usage (where without the above thread the main application uses 52%): SetPriorityClass(HIGH_PRIORITY_CLASS); SetThreadPriority(THREAD_PRIORITY_HIGH); And I set the Sleep() argument to be 0.01 - meaning I call it like this: Sleep(0.01). If I don't set the priorities and only use the Sleep(0.01) it doesn't work. If I only set the priorities but use Sleep(1) it doesn't work. I don't know how it is possible that the Sleep() function receives a float argument since it should get DWORD arguments, but it does. I get a warning that I am sending a float instead of DWORD so I gather that a casting from float to DWORD occurs, however I tried to use Sleep(0) and it gave me 90% CPU usage. This is still not the solution I am seeking. Since the high priorities may affect the performance of the rest of the application, and since the purpose of the application is to control a small vehicle, I wouldn't want my small thread to cause an accident. P.S. I've changed the thread creation to CreateThread() instead of AfxBeginThread() so that I could get a HANDLE on the thread. |
| ||
| Re: reading from serial port way too slow (C++) Another approach to consider. http://msdn.microsoft.com/en-us/libr...83(VS.85).aspx |
| ||
| Re: reading from serial port way too slow (C++) Quote:
I'll try it on Monday, report to you then :icon_wink: |
| ||
| Re: reading from serial port way too slow (C++) okay, just wanted to let you know that I've worked things out. Your tips were helpful so thank you very much. I'll explain what I did so that others with a similar problem would try it. Instead of trying to capture every data package that is sent, I waited 'till there were 5 packages and then read all of them into my working buffer (which was basically an array of chars with the appropriate size). After I rewrote my thread to work like this, the CPU usage dropped down to almost none. I tried using my thread reading a single package at a time, and it worked great! So, again, thanks everyone And have a good and happy new year. |
| All times are GMT -4. The time now is 6:52 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC