java 實現微控制器與PC串列埠通訊
Java 實現微控制器與PC串列埠通訊
在用Eclipse做開發時,我發現利用Eclipse控制檯可以很方便的實現PC與微控制器的串列埠通訊,這樣可以省略自己做一個GUI的步驟,直接利用Console做串列埠通訊終端。
實現程式碼如下:
package commPort;
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration;
import java.util.Scanner;
import java.util.TooManyListenersException;
import javax.comm.CommPortIdentifier; import javax.comm.PortInUseException; import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException; /**
* <dl>
* <dt><b>類說明:</b></dt><dd> *
* </dd> * </dl>
* @copyright :Copyright 2012. All right reserved. *【Update History】
* Version Date Time Company Name
Department
* ------- ---------- ------------ --------- ---------- ----------- * 1.00 2012-12-27 上午8:13:18 Jason Wei
*/
public class Read_Write { /**
* @param args */
public static void main(String[] args) { CommUtils commUtil=new CommUtils("COM12");
String str1;
var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/baichuan/ns.js'; document.body.appendChild(script);
String str2; java.util.Scanner in=new Scanner(System.in); while(true) {
str1=in.nextLine(); commUtil.write(str1);
commUtil.run(); // str2=commUtil.read(); // System.out.print(str2);
}
}
}
class CommUtils implements SerialPortEventListener, Runnable { CommPortIdentifier portId; Enumeration<?> portList; SerialPort serialPort; InputStream inputStream; OutputStream outputStream; String str_reader; String str_writer; Thread readThread; String[] commPortList;
public CommUtils(String commName) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { CommPortIdentifier temp; // portList.
temp = (CommPortIdentifier) portList.nextElement();
if (temp.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (temp.getName().equals(commName)) { portId = temp; System.out.println("connecting to " + commName);
}
} }
try { serialPort = (SerialPort) portId.open(commName, 2000);
} catch (PortInUseException e) {
var cpro_psid ="u2572954"; var cpro_pswidth =966; var cpro_psheight =120;
// TODO: handle exception
System.out.println(e); } try { inputStream = serialPort.getInputStream(); outputStream = serialPort.getOutputStream(); } catch (Exception e) { // TODO: handle exception
} try {
serialPort.addEventListener(this); } catch (TooManyListenersException e) { // TODO Auto-generated catch block e.printStackTrace(); }
serialPort.notifyOnDataAvailable(true); try { serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);// } catch (UnsupportedCommOperationException e) { }
readThread = new Thread(this); readThread.start();
}
@Override
public void serialEvent(SerialPortEvent event) { 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: byte[] readBuffer = new byte[2000]; try { while (inputStream.available() > 0) {
inputStream.read(readBuffer);
str_reader = new String(readBuffer).trim();
System.out.println(str_reader); }
} catch (IOException e) { // TODO: handle exception }
break;
}
}
public String read() { return str_reader;
}
public void write(String str) { try { outputStream.write(str.getBytes()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();
}
}
public boolean isOwned() { return portId.isCurrentlyOwned(); }
public void closePort() { serialPort.close();
}
@Override
public void run() { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO: handle exception }
}
}
or
str_reader = new String(readBuffer).trim();
System.out.println(str_reader); }
} catch (IOException e) { // TODO: handle exception }
break;
}
}
public String read() { return str_reader;
}
public void write(String str) { try { outputStream.write(str.getBytes()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();
}
}
public boolean isOwned() { return portId.isCurrentlyOwned(); }
public void closePort() { serialPort.close();
}
@Override
public void run() { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO: handle exception }
}
}
在用Eclipse做開發時,我發現利用Eclipse控制檯可以很方便的實現PC與微控制器的串列埠通訊,這樣可以省略自己做一個GUI的步驟,直接利用Console做串列埠通訊終端。
實現程式碼如下:
package commPort;
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration;
import java.util.Scanner;
import java.util.TooManyListenersException;
import javax.comm.CommPortIdentifier; import javax.comm.PortInUseException; import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException; /**
* <dl>
* <dt><b>類說明:</b></dt><dd> *
* </dd> * </dl>
* @copyright :Copyright 2012. All right reserved. *【Update History】
* Version Date Time Company Name
Department
* ------- ---------- ------------ --------- ---------- ----------- * 1.00 2012-12-27 上午8:13:18 Jason Wei
*/
public class Read_Write { /**
* @param args */
public static void main(String[] args) { CommUtils commUtil=new CommUtils("COM12");
String str1;
var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/baichuan/ns.js'; document.body.appendChild(script);
String str2; java.util.Scanner in=new Scanner(System.in); while(true) {
str1=in.nextLine(); commUtil.write(str1);
commUtil.run(); // str2=commUtil.read(); // System.out.print(str2);
}
}
}
class CommUtils implements SerialPortEventListener, Runnable { CommPortIdentifier portId; Enumeration<?> portList; SerialPort serialPort; InputStream inputStream; OutputStream outputStream; String str_reader; String str_writer; Thread readThread; String[] commPortList;
public CommUtils(String commName) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { CommPortIdentifier temp; // portList.
temp = (CommPortIdentifier) portList.nextElement();
if (temp.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (temp.getName().equals(commName)) { portId = temp; System.out.println("connecting to " + commName);
}
} }
try { serialPort = (SerialPort) portId.open(commName, 2000);
} catch (PortInUseException e) {
var cpro_psid ="u2572954"; var cpro_pswidth =966; var cpro_psheight =120;
// TODO: handle exception
System.out.println(e); } try { inputStream = serialPort.getInputStream(); outputStream = serialPort.getOutputStream(); } catch (Exception e) { // TODO: handle exception
} try {
serialPort.addEventListener(this); } catch (TooManyListenersException e) { // TODO Auto-generated catch block e.printStackTrace(); }
serialPort.notifyOnDataAvailable(true); try { serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);// } catch (UnsupportedCommOperationException e) { }
readThread = new Thread(this); readThread.start();
}
@Override
public void serialEvent(SerialPortEvent event) { 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: byte[] readBuffer = new byte[2000]; try { while (inputStream.available() > 0) {
inputStream.read(readBuffer);
str_reader = new String(readBuffer).trim();
System.out.println(str_reader); }
} catch (IOException e) { // TODO: handle exception }
break;
}
}
public String read() { return str_reader;
}
public void write(String str) { try { outputStream.write(str.getBytes()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();
}
}
public boolean isOwned() { return portId.isCurrentlyOwned(); }
public void closePort() { serialPort.close();
}
@Override
public void run() { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO: handle exception }
}
}
or
str_reader = new String(readBuffer).trim();
System.out.println(str_reader); }
} catch (IOException e) { // TODO: handle exception }
break;
}
}
public String read() { return str_reader;
}
public void write(String str) { try { outputStream.write(str.getBytes()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();
}
}
public boolean isOwned() { return portId.isCurrentlyOwned(); }
public void closePort() { serialPort.close();
}
@Override
public void run() { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO: handle exception }
}
}
相關文章
- Java實現RS485串列埠通訊Java串列埠
- FX-3U PLC串列埠與PC通訊除錯串列埠除錯
- java串列埠通訊例項 -Java串列埠
- 串列埠通訊串列埠
- AVR 微控制器串列埠和網口的通訊VR串列埠
- ros中使用serial包實現串列埠通訊ROS串列埠
- 打工筆記--------------------------c#實現串列埠通訊筆記C#串列埠
- C#實現掃碼槍串列埠通訊C#串列埠
- 串列埠通訊 (轉)串列埠
- 52微控制器使用T2串列埠通訊串列埠
- 串列埠通訊利器:SerialPortStream庫詳解,輕鬆實現C#串列埠開發串列埠C#
- linux 串列埠通訊Linux串列埠
- 串列埠通訊協議串列埠協議
- Android 串列埠通訊Android串列埠
- C# 串列埠通訊C#串列埠
- 11. 串列埠通訊串列埠
- 串列埠通訊型別串列埠型別
- (10)uart串列埠通訊串列埠
- 通過串列埠進行通訊 :串列埠
- 在Java中操作串列埠實現簡訊收發Java串列埠
- 串列埠資料抓取及串列埠通訊模擬串列埠
- 串列埠通訊與其他通訊方式相比有什麼優勢?串列埠
- 基於AVR的串列埠與PC機通訊程式碼(uart8位資料)VR串列埠
- 安卓串列埠通訊疑問安卓串列埠
- VC++ 串列埠通訊(轉)C++串列埠
- 串列埠無法正常通訊串列埠
- 初步使用Ardunio IDE實現STM32的串列埠通訊IDE串列埠
- Linux下串列埠通訊詳解(下)讀寫串列埠及關閉串列埠Linux串列埠
- AndroidSerialPort:安卓串列埠通訊庫Android安卓串列埠
- 串列埠通訊gui介面顯示串列埠GUI
- ROS環境下串列埠通訊ROS串列埠
- Android藍芽串列埠通訊Android藍芽串列埠
- VC++ 的串列埠通訊 (轉)C++串列埠
- 小型plc串列埠通訊簡介串列埠
- 序列通訊的基本原理及用MFC實現串列埠通訊程式設計 (轉)串列埠程式設計
- 串列埠通訊系列六-串列埠與上位機通訊呼叫Flash及Flash視訊切換(非互動)串列埠
- STM32串列埠通訊串列埠
- C#串列埠通訊遇到的坑C#串列埠