如何向通过串口连接的嵌入式电脑发送命令
我想向通过串口连接到我的电脑的嵌入式电脑发送命令...这是我使用的代码...
public class Write {
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) {
}
}
}
}
}
}
这段代码是否正确,或者应该对此进行一些修改代码 ....
i want to send commands to to an embadded pc whihch connected to my pc through serial port ... here is the code that i use ...
public class Write {
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) {
}
}
}
}
}
}
is this code is correct, or there should be some modification to this code ....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它有效,那么我看不出任何明显的错误!我唯一要指出的是,最好将 close 语句(例如当您在最后关闭串行端口时)放在 finally 块中,以便它们始终被执行(除了 VM 终止之外)。目前,如果
outputStream.write(messageString.getBytes());
抛出异常,close()
方法不会被执行。您也不应该忽略异常,至少执行
printStackTrace()
以确保没有发生任何事情。If it works then there's nothing obviously wrong that I can see! The only thing I'd point out is that it's good practice to put close statements (such as when you're closing the serial port at the end) in finally blocks so they're always executed (VM termination aside.) At present if there's an exception thrown by
outputStream.write(messageString.getBytes());
theclose()
method wouldn't be executed.You also shouldn't ignore exceptions, at least do a
printStackTrace()
to make sure there's nothing happening.