Simultaneous Serial Communication on 1 port
-
I have 2 functions that need to communicate with the same device using the same COM Port. Problem is sometimes they do so at the sametime and end up reading the wrong data. Is there anyway that I can have both use the same comport without having this conflict? I thought about Overlapped I/O, but I am not all to familiar with how to implement it and am not sure if it will solve my problem. Thanks
-
I have 2 functions that need to communicate with the same device using the same COM Port. Problem is sometimes they do so at the sametime and end up reading the wrong data. Is there anyway that I can have both use the same comport without having this conflict? I thought about Overlapped I/O, but I am not all to familiar with how to implement it and am not sure if it will solve my problem. Thanks
Brute force method: Add a comport_status flag and a pair of functions to set and check the status - BUSY or NOT_BUSY - then modify each I/O function to test the port status before attempting to use it. BUSY would be set by the function just prior to using the port, then on completion, it would reset the status to NOT_BUSY. There are certainly more elegant ways to do this, but it's one approach. "...putting all your eggs in one basket along with your bowling ball and gym clothes only gets you scrambled eggs and an extra laundry day... " - Jeffry J. Brickley
-
I have 2 functions that need to communicate with the same device using the same COM Port. Problem is sometimes they do so at the sametime and end up reading the wrong data. Is there anyway that I can have both use the same comport without having this conflict? I thought about Overlapped I/O, but I am not all to familiar with how to implement it and am not sure if it will solve my problem. Thanks
Very strange, because only one function can communicate at a time; unless they are in seperate threads. If you have 2 different threads trying to communicate using the same comport, then you have a bad design. That does not mean you can not do it, what it means is you made a mistake in disign; it can be done but it does not make any since. May be you explaned it wrong or you worded the question wrong. As you can see from my above answer there is something missing. If you can, write a minimum code example (tested) that shows the problem; the attempt to do so will probably show you the answer. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
-
Very strange, because only one function can communicate at a time; unless they are in seperate threads. If you have 2 different threads trying to communicate using the same comport, then you have a bad design. That does not mean you can not do it, what it means is you made a mistake in disign; it can be done but it does not make any since. May be you explaned it wrong or you worded the question wrong. As you can see from my above answer there is something missing. If you can, write a minimum code example (tested) that shows the problem; the attempt to do so will probably show you the answer. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
Each function is in a seperate thread. I am sorry I forgot to mention that. function1 loops in its own thread until the program closes it. It reads from COM port 2 every second (1000 ms). I did this because this function/Thread needs to update the GUI/Edit box every second. so I left it seperate from everything else going on. The other function, function2 is in another thread of its own reads from COM1 until all values it needs is read then it reads from COM2 as well to get data from there. The problem arises because function2 will read from COM2 more then once per second (like every 200ms or so). So it occurs where at some point both functions read from COM2 at the sametime thus causing one or both functions to read the wrong incoming data. I tried to do a "COMM in use" flag like Roger said above, but it didnt work because like I said at some point both will use the port at the sametime thus not allowing one function to set the flag in time before the other uses it.
-
Each function is in a seperate thread. I am sorry I forgot to mention that. function1 loops in its own thread until the program closes it. It reads from COM port 2 every second (1000 ms). I did this because this function/Thread needs to update the GUI/Edit box every second. so I left it seperate from everything else going on. The other function, function2 is in another thread of its own reads from COM1 until all values it needs is read then it reads from COM2 as well to get data from there. The problem arises because function2 will read from COM2 more then once per second (like every 200ms or so). So it occurs where at some point both functions read from COM2 at the sametime thus causing one or both functions to read the wrong incoming data. I tried to do a "COMM in use" flag like Roger said above, but it didnt work because like I said at some point both will use the port at the sametime thus not allowing one function to set the flag in time before the other uses it.
You seem to have more problems than function2 (thread2) reading from the comport more than once per second. 1) function1 does not need to be in a thread, it only reads once per second. Therefore, it can can be done in a OnTimer-(WM_TIMER) method. 2) How does function2 know that function1 is finished reading (or vis-versa)? Normaly you would set an event, that says, I have finished reading may data; then function(?) can read any data that it may be expecting to receive. What you are describing is a nightmare, that I (am surprised) have not seen before. I do not know why one thread would be getting its data from 2 different ports (one shared) (again this smacks of poor design [which I assume was not yours]). In ether case both threads needs to know when the other thread has finished reading its data from the given port; they have to set an event (signaling that they are finished). I wish I could help more, but, like I said, this makes no since. Who ever designed the origanal (COM) system, created a very bad design. Good Luck! INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
-
I have 2 functions that need to communicate with the same device using the same COM Port. Problem is sometimes they do so at the sametime and end up reading the wrong data. Is there anyway that I can have both use the same comport without having this conflict? I thought about Overlapped I/O, but I am not all to familiar with how to implement it and am not sure if it will solve my problem. Thanks