Hello to all users
I’m presently coding an application in VB.NET (VS 2010 Express on the laptop) with different parts/functions. I have to tell that I’m not an expert in neither VB or .Net But I’ve been busy on that issue for some time now. I’m trying to achieve the following:
- Send commands (string) to a Newport controller through RS232 (which in turn, drives a 2 axis-motor to rotate an item placed on these axis). For example the command to move the axis n°1 to the position “20 degrees” will look like : “1PA20” (ie: axis1-Position Absolute-20°)
- Once one movement of these 2 axis is finished, the controller sends back an answer to the first command string (ie: position, state of the motor, state of the movement: finished or running)
- After verifying the state of the rotating axis (it has a “1” at the end of the return string when it has stopped), I’m running an image processing thread (backgroundworker for the image Processing and UI thread for the Serialport stuff) which acquires images from CCDs cameras and processes them.
- The sequence detailed above happens 50 times (ie: 50 positions coordinates for the 2 axis, and the respective 50 images acquisitions after each and every position).
Now, that the context is explained, here is the problem:
For the moment I’m using ReadExisting() which maybe isn’t the best answer to my subject. From time to time, I’m having a timing issue with the Data Received Event of the Serialport class: he seems to fire too quickly and during acquisition of let’s say position n°6, the axis rotates to position n°7 (even though the thread is not the same). As he does it faster than usual, I thought of a buffer issue with some remaining data in it.
For the process to run a bit smoothly with my actual buggy setup, I have set the “ReceivedBytesThreshold” to 34, although the length of the string is closer to 70 char total (as they also are negative positions, the length of the string sent to the controller isn’t quite the same for the 50 cases, “1PA-10” being one char longer than “1PA20”). When this parameter is set to the exact length of the string (let’s say 70), the DataReceived event doesn’t fire.
The problem only occurs when I’m in a For Loop and going through the whole moving axis+process one after the other. The serialport part doesn’t produce an awkward behavior when used as a standalone vb program: I can send a 120 char long string and the movement is perfect and the answers in return in the richtextbox are as expected.
My questions:
1. Is this an OutBuffer issue?
2. Could DiscardOutBuffer + Breakstate (just after writing to the port or someplace else) bring a stable solution?
3. Should I use something not based on the DataReceived Event? Like maybe WriteLine() + ReadLine() with a vbCr as newline and try to stop the writing as soon as the vbCr is found? How to time the checking of the returned chars in that case ? (I would still need to read the last 2 char of the controller’s answer for each step).