Silverlight 5 test

Jan 27, 2012 at 6:40 AM
Edited Jan 27, 2012 at 8:49 AM

Hey gotthelastid,

I got a few questions regarding the wonderful package you shared here !

1) I need to access through serial COM port to a small electronic device and send simple commands such as "01,RO,00<CR>" (where CR is simply a new line).

2) I downloaded and added your sources in my Silverlight 5 test project, that has only one button.

3) This button calls a method that does this :

      SerialPort port = new SerialPort("COM4");
      port.NewLine = "\n"; // SEEMS I NEED A \r instead of \n
      port.DataReceived += new SerialDataReceivedEventHandler(onDataReceived);
      port.ErrorReceived += new SerialErrorReceivedEventHandler(port_ErrorReceived);
      port.PinChanged += new SerialPinChangedEventHandler(port_PinChanged);


4) I have a major issue :

After passing over those lines my applications ends abruptly. I only have a Windows.Exception in my VisualStudio output. No details, impossible to catch it. I insist that it's after a while that, it is coming. not during the call of port.WriteLine. I suspect incompatibility or cross thread stuff... But I can't put my finger on the source of my problems !

Edit : It seems that the call at this method, into your class SerialPort, is the source of my troubles !

public void Write(char[] buffer, int offset, int count)
// This line creates the crashes... If I comment out this line, no crash appears !
serialPort.Write(bBuffer, 0, bBuffer.Length); 

Could it be some incompatibility or missconfiguration ?

Edit 2: For validating purpose, I tested a simple console application with your other library and it's working nicely. My device is therefore not messing up anything.

Thanks in advance for your help !

Jan 27, 2012 at 9:06 AM
Edited Jan 27, 2012 at 10:29 AM

Hi Andy_CH.

Thank you for your feedback.
There are several things to consider. First , you should be aware that everytime you hit the button in your testapp it tries to create a new object of type SerialPort and connect to COM4 but you can only make one connection at a time to the serial ports of your computer. So hitting the button twice in one single run will produce an error.

You did not mention if you ever close the port. If you don't the system needs some time to automatically clean up the mess you left over. Especially with serial ports this often leads to unexpected results.

About the <CR> you need to send to your device: WriteLine(string) adds the NewLine character to the end of the string provided. If you want to submit the <CR> manually just use Write(string + "\n") or Write(string + "\r").

To clear things up: what "other library" do you mean when stating that you could successfully talk to your device via a console application?

Hint: I am not sure, if the lib works with Silverlight 5 at all as I have not tried it, yet. There were changes between SL4 and SL5 in the interop capabilities.

As mentioned in another discussion SL5 may make this lib obsolete.

Regards, Paul


Edit: fixed Link to discussion

Jan 27, 2012 at 9:25 AM

Regarding the stuff you commented out: 

this is the use of the acutal wrapper. If you comment it out no sending attempt is made. 

For easier error-handling I routed all Write()-calls through the 


Write(char[] buffer, int offset, int count)


function. You could go to SLSerialPort/SerialPort.cs and change 


public void WriteLine(string value) {
    Write(value + serialPort.NewLine);


public void WriteLine(string value) {


Then build SLSerialPort.csproj and add the DLL to you SL5-project.


Hope this will work out.




Jan 27, 2012 at 9:46 AM
Edited Jan 27, 2012 at 9:48 AM

Hey Paul,

Thanks a lot for your reply!

When I said i tested with an application console, the other library is actually your SerialPort.dll. It's not using Silverlight but it was simply to test with my device.

Now, I know clicking many times on my button can produce a crash and I avoided this. Actually, clicking once on the button is enough to reproduce the crash.

The serial connection opening looks good, the call of this WriteLine method looks allright as well because my device reacts to it (leds are switching on properly) but 1 second after the call to "WriteLine" is done, my application simply closes itself.

For debugging, I added your sources to my solution and stepped into your Write methods.

During all the calls to the different Write methods, everything is fine, except for the part in red. When I comment it out, as you said, it simply disable the sending of data and therefore, the application doesn't close. But as soon as something is sent, it closes itself quickly.

public void Write(char[] buffer, int offset, int count)
// This line creates the crashes... If I comment out this line, no crash appears !
serialPort.Write(bBuffer, 0, bBuffer.Length); 

I know I could replace your library with PInvoke in Silverlight 5 but I wanted to find out if your library could do the job.

Thanks again for your help!

P.s. Your other discussion link seems broken to me !

Jan 27, 2012 at 10:28 AM

Hi Andy_CH,

alright. The SerialPort.dll ist really just a wrapper that makes all the native


methods COM-Interop visible. I would wonder if this did not work.

Have you unblocked the DLLs, and installed/registered the COM interop properly (I guess yes, because otherwise the Open() would already crash)? abowne had similar problems with the lib (see here) and we did not manage to solve it, yet.


The problem with interop is, that debugging is really tricky as the symbols are dynamic. So it is not trivial to debug the communication between the two DLLs.

Maybe there is an encoding-problem.

Have you tried using the Write(string value) method instead of the Write(char[] buffer, int offset, int count) method as I suggested in my previous post?