導航:首頁 > 編程語言 > java讀串口

java讀串口

發布時間:2022-09-19 05:50:02

java讀取串口數據

byte[]是位元組流,你看到的亂碼是轉成字元亂碼了
這也沒啥原因,你得知道接收的byte[]是什麼編碼產生的,然後使用相同的編碼才能解析出來。
一般亂碼了也就是GBK、UTF8不統一

㈡ java串口,讀取和發送數據

publicstaticvoidprocess(){
try{
EnumerationportList=CommPortIdentifier.getPortIdentifiers();
while(portList.hasMoreElements())
{
CommPortIdentifierportId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL)//如果埠類型是串口則判斷名稱
{
if(portId.getName().equals("COM1")){//如果是COM1埠則退出循環
break;
}else{
portId=null;
}
}
}
SerialPortserialPort=(SerialPort)portId.open("Serial_Communication",1000);//打開串口的超時時間為1000ms
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//設置串口速率為9600,數據位8位,停止位1們,奇偶校驗無
InputStreamin=serialPort.getInputStream();//得到輸入流
OutputStreamout=serialPort.getOutputStream();//得到輸出流

//進行輸入輸出操作
//操作結束後
in.close();
out.close();
serialPort.close();//關閉串口

}catch(PortInUseExceptione){
e.printStackTrace();
}catch(){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}

}

㈢ Java怎麼讀取串口數據

public static void process() {
try {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//如果埠類型是串口則判斷名稱
{
if(portId.getName().equals("COM1")){//如果是COM1埠則退出循環
break;
}else{
portId=null;
}
}
}
SerialPort serialPort = (SerialPort)portId.open("Serial_Communication", 1000);//打開串口的超時時間為1000ms
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//設置串口速率為9600,數據位8位,停止位1們,奇偶校驗無
InputStream in = serialPort.getInputStream();//得到輸入流
OutputStream out = serialPort.getOutputStream();//得到輸出流

//進行輸入輸出操作
//操作結束後
in.close();
out.close();
serialPort.close();//關閉串口

} catch (PortInUseException e) {
e.printStackTrace();
} catch ( e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

㈣ java串口編程中只能用InputStream來讀串口來的數據嗎

這兩個方法都是阻塞式的方法,readline 方法在流關閉或者緩沖區填滿之前,除非遇到回車或換行(\n 或\r)才會返回數據,否則就會一直阻塞在那裡等待數據
inputstream的read方法同樣是阻塞式方法,當流沒有關閉時,他就會一直在那裡阻塞等待數據。
試試多線程處理數據流,這樣就不需要一直等待流輸入了;或者在設備那邊每次傳輸完成都關閉流,但是每次都開啟關閉流好像消耗略大

㈤ 用java從串口讀取數據然後顯示在網頁上,能實現嗎

用java從串口讀取數據然後顯示在網頁上,能實現。
以下是對串口讀寫代碼,來自網友網路知道網友。其它如何傳遞到網頁自己搜索吧。
public static void process() {
try {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//如果埠類型是串口則判斷名稱
{
if(portId.getName().equals("COM1")){//如果是COM1埠則退出循環
break;
}else{
portId=null;
}
}
}
SerialPort serialPort = (SerialPort)portId.open("Serial_Communication", 1000);//打開串口的超時時間為1000ms
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//設置串口速率為9600,數據位8位,停止位1們,奇偶校驗無
InputStream in = serialPort.getInputStream();//得到輸入流
OutputStream out = serialPort.getOutputStream();//得到輸出流

//進行輸入輸出操作
//操作結束後
in.close();
out.close();
serialPort.close();//關閉串口

} catch (PortInUseException e) {
e.printStackTrace();
} catch ( e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}</ol>

㈥ 如何用Java語言向串口讀寫數據

public static void process() {
try {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//如果埠類型是串口則判斷名稱
{
if(portId.getName().equals("COM1")){//如果是COM1埠則退出循環
break;
}else{
portId=null;
}
}
}
SerialPort serialPort = (SerialPort)portId.open("Serial_Communication", 1000);//打開串口的超時時間為1000ms
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//設置串口速率為9600,數據位8位,停止位1們,奇偶校驗無
InputStream in = serialPort.getInputStream();//得到輸入流
OutputStream out = serialPort.getOutputStream();//得到輸出流

//進行輸入輸出操作
//操作結束後
in.close();
out.close();
serialPort.close();//關閉串口

} catch (PortInUseException e) {
e.printStackTrace();
} catch ( e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

㈦ java 多線程讀寫同一串口

串口本來是單向的,所以,讀的時候肯定寫不了(相反亦然)、一個程序(線程)寫了別的肯定寫不了。

所以,一個線程讀、另一線程寫。通過事件通知上層。 你懂。

㈧ 求java 關於串口數據讀取和寫入方法(最好解釋下)

Comm API基礎

我無意於在此詳細描述Comm API每個類和介面的用法,但我會介紹Comm API的類結構和幾個重要的API用法。

所有的comm API位於javax.comm包下面。從Comm API的javadoc來看,它介紹給我們的只有區區以下13個類或介面:

javax.comm.CommDriver

javax.comm.CommPort

javax.comm.ParallelPort

javax.comm.SerialPort

javax.comm.CommPortIdentifier

javax.comm.CommPortOwnershipListener

javax.comm.ParallelPortEvent

javax.comm.SerialPortEvent

javax.comm.ParallelPortEventListener (extends java.util.EventListener)

javax.comm.SerialPortEventListener (extends java.util.EventListener)

javax.comm.NoSuchPortException

javax.comm.PortInUseException

javax.comm.

下面講解一下幾個主要類或介面。

1.枚舉出系統所有的RS232埠

在開始使用RS232埠通訊之前,我們想知道系統有哪些埠是可用的,以下代碼列出系統中所有可用的RS232埠:

Enumeration en = CommPortIdentifier.getPortIdentifiers();

CommPortIdentifier portId;

while (en.hasMoreElements())

{

portId = (CommPortIdentifier) en.nextElement();

/*如果埠類型是串口,則列印出其埠信息*/

if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)

{

System.out.println(portId.getName());

}

}

在我的電腦上以上程序輸出以下結果:

COM1

COM2

CommPortIdentifier類的getPortIdentifiers方法可以找到系統所有的串口,每個串口對應一個CommPortIdentifier類的實例。

2.打開埠

如果你使用埠,必須先打開它。

try{

CommPort serialPort = portId.open("My App", 60);

/*從埠中讀取數據*/

InputStream input = serialPort.getInputStream();

input.read(...);

/*往埠中寫數據*/

OutputStream output = serialPort.getOutputStream();

output.write(...)

...

}catch(PortInUseException ex)

{ ... }

通過CommPortIdentifier的open方法可以返回一個CommPort對象。open方法有兩個參數,第一個是String,通常設置為你的應用程序的名字。第二個參數是時間,即開啟埠超時的毫秒數。當埠被另外的應用程序佔用時,將拋出PortInUseException異常。

在這里CommPortIdentifier類和CommPort類有什麼區別呢?其實它們兩者是一一對應的關系。CommPortIdentifier主要負責埠的初始化和開啟,以及管理它們的佔有權。而CommPort則是跟實際的輸入和輸出功能有關的。通過CommPort的getInputStream()可以取得埠的輸入流,它是java.io.InputStream介面的一個實例。我們可以用標準的InputStream的操作介面來讀取流中的數據,就像通過FileInputSteam讀取文件的內容一樣。相應的,CommPort的getOutputStream可以獲得埠的輸出流,這樣就可以往串口輸出數據了。

3.關閉埠

使用完的埠,必須記得將其關閉,這樣可以讓其它的程序有機會使用它,不然其它程序使用該埠時可能會拋出埠正在使用中的錯誤。很奇怪的是,CommPortIdentifier類只提供了開啟埠的方法,而要關閉埠,則要調用CommPort類的close()方法。
CommPort的輸入流的讀取方式與文件的輸入流有些不一樣,那就是你可能永遠不知這個InputStream何時結束,除非對方的OutputStream向你發送了一個特定數據表示發送結束,你收到這個特定字元後,再行關閉你的InputStream。而comm.jar提供了兩種靈活的方式讓你讀取數據。

1.輪詢方式(Polling)

舉個例子,你同GF相約一起出門去看電影,但你的GF好打扮,這一打扮可能就是半小時甚至一小時以上。這時你就耐不住了,每兩分鍾就催問一次「好了沒?」,如此這樣,直到你的GF說OK了才算完。這個就叫輪詢(Polling)。

在程序中,輪詢通常設計成一個封閉的循環,當滿足某個條件時即結束循環。剛才那個例子中,你的GF說「OK了!」,這個就是結束你輪詢的條件。在單線程的程序中,當循環一直執行某項任務而又無法預知它何時結束時,此時你的程序看起來可能就像死機一樣。在VB程序中,這個問題可以用在循環結構中插入一個doEvent語句來解決。而Java中,最好的方式是使用線程,就像以下代碼片斷一樣。

public TestPort extend Thread

{

...

InputStream input = serialPort.getInputStream();

StringBuffer buf = new StringBuffer();

boolean stopped = false;

...

public void run()

{

try {

while( !stopped )

int ch = input.read();

if ( ch=='q' || ch=='Q' )

{

/*結束讀取,關閉埠...*/

stopped = true;

...

}

else

{

buf.append((char)ch);

...

}

}catch (InterruptedException e) { }

}

}

2.監聽方式(listening)

Comm API支持標準的Java Bean型的事件模型。也就是說,你可以使用類似AddXXXListener這樣的方法為一個串口注冊自己的監聽器,以監聽方式進行數據讀取。

如要對埠監聽,你必須先取得CommPortIdentifier類的一個實例,

CommPort serialPort = portId.open("My App", 60);

從而取得SerialPort,再調用它的addEventListener方法為它添加監聽器,

serialPort.addEventListener(new MyPortListener());

SerialPort的監聽器必須繼承於SerialPortEventListener介面。當有任何SerialPort的事件發生時,將自動調用監聽器中的serialEvent方法。Serial Event有以下幾種類型:

BI -通訊中斷.

CD -載波檢測.

CTS -清除發送.

DATA_AVAILABLE -有數據到達.

DSR -數據設備准備好.

FE -幀錯誤.

OE -溢位錯誤.

OUTPUT_BUFFER_EMPTY -輸出緩沖區已清空.

PE -奇偶校驗錯.

RI -振鈴指示.

下面是一個監聽器的示例:

public void MyPortListener implements SerialPortEventListener

{

public void serialEvent(SerialPortEvent evt)

{

switch (evt.getEventType())

{

case SerialPortEvent.CTS :

System.out.println("CTS event occured.");

break;

case SerialPortEvent.CD :

System.out.println("CD event occured.");

break;

case SerialPortEvent.BI :

System.out.println("BI event occured.");

break;

case SerialPortEvent.DSR :

System.out.println("DSR event occured.");

break;

case SerialPortEvent.FE :

System.out.println("FE event occured.");

break;

case SerialPortEvent.OE :

System.out.println("OE event occured.");

break;

case SerialPortEvent.PE :

System.out.println("PE event occured.");

break;

case SerialPortEvent.RI :

System.out.println("RI event occured.");

break;

case SerialPortEvent.OUTPUT_BUFFER_EMPTY :

System.out.println("OUTPUT_BUFFER_EMPTY event occured.");

break;

case SerialPortEvent.DATA_AVAILABLE :

System.out.println("DATA_AVAILABLE event occured.");

int ch;

StringBuffer buf = new StringBuffer();

InputStream input = serialPort.getInputStream

try {

while ( (ch=input.read()) > 0) {

buf.append((char)ch);

}

System.out.print(buf);

} catch (IOException e) {}

break;

}

}

這個監聽器只是簡單列印每個發生的事件名稱。而對於大多數應用程序來說,通常關心是DATA_AVAILABLE事件,當數據從外部設備傳送到埠上來時將觸發此事件。此時就可以使用前面提到過的方法,serialPort.getInputStream()來從InputStream中讀取數據了。

㈨ 用java從串口讀取數據然後顯示在網頁上,能實現嗎

  1. 用java從串口讀取數據然後顯示在網頁上,能實現。

  2. 以下是對串口讀寫代碼,來自網友網路知道網友。其它如何傳遞到網頁自己搜索吧。

  3. public static void process() {

  4. try {

  5. Enumeration portList = CommPortIdentifier.getPortIdentifiers();

  6. while (portList.hasMoreElements())

  7. {

  8. CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();

  9. if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//如果埠類型是串口則判斷名稱

  10. {

  11. if(portId.getName().equals("COM1")){//如果是COM1埠則退出循環

  12. break;

  13. }else{

  14. portId=null;

  15. }

  16. }

  17. }

  18. SerialPort serialPort = (SerialPort)portId.open("Serial_Communication", 1000);//打開串口的超時時間為1000ms

  19. serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//設置串口速率為9600,數據位8位,停止位1們,奇偶校驗無

  20. InputStream in = serialPort.getInputStream();//得到輸入流

  21. OutputStream out = serialPort.getOutputStream();//得到輸出流

  22. //進行輸入輸出操作

  23. //操作結束後

  24. in.close();

  25. out.close();

  26. serialPort.close();//關閉串口

  27. } catch (PortInUseException e) {

  28. e.printStackTrace();

  29. } catch ( e) {

  30. e.printStackTrace();

  31. } catch (IOException e) {

  32. e.printStackTrace();

  33. }

  34. }

閱讀全文

與java讀串口相關的資料

熱點內容
安卓如何查看異常重啟 瀏覽:715
解壓音樂排名 瀏覽:383
安卓手機瀏覽器怎麼掃二維碼 瀏覽:715
通達信成本均線源碼 瀏覽:614
可以下載的解壓音頻 瀏覽:564
海賊王怎麼換伺服器 瀏覽:318
計算機上的共享文件夾映射 瀏覽:940
榮耀安裝包在文件夾哪裡 瀏覽:195
機票php源碼 瀏覽:231
linux共享mac 瀏覽:923
中國沒有國外的伺服器地址 瀏覽:759
為什麼退款伺服器連接錯誤 瀏覽:557
android簡訊存儲位置 瀏覽:972
unix網路編程卷4 瀏覽:808
找靚機app下單什麼時候發貨 瀏覽:413
android一個應用兩個進程 瀏覽:803
linux硬碟復制 瀏覽:808
php圖片伺服器搭建 瀏覽:801
下載壓縮文件怎麼打開 瀏覽:194
新建文件夾叫什麼名字 瀏覽:567