【问题标题】:send CRC command through the serial port using java使用java通过串口发送CRC命令
【发布时间】:2011-08-05 19:16:11
【问题描述】:

有没有什么方法可以通过串口从一台 PC 使用 Java 向另一台 PC 发送 CRC 命令!

这是连接端口并打开它的代码...

public class Write {

static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "\n";
static SerialPort serialPort;
static OutputStream outputStream;

public static void main(String[] args) {
    portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (portId.getName().equals("COM1")) {
                try {
                    serialPort = (SerialPort) portId.open("Embedded", 8000);
                    System.out.println("openning the port...");
                } catch (PortInUseException e) {
                }
                try {
                    outputStream = serialPort.getOutputStream();
                    System.out.println("sending the command...");
                } catch (IOException e) {
                }
                try {
                    serialPort.setSerialPortParams(9600,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);

                } catch (UnsupportedCommOperationException e) {
                }
                try {
                    outputStream.write(messageString.getBytes());
                    serialPort.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

【问题讨论】:

标签: java serial-port rxtx


【解决方案1】:

您似乎已经有了代码中较难的部分,即串行端口写入。现在您只需计算 crc 并将其放入您的 outputStream.write:http://download.oracle.com/javase/1.4.2/docs/api/java/util/zip/CRC32.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多