Java 串口 mysql_java串口通讯
使用java進(jìn)行串口通信確實(shí)不是串口通訊操作的主流方法,另外寫(xiě)好的的java程序無(wú)法隨意的遷移。不過(guò)對(duì)熟悉java的同學(xué)確實(shí)挺有幫助,下面是我使用java語(yǔ)言進(jìn)行串口通訊的過(guò)程。
1 usb轉(zhuǎn)串口安裝。
2 jar包的安裝。
3 java代碼的實(shí)現(xiàn)。
usb轉(zhuǎn)串口的安裝。
一般把串口線(xiàn)插上電腦就會(huì)提示安裝驅(qū)動(dòng),在網(wǎng)上下一個(gè)串口驅(qū)動(dòng)就會(huì)成功了。可我的電腦卻怎么也成功不了,在網(wǎng)上找了各種方法就是不行(我的系統(tǒng)是win7)。更有不靠譜的說(shuō)法是重裝系統(tǒng)。總之試變了網(wǎng)上的驅(qū)動(dòng),還用什么驅(qū)動(dòng)精靈之類(lèi)的都把還是沒(méi)有成功。打開(kāi)設(shè)備管理器,usb-serial controller總是有×××的嘆號(hào)。
我的解決方法。
運(yùn)行-》輸入hdwwiz-》下一步選擇安裝我手動(dòng)列表中選擇硬件(高級(jí))-》下一步選擇端口(com和lpt)-》下一步選擇public-》然后選擇第一個(gè)public ............->再點(diǎn)擊下一步進(jìn)行安裝。
最后終于成功了。
安裝jar包
安裝jar包和我們平常的引入jar包不太一樣。附件是用到的jar包。
首先把RXTXcomm.jar放入所用jre的lib目錄下,把rxtxSerial.dll放到j(luò)re的bin目錄下就可以了。
代碼實(shí)現(xiàn)
主要是兩步第一步查到所使用的com口。第二步對(duì)串口進(jìn)行監(jiān)聽(tīng)。
具體的代碼如下:
import java.io.*;
import java.util.*;
import test.FeelTheBase;
import test.SimpleReadA;
import gnu.io.*;
public class SimpleRead implements SerialPortEventListener { // SerialPortEventListener
// 監(jiān)聽(tīng)器,我的理解是獨(dú)立開(kāi)辟一個(gè)線(xiàn)程監(jiān)聽(tīng)串口數(shù)據(jù)
public CommPortIdentifier portId; // 串口通信管理類(lèi)
static Enumeration portList; // 已經(jīng)連接上的端口的枚舉
public InputStream inputStream; // 從串口來(lái)的輸入流
public InputStreamReader streamReader;
public OutputStream outputStream;// 向串口輸出的流
public OutputStreamWriter streamwriter;
public SerialPort serialPort; // 串口的引用
public void init() {
try {
serialPort = (SerialPort) portId.open(s, 2000);// 打開(kāi)串口名字為myapp,延遲為2毫秒
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
serialPort.addEventListener(this); // 給當(dāng)前串口天加一個(gè)監(jiān)聽(tīng)器
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
catch (TooManyListenersException e) {
}
serialPort.notifyOnDataAvailable(true); // 當(dāng)有數(shù)據(jù)時(shí)通知
try {
serialPort.setSerialPortParams(57600, SerialPort.DATABITS_8, // 設(shè)置串口讀寫(xiě)參數(shù)
SerialPort.STOPBITS_1, SerialPort.PARITY_ODD);
} catch (UnsupportedCommOperationException e) {
}
}
public void serialEvent(SerialPortEvent event) {// SerialPortEventListener
// 的方法,監(jiān)聽(tīng)的時(shí)候會(huì)不斷執(zhí)行
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:// 當(dāng)有可用數(shù)據(jù)時(shí)讀取數(shù)據(jù),并且給串口返回?cái)?shù)據(jù)
try {
while (inputStream.available() > 0) {
byte[] by = new byte[inputStream.available()];
inputStream.read(by);
System.out.println(by);//獲得的數(shù)據(jù)
}
} catch (IOException e) {
}
break;
}
}
public static void main(String[] args) {
SimpleRead com3 = new SimpleRead();
try {
portList = CommPortIdentifier.getPortIdentifiers(); // 得到當(dāng)前連接上的端口
while (portList.hasMoreElements()) {
CommPortIdentifier p = (CommPortIdentifier) portList
.nextElement();
if (p.getPortType() == CommPortIdentifier.PORT_SERIAL) {// 判斷如果端口類(lèi)型是串口
if (p.getName().equals("COM25")) { // 判斷如果COM3端口已經(jīng)啟動(dòng)就連接
com3.setCom(p);
com3.setOther(com5);
System.out.println(p.getName());
}
}
}
com3.init(); // 實(shí)例一個(gè);
} catch (Exception e) {
e.printStackTrace();
}
}
}
總結(jié)
以上是生活随笔為你收集整理的Java 串口 mysql_java串口通讯的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: b700a怎么连蓝牙_小身材有大功能,小
- 下一篇: [云炬创业基础笔记]第十一章创业计划书测