Tagged: , ,

This topic contains 13 replies, has 2 voices, and was last updated by  Bitahwa Bindu 6 months, 1 week ago.

  • Author
    Posts
  • #2948

    romant
    Participant

    Hi everyone! I need a help. This is my first project using C#. I created a GUI, similar to the one published by “Student Companion” The problem I got is in transfering data in only one way. I tested my GUI the same computer with hyper termonal instolled on it. To make sure the connection is OK I tested it first on two terminals connected to different COM ports. Data flow was OK in both directions. As soon as I replaced one terminal with my GUI the data flow goes only from GUI to the other terminal and not vice versa. It looks like the Mthod: “private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)” does not generate any event, and thus the code block withion curly brackets cannot be executed.
    I appreciate any idea. Thanks.

  • #2949

    Bitahwa Bindu
    Keymaster

    If I understand you correctly you can send to serial port but you can’t receive?

    Please post your receive event code. Make sure to use code tags when you post. In the editor, under Tools menu click on Source Code to insert your code.

  • #2950

    romant
    Participant

    That is correct, I can send information from my GUI to hyper terminal with no problem.  Any information typed in terminal and sent  cannot be visible in “rich text box” of my GUI.

    • This reply was modified 6 months, 2 weeks ago by  romant.
  • #2954

    Bitahwa Bindu
    Keymaster

    You need to subscribe to the DataReceived event before opening the port, then listen to that event when triggered. You can’t just read data without knowing if data is there.
    when there is data in the buffer, the data received event is triggered.
    use something like this:

     

     

  • #2955

    romant
    Participant

    Sorry, I still have a trouble.

    What does “subscribe” mean?

    Huw it looks like in the “Here is the full project code:” (Line number?):

  • #2956

    Bitahwa Bindu
    Keymaster

    To understand Subscribe, please Google C# Event and Delegates to learn more. We can’t explain all that here as C# is not our main tutorial topic.

    When you receive data from a serial port, it’s gonna generate a receive event. You’ll have to subscribe to that event so that you can be notified when this event is raised. Then you can check if there is data to be read.

  • #2962

    romant
    Participant

    Thanks, Bitahwa. Finaly I moved forward but unfortunately ony half step. Now I started getting data from terminal. The new problem is that data I can see on MessageBox. The next line gives me message:

    “An unhandled exception of type ‘System.InvalidOperationException’ occurred in System.Windows.Forms.dll

    Additional information: Cross-thread operation not valid: Control ‘rtxtDataArea’ accessed from a thread other than the thread it was created on.”

    What are those two different threads?

  • #2963

    Bitahwa Bindu
    Keymaster

    Hi. You must upload the full code to see what could be the problem.

  • #2964

    romant
    Participant
  • #2965

    Bitahwa Bindu
    Keymaster

    Please upload the full zipped project so that we can compile it.

  • #2973

    romant
    Participant

    Hi, Bitahwa. Here is my zipped project:

    Attachments:
    You must be logged in to view attached files.
  • #2977

    Bitahwa Bindu
    Keymaster

    The data received in your serialPort1_DataReceived method is coming from another thread context than the UI thread, and that’s the reason you see this error.
    To resolve this problem, instead of reading the content directly in the serialport1_DataReceived method, create a delegate like this:

    • This reply was modified 6 months, 1 week ago by  Bitahwa Bindu.
  • #2979

    romant
    Participant

    Hi, Bitahwa! Finnaly everuthig works well. Thanks a lot.

  • #2980

    Bitahwa Bindu
    Keymaster

    Good to hear.

    Please upload your  visual studio project. It might help others in the future.

    Thanks

You must be logged in to reply to this topic.