I'm writing my master thesis and I stuck with programming serial port device communication.
I'm writing photo-acoustic scanner. Application that will allow to create thermal images.
I'm using and USB device to control steeper motors and an National Instruments device to acquire signal.
My application is controlling two stepper motors (it should control).
I have an USB controller build to control those motors, it is visible in system as a COM device. I can control it with hyper terminal, and with a simple application, so communication isn't the issue.
I have problem with building a two way communication between device and my application.
I'm sending strings to device like so:
serial.Write("CAL\r");
- this calibrates the device. But it can return two responses - 'CAL OK' or 'BUSY' - calibration is OK or device is doing something else right now.
I can listen for DataRecived event on serial port and then do string.equals.
My application must control motors and wait for response.
In a simple scenario:
- Start application
- Using inputs enters size of sample that will be scanned.
- Press contour button - application start to move motors first to top left corner, then top right, bottom right and bottom left. (I can send simple command, but how to send one, wait for response, then send another and after fourth enable buttons on GUI?)
- If region is defined correctly click next and scanning starts.
- Motors move to point 1,1 (lets call it like that) and signal acquisition starts.
- Data collected is processed and then we move to point 1,2
- Whole region is complete and user gets message that scanning is complete.
I don't know how to start with that Serial Port communication. How to implement that two-way communication.
I have a simple class (singleton) that open com port, sends commands and has event that is raised on every data received.
Would it be a good solution to create a stack in my class and send all commands on it and then in loop take them and send to device?
But how to handle that in a threat? Do threat sleep and then check if all command are processed, if no then again threat sleep?
I will be grateful for any advice.
I'm editing this to show how I would like my application to look/work
I have a simple application, that has a TabControl (modified version that hides tabs in application, I can see them only in design mode).
First tab has some text and information (mostly statistics). Second allows acquisition.
User click on a button on first tab "Go to Acquisition", that tab shows, but all buttons are disabled on it.
He must click "Open Calibration" dialog. Then in that dialog he calibrates device and enters all the acquisition parameters (in that dialog he has option to do contour- move motor to 4 corners on sample that will be measured). After calibration is done he can close that dialog and all controls are then active on main form. Now he can click "start acquisition" button. After that steeper motors will move to specific position (calculated for every point), data will be acquired for that point, processed, saved to file and displayed as a pixel in imagebox. Then motors will move to another point and the process repeats until the end of sample. Basically I will scan a square sample, moving laser to specific points, collecting data and building thermal image from that, something like building an image pixel by pixel.
I'll try to illustrate my idea with a diagram or with a screenshot.