-1
我想發送命令到whihch通過串口連接到我的電腦的embadded PC連接嵌入式PC ...這裏是我使用的代碼...如何發送命令通過串口
公衆類寫{
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!\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("SimpleWriteApp", 2000);
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) {
}
}
}
}
}
}
是這個代碼是正確的,還是應該有一些修改這個代碼....
那麼,它的工作?你試過了嗎? – berry120
雅我做...它的工作,但我仍然懷疑代碼...這是正確的嗎? –
berry120,上面的代碼是發送反正有發送一個特定的命令,如CRC「循環冗餘檢查」到另一臺PC機! –