Click here to Skip to main content
15,846,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi People,

I was wondering if any of you could lend insight on a particular problem of mine, so let me describe the situation.

I use a RF-Dongle that does serial emulation, and I use the Serial Port Data Received Event to cater for any transmissions that occur and store it in an "Inbox" buffer

I have a 5ms timer event, I even have got it down to 1ms using the High Resolution Timer, that detects if there is a message in the "Inbox" and then responds to that message.

The problem arises when I send Data over the serial Port. I send packet sizes of max length 64bytes and on my receiving Device receives them every 15ms. This is the problem, the protocol defined requires 5 ms or less. I know the 15ms thing sounds similar to the Drawback of System.Net.Timers and I've read that they can only give you 15ms, However when I use SerialPort.write instead of SerialPort.writebytes the time taken is 6ms for a transmission and not 15.

My question is is there a way to speed this up, I do not want to break up my packet and transmit. Is this a timer problem or a Serial Port problem

Advice needed,

Thanks
Posted
Comments
Romain TAILLANDIER 13-Dec-11 3:18am    
It is not clear if your 5ms constraint is
- between start of frame and end of frame, OR
- between end of reception and start of emission ....

If i do not mistake, your have 2 delay :
- Time for react to a reception
- Time a write transmition

Time to write 64 bytes @115200 Baud (115200 bit/seconds)
should be 512/115200 = 0.0044 sec = 4.4ms, so you have less than one ms to create the reply ...

If your RF dongle is USB, you probably have a usb-serial converter inside. So your speed should be only limited by usb speed AND driver of the USB-COM converter. That way you (usually) should be able to set up the BaudRate to 115200*8 (and more for USB2), but i believe Framework is limited to 115200*8.

Now to speed up you time of react, it really depend on your implementation.
How do you measure the time of your reaction ?
I have notice that in debug, Console.WriteLine and Debug.WriteLine are really slow down all times i can measure.

Romain TAILLANDIER
codenameyash 13-Dec-11 3:27am    
Thanks for the reply, well the constraint is once I receive a message, I need to reply within 5ms or all communications fail.

You are correct with respect to the USB dongle, and it does have a converter inside. The Baudrate of the device is configurable and it is set at 115200.

To measure the reaction time, I have the receiving device toggle an LED when something is received over RF, and measure this toggle rate on an Oscilloscope. I have noticed it comes to 15ms. Another test that I have run was pre-loading a byte array and sending this byte array over the serial port constantly but to no avail the 15ms time still prevailed.
Romain TAILLANDIER 13-Dec-11 7:28am    
Start by trying to set the baud rate to 115200*8. you will gain a little during the transmition. according to your 5ms, even one 0.5 ms is a good gain ...

You can try to set your reading and writing threads to higher priority.
the 15ms may contains the delay used by windows to give some processor time to your process.

I can't understand why you need timers of 5ms ?

The better way to get the better time reaction is to reply in the same function as you receive like
public void SerialPort_Receved(..)
{
SerialPort.read(buffer,0,SerialPort.ByteToRead);
byte[] reply = this.BuildMyReply(buffer);
SerialPort.Write(reply,0,reply.Lenght);
}
if you use an additional timer, it should be less fast.

Romain TAILLANDIER

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900