Java Mitutoyo串行通信数据值未更新
我正在进行单元测试,以确保我从Mitutoyo仪表读取的数据值是正确的。我发送命令“GA00”并接收回数据值,然后发送命令“CR01”将第一个通道设置为0。然后,我再次发送命令“GA00”,查看通道1是否已更正回0,并且没有更改 但是,如果我重新启动测试,那么值将更改为我将其设置为的0,并且一切正常。重新启动将数据设置回0的应用程序/测试时会发生什么情况?如果我能弄明白,那么我就用我的代码来做,我会做得很好 谢谢你的帮助 我的代码:Java Mitutoyo串行通信数据值未更新,java,serial-port,serial-communication,Java,Serial Port,Serial Communication,我正在进行单元测试,以确保我从Mitutoyo仪表读取的数据值是正确的。我发送命令“GA00”并接收回数据值,然后发送命令“CR01”将第一个通道设置为0。然后,我再次发送命令“GA00”,查看通道1是否已更正回0,并且没有更改 但是,如果我重新启动测试,那么值将更改为我将其设置为的0,并且一切正常。重新启动将数据设置回0的应用程序/测试时会发生什么情况?如果我能弄明白,那么我就用我的代码来做,我会做得很好 谢谢你的帮助 我的代码: @Override public void run() {
@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;
}
}
}
@覆盖
公开募捐{
做{
如果(!已连接){
已连接=连接(组件名称、波特率、数据位、停止位、奇偶校验);
}
如果(已连接){
MitutoyoCommand-nextCommand=getNextCommand();
if(nextCommand!=null){
执行(下一个命令);
}否则{
执行(getAllValuesCommand);
}
}
试一试{
休眠(dataFrameRefreshRate);
}捕获(中断异常例外){
}
}而(!停止);
}
公共布尔执行(MitutoyoCommand commandToSend){
字符串接收数据;
System.out.println(“写入:+commandToSend.getCommandToSend());
WritePort(commandToSend.getCommandToSend());
布尔值executeCommandStarted=true;
long-timeSinceCommandSent=java.lang.System.currentTimeMillis();
做{
if(java.lang.System.currentTimeMillis()-timeSinceCommandSent>dataFrameRefreshRate){
receivedData=新字符串(serialBuffer,0,serialBufferLength);
System.out.println(“接收数据:“+receivedData”);
communicationOK=commandToSend.processReceivedData(这个,receivedData);
executeCommandStarted=false;
返回true;
}
}而(!isStop());
返回false;
}
私有void writeport(字符串发送){
试一试{
byte[]commandBytes=send.getBytes();
System.arraycopy(commandBytes,0,sendCommandBuffer,0,commandBytes.length);
sendCommandBuffer[commandBytes.length]=13;//返回
sendCommandBuffer[commandBytes.length+1]=10;//换行符
out1.写入(sendCommandBuffer,0,commandBytes.length+2);
out1.flush();
试一试{
休眠(dataFrameRefreshRate);
}捕获(中断异常例外){
}
}捕获(IOE异常){
logger.log(Level.INFO,“写入串行端口时出错”,e);
断开连接();
}
}
私有静态final int RETRY_WRITE=5;
@凌驾
public void serialEvent(SerialPortEvent事件){
if(event.getEventType()==SerialPortEvent.DATA\u可用){
试一试{
while(in1.available()>0){
int numBytes=in1.read(serialBuffer,this.serialBufferLength,serialBuffer.length-this.serialBufferLength);
serialBufferLength+=numBytes;
}
}捕获(IOE异常){
logger.log(Level.INFO,“从串行端口接收数据时出错”,e);
}
}
}
专用void队列命令(MitutoyoCommand命令){
已同步(命令队列){
commandsque.add(命令);
System.out.println(“命令队列:+Command.getCommandToSend());
}
}
私有MitutoyoCommand getNextCommand(){
已同步(命令队列){
System.out.print(“命令队列:”);
for(MitutoyoCommand:commandsQueue){
System.out.print(command.getCommandToSend()+“,”);
}
System.out.println(“”);
if(commandsque.size()>0){
返回commandsquee.poll();
}否则{
返回null;
}
}
}
这是我的输出:(我删除了您看到省略号的几行输出。)
。。。
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
命令队列:GA00
命令队列:GA00,
写作:GA00
命令队列:CS01
setProbeAToZero
命令队列:CR01
命令队列:CR02
命令队列:CR03
接收数据: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
命令队列:GA00
命令队列:CS01、CR01、CR02、CR03、GA00、,
写作:CS01
接收数据: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
命令队列:CR01、CR02、CR03、GA00、,
写作:CR01
接收数据: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
命令队列:CR02、CR03、GA00、,
写作:CR02
接收数据: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
命令队列:CR03,GA00,
写作:CR03
接收数据: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
GN
...
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
private final Object mutex = new Object();
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
synchronized (mutex) {
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();
}
}
@Override
public void serialEvent(SerialPortEvent event) {
if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
synchronized (mutex) {
while (in1.available() > 0) {
System.out.print("Getting Input. Available Bytes:" + in1.available());
int numBytes = in1.read(serialBuffer, this.serialBufferLength, serialBuffer.length - this.serialBufferLength);
serialBufferLength += numBytes;
try {
Thread.sleep(dataFrameRefreshRate);
} catch (InterruptedException ex) {
}
}
}
} catch (IOException e) {
logger.log(Level.INFO, "There was an error while receiving data from the serial port", e);
}
}
}