日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

java linux 串口_Linux Java 串口通信 | 学步园

發布時間:2023/12/15 linux 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java linux 串口_Linux Java 串口通信 | 学步园 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

費了好大的勁搞定Linux系統上用Java寫串口通信的問題。

jdk中沒有原生的串口api,網上找了半天的資料,大概知道了:Linux系統上用Java寫串口程序,有兩個包比較常用,一個是當年sun官方出的javacomm,但是找了半天都是老版本的居多,oracle官方不提供下載了,不爽。另一個是gnu的rxtx comm,看了一下還算靠譜,不過官方的wiki上(http://rxtx.qbang.org/wiki/index.php/Main_Page)說linux版本的在卸載usb轉串口線的時候會崩潰,頓時心頭一顫。下載下來之后試了一下,確實可以用,系統里幾個原生的串口都識別了,但唯獨我的Arduino板子不識別。

捉急之時想到了Arduino IDE里面是有Serial Monitor的,而且也是用Java寫的,于是去Arduino的安裝目錄里面找找,果然發現了在arduino安裝目錄下的lib目錄下,有RXTXcomm.jar、librxtxSerial.so、librxtxSerial64.so這三個文件(我的Linux是64位的,不知道32位的是不是沒有64.so這個文件),可以去Arduino官網上下載一個IDE,把這幾個文件復制出來,按照rxtx wiki上說明使用,用法是一樣的。

如果要脫離eclipse單獨執行串口通信程序,將RXTXcomm.jar文件復制到$JAVA_HOME/jre/lib/ext目錄下,將librxtxSerial.so和librxtxSerial64.so復制到$JAVA_HOME/jre/lib/amd64/目錄下(如果是32位系統應該是i386或者i686,而不是amd64)就OK了,官方wiki上描述的安裝步驟很麻煩,做了一下不行。

下面是一個例程,是官方的wiki里例程的改進版【PS:以下代碼在win7上用不了,不能用BufferedReader包裝InputStream,不知道為啥,有知道原因的童鞋請告訴我】:

package test;

import gnu.io.CommPort;

import gnu.io.CommPortIdentifier;

import gnu.io.SerialPort;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

public class TwoWaySerialComm

{

public TwoWaySerialComm()

{

super();

}

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 in = serialPort.getInputStream();

InputStreamReader reader = new InputStreamReader(in);

BufferedReader r = new BufferedReader(reader);

(new Thread(new SerialReader(r))).start();

}

else

{

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

}

}

}

/** */

public static class SerialReader implements Runnable

{

BufferedReader in;

public SerialReader ( BufferedReader in )

{

this.in = in;

}

public void run ()

{

try

{

String line;

while ((line = in.readLine()) != null){

System.out.println(line);

}

}

catch ( IOException e )

{

e.printStackTrace();

}

}

}

/** */

public static class SerialWriter implements Runnable

{

OutputStream out;

public SerialWriter ( OutputStream out )

{

this.out = out;

}

public void run ()

{

try

{

int c = 0;

while ( ( c = System.in.read()) > -1 )

{

this.out.write(c);

}

}

catch ( IOException e )

{

e.printStackTrace();

}

}

}

static void listPorts()

{

@SuppressWarnings("unchecked")

java.util.Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

while ( portEnum.hasMoreElements() )

{

CommPortIdentifier portIdentifier = portEnum.nextElement();

System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()) );

}

}

static String getPortTypeName ( int portType )

{

switch ( portType )

{

case CommPortIdentifier.PORT_I2C:

return "I2C";

case CommPortIdentifier.PORT_PARALLEL:

return "Parallel";

case CommPortIdentifier.PORT_RAW:

return "Raw";

case CommPortIdentifier.PORT_RS485:

return "RS485";

case CommPortIdentifier.PORT_SERIAL:

return "Serial";

default:

return "unknown type";

}

}

public static void main ( String[] args )

{

listPorts();

try

{

(new TwoWaySerialComm()).connect("/dev/ttyACM0");

}

catch ( Exception e )

{

e.printStackTrace();

}

}

}

總結

以上是生活随笔為你收集整理的java linux 串口_Linux Java 串口通信 | 学步园的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。