I am working on a unit test to ensure the data values that I am reading from my Mitutoyo gauges are correct. I send the command "GA00" and receive the data values back, then I send the command "CR01" to set the 1st channel to 0. I then send the command "GA00" again to see if channel 1 has been corrected back to 0 and it hasn't changed.
However, if I restart the test, then the values are changed to the 0 that I set it to and all is well. What happens when I restart the application/test that sets the data back to 0? If I could figure that out, then I would just do it in my code, and I'd be good.
Thanks for any help.
My code:
@Override
public void run() {
do {
if (!connected) {
connected = Connect(comPortName, baudRate, dataBit, stopBit, parity);
}
if (connected) {
MitutoyoCommand nextCommand = getNextCommand();
if (nextCommand != null) {
execute(nextCommand);
} else {
execute(getAllValuesCommand);
}
}
try {
Thread.sleep(dataFrameRefreshRate);
} catch (InterruptedException ex) {
}
} while (!stop);
}
public boolean execute(MitutoyoCommand commandToSend) {
String receivedData;
System.out.println("Writing: " + commandToSend.getCommandToSend());
WriteToPort(commandToSend.getCommandToSend());
boolean executeCommandStarted = true;
long timeSinceCommandSent = java.lang.System.currentTimeMillis();
do {
if (java.lang.System.currentTimeMillis() - timeSinceCommandSent > dataFrameRefreshRate) {
receivedData = new String(serialBuffer, 0, serialBufferLength);
System.out.println("Received Data: " + receivedData);
communicationOK = commandToSend.processReceivedData(this, receivedData);
executeCommandStarted = false;
return true;
}
} while (!isStop());
return false;
}
private void WriteToPort(String send) {
try {
byte[] commandBytes = send.getBytes();
System.arraycopy(commandBytes, 0, sendCommandBuffer, 0, commandBytes.length);
sendCommandBuffer[commandBytes.length] = 13; // CarriageReturn
sendCommandBuffer[commandBytes.length + 1] = 10; // LineFeed
out1.write(sendCommandBuffer, 0, commandBytes.length + 2);
out1.flush();
try {
Thread.sleep(dataFrameRefreshRate);
} catch (InterruptedException ex) {
}
} catch (IOException e) {
logger.log(Level.INFO, "There was an error while writing to the serial port", e);
Disconnect();
}
}
private static final int RETRY_WRITE = 5;
@Override
public void serialEvent(SerialPortEvent event) {
if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
while (in1.available() > 0) {
int numBytes = in1.read(serialBuffer, this.serialBufferLength, serialBuffer.length - this.serialBufferLength);
serialBufferLength += numBytes;
}
} catch (IOException e) {
logger.log(Level.INFO, "There was an error while receiving data from the serial port", e);
}
}
}
private void queueCommand(MitutoyoCommand command) {
synchronized (commandsQueue) {
commandsQueue.add(command);
System.out.println("Command queued: " + command.getCommandToSend());
}
}
private MitutoyoCommand getNextCommand() {
synchronized (commandsQueue) {
System.out.print("Command Queue: ");
for (MitutoyoCommand command : commandsQueue) {
System.out.print(command.getCommandToSend() + ", ");
}
System.out.println("<END>");
if (commandsQueue.size() > 0) {
return commandsQueue.poll();
} else {
return null;
}
}
}
Here is my output: (I removed several lines of output where you see the ellipsis.)
...
GN03,+0000.2445
GN04,-0000.0985
GN05,-0000.1255
GN06,-0000.1100
GN07,-0021.2845
GN08,-0012.9835
GN09,-0014.9980
GN10,-0015.0275
GN11,-0002.4090
GN12,-0002.3935
GN01,+00
Command queued: GA00
Command Queue: GA00, <END>
Writing: GA00
Command queued: CS01
setProbeAToZero
Command queued: CR01
Command queued: CR02
Command queued: CR03
Received Data: GN01,+0000.2775
GN02,+0000.2505
GN03,+0000.2450
GN04,-0000.0985
GN05,-0000.1255
GN06,-0000.1100
GN07,-0021.2845
GN08,-0012.9835
...
GN06,-0000.1100
GN07,-0021.2845
GN08,-0012.9835
GN09,-0014.9980
GN10,-0015.0275
GN11,-0002.4090
GN12,-0002.3935
GN01,+00
Command queued: GA00
Command Queue: CS01, CR01, CR02, CR03, GA00, <END>
Writing: CS01
Received Data: GN01,+0000.2775
GN02,+0000.2505
GN03,+0000.2450
GN04,-0000.0985
GN05,-0000.1255
GN06,-0000.1100
GN07,-0021.2845
GN08,-0012.9835
GN09,-0014.9980
GN10,-0015.0275
...
GN10,-0015.0275
GN11,-0002.4090
GN12,-0002.3935
GN01,+00
Command Queue: CR01, CR02, CR03, GA00, <END>
Writing: CR01
Received Data: GN01,+0000.2775
GN02,+0000.2505
GN03,+0000.2450
GN04,-0000.0985
GN05,-0000.1255
GN06,-0000.1100
GN07,-0021.2845
GN08,-0012.9835
GN09,-0014.9980
GN10,-0015.0275
GN11,-0002.4090
GN12,-0002.3935
...
GN04,-0000.0985
GN05,-0000.1255
GN06,-0000.1100
GN07,-0021.2845
GN08,-0012.9835
GN09,-0014.9980
GN10,-0015.0275
GN11,-0002.4090
GN12,-0002.3935
GN01,+00
Command Queue: CR02, CR03, GA00, <END>
Writing: CR02
Received Data: GN01,+0000.2775
GN02,+0000.2505
GN03,+0000.2450
GN04,-0000.0985
GN05,-0000.1255
GN06,-0000.1100
GN07,-0021.2845
GN08,-0012.9835
GN09,-0014.9980
GN10,-0015.0275
...
GN05,-0000.1255
GN06,-0000.1100
GN07,-0021.2845
GN08,-0012.9835
GN09,-0014.9980
GN10,-0015.0275
GN11,-0002.4090
GN12,-0002.3935
GN01,+00
Command Queue: CR03, GA00, <END>
Writing: CR03
Received Data: GN01,+0000.2775
GN02,+0000.2505
GN03,+0000.2450
GN04,-0000.0985
GN05,-0000.1255
GN06,-0000.1100
GN07,-0021.2845
GN08,-0012.9835
GN09,-0014.9980
GN10,-0015.0275
GN11,-0002.4090
GN12,-0002.3935
...
GN08,-0012.9835
GN09,-0014.9980
GN10,-0015.0275
GN11,-0002.4090
GN12,-0002.3935
GN01,+00
Command Queue: GA00, <END>
Writing: GA00
Received Data: GN01,+0000.2775
GN02,+0000.2505
GN03,+0000.2450
GN04,-0000.0985
GN05,-0000.1255
GN06,-0000.1100
GN07,-0021.2845
GN08,-0012.9835
GN09,-0014.9980
...
GN05,-0000.1255
GN06,-0000.1100
GN07,-0021.2845
GN08,-0012.9835
GN09,-0014.9980
GN10,-0015.0275
GN11,-0002.4090
GN12,-0002.3935
GN01,+00
Command queued: GA00
Command Queue: GA00, <END>
Writing: GA00
Received Data: GN01,+0000.2775
GN02,+0000.2505
GN03,+0000.2450
GN04,-0000.0985
GN05,-0000.1255
GN06,-0000.1100
GN07,-0021.2845
GN08,-0012.9835
GN09,-0014.9980
...
GN10,-0015.0275
GN11,-0002.4090
GN12,-0002.3935
GN01,+00
Command queued: GA00
Command Queue: GA00, <END>
Writing: GA00
Received Data: GN01,+0000.2775
GN02,+0000.2505
GN03,+0000.2450
GN04,-0000.0985
GN05,-0000.1255
GN06,-0000.1100
GN07,-0021.2845
GN08,-0012.9835
GN09,-0014.9980
GN10,-0015.0275
GN11,-0002.4090
GN12,-00