locked
Serial communication - using multiple forms RRS feed

  • Question

  • Hi guys.

    Fist some importaint info:
    Developement : Visual Studio 2017
    Language : C#
    Object : SerialPort

    Some background info:
    I am developing an application that is going to collect some data from a Serial device. I am using a Serial-to-USB device to communicate with the computer.
    The idea I have is to start the Serial communication on the main window and have a different form for configuration. And I have a 3rd form for calibration of the Serial data.

    Now for the problem:
    The main form is able to open the Serial communication. No problem.

    In the 2nd form, where I make a list of all Serial port on the computer, I am unable to close the original connection and choose a new serial port. No error messages. It simply continues to use the old Comport. If I remove the USB, however (hardware disconnect Comport), the dropdown list of Comports works fine and connectes to the port I request.

    In the 3rd form, where I am trying to send command strings back to the Serial device, I get an error message that claims that "System.InvalidOperationException: 'The port is closed.' ". This is not true. I can see data retreived on the main form.


    I have read that you are not allowed to access resources directly from another form, so the main form has public functions to manipulate the SerialPort resource.

    Tests indicate that both changing the Comport and sending data works fine from the main form, so I don't understand why the functions don't work when called from another form.

    Please help.

    I am thinking that this is simply some failure to respect object transfer between the forms, so I'm not going to post any code if not asked.

    Let me know if you need any snippets of my code for further debugging.


    Thank you so much in advance.

    Kind regards,
    Geir Moen

    Tuesday, June 19, 2018 6:46 PM

Answers

  • You are ALLOWED to manipulate data in another class, assuming it is declared "public", but it's almost always a bad idea.

    In this case, my suggestion would be that you create a separate class to own and manage the serial port, and pass that object to the subordinate classes.  That way, you know you will only have one handle to worry about.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    • Marked as answer by Geir Moen Monday, July 2, 2018 8:23 AM
    Wednesday, June 20, 2018 3:50 AM

All replies

  • You are ALLOWED to manipulate data in another class, assuming it is declared "public", but it's almost always a bad idea.

    In this case, my suggestion would be that you create a separate class to own and manage the serial port, and pass that object to the subordinate classes.  That way, you know you will only have one handle to worry about.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    • Marked as answer by Geir Moen Monday, July 2, 2018 8:23 AM
    Wednesday, June 20, 2018 3:50 AM
  • Hi Tim.
    Thank you so much for your reply.

    It makes sense to "isolate" the serial communication in a separate Class.
    I tried, but that is a lot of detail picking and a lot of work.

    I kind of went another way.
    I made the main window to auto-open 2nd form and moved the Communication Startup in this form.
    It works fine now.

    Thanx a lot though.
    I may isolate the code one day, but I have way too many things to do right now ;-)

    Wednesday, June 20, 2018 10:25 PM