UART Transmission using Threads in C#


We are using Windows CE 7 OS for the one of our project and developing our application using windows forms in C#. We have this case now. Our application is using MODBUS protocol through UART collecting data from the modbus slaves.
Our application acts as modbus master , which sends command and slave in turn will respond with the data. Now this we implemented using C# in windows form. For receiving data we have used event handler. But for the transmission there was little more effort to workout since we never found when not events are executing where exactly the control lies. (We are new bees to the C# (Windows forms) , But having enough experience in developing embedded application using C for microcontrollers) . Then we came with solution to use timer of minimum resolution say 5ms. So whatever we wanted to execute continuously we will put in that segment . Basically we are polling flags and doing actions based on that. Now we have go to know the concept of threading because the above method consumes most CPU cycles. We implemented same algorithm using threads. We have put while(true) inside the thread so that thread will last till the end , all the polling activities which we were doing in 5ms segment we are doing in while(true){} of thread. But now everything has slowed down. Initially on the form load if we start the thread loading of form is taking a lot of time, so we delayed creating of thread by 2secs after the form load. But the serial transmission is taking lot of time. as soon as it transmits it receives response but the next transmission is taking time. We not not able to debug this problem . How do we proceed to solve this?

Board: Iris Carrier board
SOM : Colibri T20
OS : Windows embedded compact 7
Language : C#
IDE: Visual studio 2008

“It worked”

Thank you for the immediate solution. if you don’t mind Can you take your time to explain us what was the exact problem. Does every continuous thread needs sleep call ? if so why ?

This is not the best solution but not knowing exactly how your code works I can not propose better solution.

By calling Sleep 1 you give system some time to handle also other things not just loop your thread.

Inside your while(true) loop you should always have some sleep. Try adding

System.Threading.Thread.Sleep(1) inside that while loop.