0

有没有办法使用Java通过串口从一台PC发送CRC命令到另一台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) {
                }
            }
        }
    }
}
4

1 回答 1

1

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

于 2011-08-05T09:51:00.630 回答