java linux 串口_Linux Java 串口通信 | 学步园
費(fèi)了好大的勁搞定Linux系統(tǒng)上用Java寫串口通信的問題。
jdk中沒有原生的串口api,網(wǎng)上找了半天的資料,大概知道了:Linux系統(tǒng)上用Java寫串口程序,有兩個(gè)包比較常用,一個(gè)是當(dāng)年sun官方出的javacomm,但是找了半天都是老版本的居多,oracle官方不提供下載了,不爽。另一個(gè)是gnu的rxtx comm,看了一下還算靠譜,不過官方的wiki上(http://rxtx.qbang.org/wiki/index.php/Main_Page)說linux版本的在卸載usb轉(zhuǎn)串口線的時(shí)候會(huì)崩潰,頓時(shí)心頭一顫。下載下來之后試了一下,確實(shí)可以用,系統(tǒng)里幾個(gè)原生的串口都識(shí)別了,但唯獨(dú)我的Arduino板子不識(shí)別。
捉急之時(shí)想到了Arduino IDE里面是有Serial Monitor的,而且也是用Java寫的,于是去Arduino的安裝目錄里面找找,果然發(fā)現(xiàn)了在arduino安裝目錄下的lib目錄下,有RXTXcomm.jar、librxtxSerial.so、librxtxSerial64.so這三個(gè)文件(我的Linux是64位的,不知道32位的是不是沒有64.so這個(gè)文件),可以去Arduino官網(wǎng)上下載一個(gè)IDE,把這幾個(gè)文件復(fù)制出來,按照rxtx wiki上說明使用,用法是一樣的。
如果要脫離eclipse單獨(dú)執(zhí)行串口通信程序,將RXTXcomm.jar文件復(fù)制到$JAVA_HOME/jre/lib/ext目錄下,將librxtxSerial.so和librxtxSerial64.so復(fù)制到$JAVA_HOME/jre/lib/amd64/目錄下(如果是32位系統(tǒng)應(yīng)該是i386或者i686,而不是amd64)就OK了,官方wiki上描述的安裝步驟很麻煩,做了一下不行。
下面是一個(gè)例程,是官方的wiki里例程的改進(jìn)版【PS:以下代碼在win7上用不了,不能用BufferedReader包裝InputStream,不知道為啥,有知道原因的童鞋請(qǐng)告訴我】:
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();
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java linux 串口_Linux Java 串口通信 | 学步园的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mac OS X的远程访问控制条怎么用
- 下一篇: mysql的主从分离_Mysql的主从分