使用java通过串口发送CRC命令
有没有办法使用Java通过串行端口将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) {
}
}
}
}
}
is there any way to send CRC command through serial port from one PC to another one using Java!
here is the code to connect to the port and open it...
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) {
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎已经有了代码中更难的部分,即串行端口写入。现在您所需要做的就是计算 crc 并将其放入
outputStream.write
中:http://download.oracle.com/javase/1.4.2/docs/api/java/util/zip/CRC32.htmlYou seem to already have the harder part of the code, the serial-port write. Now all you need is to calculate the crc and place it in your
outputStream.write
: http://download.oracle.com/javase/1.4.2/docs/api/java/util/zip/CRC32.html