none
how to correctly communicate using rs232?

    Question

  • Hi,

    No one seems to know my problem so I'm asking to see if anyone knows how to properly communicate between computer and external device.

    The protocol is IO and typical settings on both ends, i get that part as its the same as winforms. What is diffrent in WPF 4.0, what else do I have to do so a command could be sent down the comport line to the external device???

    The command to be sent down is for example "!GV 1\r".

    With my code the comport communication is setup so its the same as the external device, but nothing is sent down the line so im obviously missing somthing and there is nooo documentation on this that can help me and that is why I am asking here but i dont seem to get any reply from anyone!

    Is this a problem with the IO protocol itself? btw the OS is XP and its WPF 4.0 C# VS2010 .Net 4. I have an idea that it might be todo with invoking or threading of some sort but dont know how and where to do it as its new to me. In winforms C# we never had to do that!

     

    Kind regards

    Monday, May 09, 2011 6:29 PM

Answers

  • Hi, I am working on a WPF app that communicates via the .NET 4 System.IO.Ports.SerialPort class without any problems.
    I am using the serialPort.Write to send commands as well.
    You might want to first check the serial port settings required by the device i.e. these are passed to the SerialPort constructor - e.g. port name ("COM1") baud, data bits, stop bits and parity, etc.

    One gotcha with the WPF UI code is that the serial port driver in .NET will be running on its own thread, which is different to the WPF user interface thread.  So your   serialPort_DataReceived() event handler will not be able to directly manipulate the WPF user interface. But there is a simple way around this - which is to use the dispatcher ...


                // in the serialPort_DataReceived event handler, need to perform any actions on the UI thread
                Action action = () =>
                {
                    SomeStatusText = "Whatever";
                };
                Dispatcher.CurrentDispatcher.BeginInvoke(action);

    Thursday, May 12, 2011 6:14 AM

All replies

  • You could do it with Winform, and then host it in WPF, you could refer to this link:

    http://www.switchonthecode.com/tutorials/wpf-tutorial-using-winforms-in-wpf

     

     


    Stay hungry, stay foolish
    Thursday, May 12, 2011 5:07 AM
  • Hi, I am working on a WPF app that communicates via the .NET 4 System.IO.Ports.SerialPort class without any problems.
    I am using the serialPort.Write to send commands as well.
    You might want to first check the serial port settings required by the device i.e. these are passed to the SerialPort constructor - e.g. port name ("COM1") baud, data bits, stop bits and parity, etc.

    One gotcha with the WPF UI code is that the serial port driver in .NET will be running on its own thread, which is different to the WPF user interface thread.  So your   serialPort_DataReceived() event handler will not be able to directly manipulate the WPF user interface. But there is a simple way around this - which is to use the dispatcher ...


                // in the serialPort_DataReceived event handler, need to perform any actions on the UI thread
                Action action = () =>
                {
                    SomeStatusText = "Whatever";
                };
                Dispatcher.CurrentDispatcher.BeginInvoke(action);

    Thursday, May 12, 2011 6:14 AM
  • Hi Anonymousa,

    I am marking your issue as "Answered", if you have new findings about your issue, please let me know.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 17, 2011 6:26 AM