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

I have a peculiar problem, I have a serial Port event that fires for a few instances and then just doesnt work afterwards. The setup is as follows, I have a Usb RF dongle plugged in that emulates the Serial Port and that is what I use to interface to my program.

It isn't the Dongles fault, as the Dongle has an indicator light that flashes when RF is received, and When I close my program when I see the Serial Event Handler is dead, and Connect the Same dongle to Docklight, I see data coming through.

I have seen quite a few proposed solutions, even one on this forum about a Bug with .Net 2.0 with the Serial Event handler, that person suggested using the Readbyte method which resulted in the Serial Port event waking up again.

However this did not work for me. Below is the code that I am using.

C#
SP.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort_DataReceived);//How I assign the handle

public void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
       {
           WriteToText(Environment.NewLine + "Entered Serial Handler" + Environment.NewLine);
           this.BeginInvoke(new EventHandler(ReceiveHandler));

       }

       public void ReceiveHandler(object s, EventArgs e)
       {

               ReadBufferByte = new byte[SP.BytesToRead];
               int BytesSent = SP.Read(ReadBufferByte, 0, ReadBufferByte.Length);
               if (BytesSent != ReadBufferByte.Length) MessageBox.Show("ERROR: Not all bytes read from the buffer!");

               Stacked.AddToInbox(ReadBufferByte,ReadBufferByte.Length);

       }



Any help is greatly appreciated.
Posted
Updated 9-Feb-12 21:10pm
v2

1 solution

From the first glance, the only reason is that the event does not fire, and this is because SP does not receive anything. In such cases, a lot depends on what's going on on the other end of your RS-232 connector, in your case, in a dongle. You did not show the dongle specs, the communications you send to it, so who knows. Read documentation, experiment, but do something which the dongle expect. And, first of all, use the Debugger to see what exactly is going on.

Besides, I would recommend you to work without this event. This way is more straightforward, easier to implement, and, importantly, to debug. Do all serial communications in a separate thread, and makes all the code sequential, without all those asynchronous methods. As the communication is in a separate thread, you can allow for blocking calls. Put a break point; and you will see when some data is received and what it is not.

—SA
 
Share this answer
 
Comments
codenameyash 10-Feb-12 4:01am    
Hi, thanks for reply, the Dongle is a piece of hardware that we produce here, Using Microchips controller to interface RF over the Usb, it is one of their Drivers that is in use. Well I was trying to use the debugger, but to no avail as once the Serial Event stops working the breakpoints I have in wont fire. The strange thing to me is that when it works it works well, but when it fails it just doesn't work at all.
I will follow up on the threads Idea. Blocking calls? Not sure what you mean by that...

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