Mitutoyo串行通信数据值未更新

时间:2014-07-08 22:23:21

标签: java serial-port serial-communication

我正在进行单元测试,以确保我从Mitutoyo仪表读取的数据值是正确的。我发送命令“GA00”并接收数据值,然后我发送命令“CR01”将第一个通道设置为0.然后再次发送命令“GA00”以查看通道1是否已被更正回0它没有改变。

但是,如果我重新开始测试,那么值将更改为我设置为0的值,一切都很好。当我重新启动将数据设置回0的应用程序/测试时会发生什么?如果我能弄明白,那么我会在我的代码中做到这一点,我会很好。

感谢您的帮助。

我的代码:

@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;
        }
    }
}

这是我的输出:(我删除了几行输出,你会看到省略号。)

...
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

1 个答案:

答案 0 :(得分:0)

我的问题是我试图同时读取和写入串口。一位同事建议我查看并发和多线程,这引导我阅读这些人的教程: http://tutorials.jenkov.com/java-concurrency/index.html 强烈推荐他关于任何主题的教程。写得很好,易于理解。 最后,我在类中添加了一个Object,并在读取和写入部分周围同步了块(在对象上同步)。

固定代码:

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);
        }
    }
}