-
March 30th, 2007, 09:36 AM
#1
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
-
March 30th, 2007, 10:03 AM
#2
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.
-
March 30th, 2007, 10:12 AM
#3
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
-
March 30th, 2007, 11:13 AM
#4
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...
-
April 2nd, 2007, 01:47 AM
#5
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
-
April 2nd, 2007, 01:28 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|