Friday, July 25, 2008

c# serial port data download

I recently posted a link to very good tutorial on communication with serial port using .Net c#.

In terms of the structure, the tutorial demonstrate the way to send command, and register SerialDataReceivedEventHandler
to handle DataReceived event.

The point I want to make is because it is a separate thread, if you wanted to change UI in that thread, Invoke delegate functions.
In addition, communication with serial port will involve CRC checking. So, before you do any thing to the data, check it.
Normally, you might need to send multiple commands to the port. You should send it by sequence, and send it before the previous command is handled. For example, check all the data is correctly received. If there are missing or error data, or even timeout, Resend the request command again. Finally, If you wanted to save data onto disk, You don't have to use asynchronous method, because the data received is very small, and in Windows, you won't enhance the performance of IO if your data block is less than 64K.

The final trick, don't use Thread.Sleep() in your GUI. Since you need to wait until the previous command is handled, doesn't mean you have to stuck in an infinite loop, or call Sleep to hang your GUI. You can simply create a timer to perform the checking and sending.


1 comment:

Mila said...

This was lovely thanks for sharing