使用java通过串口发送CRC命令

时间:2011-08-05 06:46:40

标签: java serial-port rxtx

有没有办法通过串口将CRC命令从一台PC发送到另一台PC!

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

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

1 个答案:

答案 0 :(得分:1)

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