Javaコンソールでシリアル通信を設定の方法

Javaでは、RXTXcommライブラリを使用してシリアル通信を実行できます。以下に簡単な例を示します。

  1. まず、RXTXcommライブラリをダウンロードしてインストールする必要があります。最新版のライブラリはhttps://github.com/rxtx/rxtxにあります。
  2. RXTXcommライブラリをJavaプロジェクトに追加します。
  3. 新しいJavaクラスを作成し、必要なクラスやパッケージをインポートします。
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
  1. シリアル通信メソッドをクラスへ定義する:
public class SerialCommunication {
    private InputStream inputStream;
    private OutputStream outputStream;

    public void connect(String portName) throws Exception {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if (portIdentifier.isCurrentlyOwned()) {
            System.out.println("Error: Port is currently in use");
        } else {
            CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

            if (commPort instanceof SerialPort) {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

                inputStream = serialPort.getInputStream();
                outputStream = serialPort.getOutputStream();

                (new Thread(new SerialReader(inputStream))).start();
                (new Thread(new SerialWriter(outputStream))).start();

            } else {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }
    }

    public static class SerialReader implements Runnable {
        private InputStream inputStream;

        public SerialReader(InputStream inputStream) {
            this.inputStream = inputStream;
        }

        public void run() {
            byte[] buffer = new byte[1024];
            int len = -1;
            try {
                while ((len = this.inputStream.read(buffer)) > -1) {
                    System.out.print(new String(buffer, 0, len));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static class SerialWriter implements Runnable {
        private OutputStream outputStream;

        public SerialWriter(OutputStream outputStream) {
            this.outputStream = outputStream;
        }

        public void run() {
            try {
                int c = 0;
                while ((c = System.in.read()) > -1) {
                    this.outputStream.write(c);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            SerialCommunication serial = new SerialCommunication();
            serial.connect("/dev/ttyUSB0"); // Replace with your serial port name
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  1. シリアルポート名を”/dev/ttyUSB0″に置き換えてコードを実行し、コンソールからシリアルポートと入出力で通信する。

この例ではシステムに正しいシリアルドライバーがインストールおよび構成されていることを前提に説明されている点にご注意ください。

bannerAds