JAVAによるシリアルポート(RS 232)との通信例
最近分かった需要は、レーザ打刻機による(RS 232)シリアル通信が必要であり、
ここではRXTXオープンソースパッケージを用いて実装した.
これまでjavaでシリアル通信をしたことはなく、この方面の資料は多くありません.
プロジェクトの実際の応用ではVB開発を採用する可能性があります(これは私にはできません)
ただjavaで試して、メモを取って、関連開発に役立つことを望んでいます.
次は実装コードです
テスト
ここではRXTXオープンソースパッケージを用いて実装した.
これまでjavaでシリアル通信をしたことはなく、この方面の資料は多くありません.
プロジェクトの実際の応用ではVB開発を採用する可能性があります(これは私にはできません)
ただjavaで試して、メモを取って、関連開発に役立つことを望んでいます.
次は実装コードです
package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
public class CommUtil implements SerialPortEventListener {
InputStream inputStream; //
OutputStream outputStream;//
SerialPort serialPort; //
CommPortIdentifier portId;
public CommUtil(Enumeration portList, String name) {
while (portList.hasMoreElements()) {
CommPortIdentifier temp = (CommPortIdentifier) portList.nextElement();
if (temp.getPortType() == CommPortIdentifier.PORT_SERIAL) {//
if (temp.getName().equals(name)) { //
portId = temp;
}
}
}
try {
serialPort = (SerialPort) portId.open("My"+name, 2000);
} catch (PortInUseException e) {
}
try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
}
try {
serialPort.addEventListener(this); //
} catch (TooManyListenersException e) {
}
serialPort.notifyOnDataAvailable(true); //
try {
serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8, //
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:// ,
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
System.out.println(inputStream.available());
int numBytes = inputStream.read(readBuffer);
System.out.println(numBytes);
}
System.out.println(new String(readBuffer).trim());
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
public void send(String content){
try {
outputStream.write(content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
public void ClosePort() {
if (serialPort != null) {
serialPort.close();
}
}
}
テスト
package test;
import gnu.io.CommPortIdentifier;
import java.util.Enumeration;
public class Test {
public static void main(String[] args) throws InterruptedException {
Enumeration portList = CommPortIdentifier.getPortIdentifiers(); //
CommUtil comm3 = new CommUtil(portList,"COM3");
int i = 0;
while(i<5)
{
Thread.sleep(3000);
comm3.send("hello");
i++;
}
comm3.ClosePort();
}
}