CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2003
    Location
    The Netherlands
    Posts
    586

    Again serial port threads

    My assignment is to do serial port communications with the Modbus protocol.
    I already wrote a Read thread and a Write thread. The readthread is a threadfunction in the main process and the write thread is derived from CWinThread.

    The tasks the program should perform are:
    1)Send Modbus status request to serial port (Write thread)
    2)Wait for response to arrive at the serial port (Read thread)
    3)Send Modbus value request to serial port (Write thread)
    4)Wait for response to arrive at the serial port (Read thread)

    These tasks are to be performed repeatedly, every 2 or 3 secs.
    The problem is in the synchronisation between the threads. I now use a timer that fires every 2 secs and place the calls to the threads inside the OnTimer(). This all works fine, however it makes only sense to start reading after the request has been done and the threads can not use more time than 2 secs, otherwise a new timerevent is fired. What is the best way to do this? Some advice would be welcome.
    Time is fun when you're having flies

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Again serial port threads

    I already wrote a Read thread and a Write thread.
    why 2 threads ?? why not use 1 thread for reading AND writing.

  3. #3
    Join Date
    Jun 2005
    Posts
    315

    Re: Again serial port threads

    I agree with Skizmo, if the processing order is always the same, send request then wait for reply, one thread would seem to make more sense.

    Jeron

  4. #4
    Join Date
    Sep 2001
    Location
    Victoria, BC, Canada
    Posts
    363

    Re: Again serial port threads

    If you re really wanting two threads, then share a mutex or an event or critical section or something.

    Basically, you have a csRX and a csTX for the two threads.

    Your TX() thread waits for the csTX to be triggered before proceeding and the RX() thread waits for the csRX event to be signaled.

    So step 1, you transmit the status request, then signal the csRX event, change the state of csTX and then wait on csTX.

    Step 2, RX() does it's thing, then signals the csTX and resets csRX.

    Continue forever...

  5. #5
    Join Date
    Mar 2003
    Location
    The Netherlands
    Posts
    586

    Re: Again serial port threads

    Thanx for the comments guys. Radius i think i am going for your solution. One question however. Should the threads use a semaphore for using the serial port, i can imagine two write threads wanting to write something at the same time to the port, or two receive threads want to receive something. Or doesn't this ever happen? I am in the assumption to create a new thread everytime or should i reuse a created thread?
    Time is fun when you're having flies

  6. #6
    Join Date
    Sep 2001
    Location
    Victoria, BC, Canada
    Posts
    363

    Re: Again serial port threads

    In the case of Modbus (I have used Modbus a lot in the past) it's a request/receive type of thing, so I just use critical sections. You get a response for each request normally so this is sufficient.

    Remember to add timeouts though, just in case the device you're talking to doesn't respond for whatever reason.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured