java串列埠通訊例項 -
package com.yuan;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.TooManyListenersException;
public class SerialComm implements SerialPortEventListener, Runnable {
public final static String PORT_OWER = "MonitorApp";
private boolean isOpen;
private boolean isStart;
private boolean isSave;
private boolean isPrint;
private Thread readThread;
private String portName;
private String portAddress;
private CommPortIdentifier portId;
private SerialPort serialPort;
private DataInputStream inputStream;
private OutputStream outputStream;
private SimpleDateFormat formatter;
private String dataProtocol;
private Object readEWriteLock = new Object();
public SerialComm(){
this.isOpen = false;
this.isStart = false;
this.isSave = true;
this.isPrint = false;
formatter = new SimpleDateFormat("[yyyy-MM-dd hh:mm:ss,SSS]");
portName = "COM1";
portAddress = "LOCAL";
dataProtocol = "Gooseli";
}
public void init(String port,String protocol ) throws Exception{
this.portName = port;
this.portAddress = portName;
this.dataProtocol = protocol;
init();
}
public void init(String port,String address,String protocol) throws Exception{
this.portName = port;
this.portAddress = address;
this.dataProtocol = protocol;
}
public void init()throws IOException,Exception{
if(isOpen){
close();
}
try{
portId = CommPortIdentifier.getPortIdentifier(portName);
serialPort = (SerialPort)portId.open(PORT_OWER,2000);
inputStream = new DataInputStream(serialPort.getInputStream());
outputStream = serialPort.getOutputStream();
isOpen = true;
}catch(NoSuchPortException ex){
throw new Exception(ex.toString());
}
}
public void start()throws Exception{
if(!isOpen){
throw new Exception(portName + " has not bean opened!");
}
try{
readThread = new Thread(this);
readThread.start();
serialPort.notifyOnDataAvailable(true);
serialPort.addEventListener(this);
isStart = true;
} catch( TooManyListenersException ex){
throw new Exception(ex.toString());
}
}
private void close() {
stop();
if(isOpen){
try{
inputStream.close();
outputStream.close();
serialPort.close();
isOpen = false;
}catch(IOException ex){
}
}
}
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:// 當有可用資料時讀取資料,並且給串列埠返回資料
readComm();
/*byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
System.out.println(inputStream.available());
int numBytes = inputStream.read(readBuffer);
System.out.println(numBytes);
}
data = new String(readBuffer).trim();
System.out.println(new String(readBuffer).trim());
} catch (IOException e) {
e.fillInStackTrace();
}*/
break;
default:break;
}
}
private void readComm() {
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
System.out.println(inputStream.available());
int numBytes = inputStream.read(readBuffer);
}
//data = new String(readBuffer).trim();
System.out.println(new String(readBuffer).trim());
} catch (IOException e) {
e.fillInStackTrace();
}
/*StringBuffer readBuffer = new StringBuffer();
String scannedInput = "";
Date currentTime = null;
String TimeStamp = "";
int c ;
char a;
try {
InputStreamReader fis = new InputStreamReader(inputStream,"utf-8");
while((c=fis.read())!=-1){
readBuffer.append((char)c);
}
scannedInput = readBuffer.toString().trim();
currentTime = new Date();
TimeStamp = formatter.format(currentTime);
} catch(IOException ex ){
ex.printStackTrace();
} catch(Exception exx){
exx.printStackTrace();
}*/
}
public void run() {
/*while(true){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String at = "test";
writeComm(at);
isPrint = true;
}*/
/*String at = "at^hcmgr=1\r";
String strTemp = at+(char)Integer.parseInt("la",16) + "z";
writeComm(strTemp);*/
}
private void writeComm(String outString) {
synchronized(readEWriteLock){
try{
outputStream.write(outString.getBytes());
} catch(IOException ex){
ex.printStackTrace();
}
}
}
public void stop(){
if(isStart){
serialPort.notifyOnDataAvailable(false);
serialPort.removeEventListener();
isStart =false;
}
}
public static void main(String[] args){
SerialComm serialComm = new SerialComm();
try {
serialComm.init("COM2","Air");
serialComm.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.TooManyListenersException;
public class SerialComm implements SerialPortEventListener, Runnable {
public final static String PORT_OWER = "MonitorApp";
private boolean isOpen;
private boolean isStart;
private boolean isSave;
private boolean isPrint;
private Thread readThread;
private String portName;
private String portAddress;
private CommPortIdentifier portId;
private SerialPort serialPort;
private DataInputStream inputStream;
private OutputStream outputStream;
private SimpleDateFormat formatter;
private String dataProtocol;
private Object readEWriteLock = new Object();
public SerialComm(){
this.isOpen = false;
this.isStart = false;
this.isSave = true;
this.isPrint = false;
formatter = new SimpleDateFormat("[yyyy-MM-dd hh:mm:ss,SSS]");
portName = "COM1";
portAddress = "LOCAL";
dataProtocol = "Gooseli";
}
public void init(String port,String protocol ) throws Exception{
this.portName = port;
this.portAddress = portName;
this.dataProtocol = protocol;
init();
}
public void init(String port,String address,String protocol) throws Exception{
this.portName = port;
this.portAddress = address;
this.dataProtocol = protocol;
}
public void init()throws IOException,Exception{
if(isOpen){
close();
}
try{
portId = CommPortIdentifier.getPortIdentifier(portName);
serialPort = (SerialPort)portId.open(PORT_OWER,2000);
inputStream = new DataInputStream(serialPort.getInputStream());
outputStream = serialPort.getOutputStream();
isOpen = true;
}catch(NoSuchPortException ex){
throw new Exception(ex.toString());
}
}
public void start()throws Exception{
if(!isOpen){
throw new Exception(portName + " has not bean opened!");
}
try{
readThread = new Thread(this);
readThread.start();
serialPort.notifyOnDataAvailable(true);
serialPort.addEventListener(this);
isStart = true;
} catch( TooManyListenersException ex){
throw new Exception(ex.toString());
}
}
private void close() {
stop();
if(isOpen){
try{
inputStream.close();
outputStream.close();
serialPort.close();
isOpen = false;
}catch(IOException ex){
}
}
}
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:// 當有可用資料時讀取資料,並且給串列埠返回資料
readComm();
/*byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
System.out.println(inputStream.available());
int numBytes = inputStream.read(readBuffer);
System.out.println(numBytes);
}
data = new String(readBuffer).trim();
System.out.println(new String(readBuffer).trim());
} catch (IOException e) {
e.fillInStackTrace();
}*/
break;
default:break;
}
}
private void readComm() {
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
System.out.println(inputStream.available());
int numBytes = inputStream.read(readBuffer);
}
//data = new String(readBuffer).trim();
System.out.println(new String(readBuffer).trim());
} catch (IOException e) {
e.fillInStackTrace();
}
/*StringBuffer readBuffer = new StringBuffer();
String scannedInput = "";
Date currentTime = null;
String TimeStamp = "";
int c ;
char a;
try {
InputStreamReader fis = new InputStreamReader(inputStream,"utf-8");
while((c=fis.read())!=-1){
readBuffer.append((char)c);
}
scannedInput = readBuffer.toString().trim();
currentTime = new Date();
TimeStamp = formatter.format(currentTime);
} catch(IOException ex ){
ex.printStackTrace();
} catch(Exception exx){
exx.printStackTrace();
}*/
}
public void run() {
/*while(true){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String at = "test";
writeComm(at);
isPrint = true;
}*/
/*String at = "at^hcmgr=1\r";
String strTemp = at+(char)Integer.parseInt("la",16) + "z";
writeComm(strTemp);*/
}
private void writeComm(String outString) {
synchronized(readEWriteLock){
try{
outputStream.write(outString.getBytes());
} catch(IOException ex){
ex.printStackTrace();
}
}
}
public void stop(){
if(isStart){
serialPort.notifyOnDataAvailable(false);
serialPort.removeEventListener();
isStart =false;
}
}
public static void main(String[] args){
SerialComm serialComm = new SerialComm();
try {
serialComm.init("COM2","Air");
serialComm.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
相關文章
- 串列埠通訊串列埠
- linux 串列埠通訊Linux串列埠
- Android 串列埠通訊Android串列埠
- 11. 串列埠通訊串列埠
- (10)uart串列埠通訊串列埠
- 串列埠通訊型別串列埠型別
- 串列埠通訊協議串列埠協議
- 串列埠資料抓取及串列埠通訊模擬串列埠
- 通過串列埠進行通訊 :串列埠
- C# SerialPort 串列埠通訊C#串列埠
- 串列埠無法正常通訊串列埠
- Java實現RS485串列埠通訊Java串列埠
- Java的Socket通訊簡單例項Java單例
- 小型plc串列埠通訊簡介串列埠
- 安卓串列埠通訊疑問安卓串列埠
- ROS環境下串列埠通訊ROS串列埠
- 串列埠通訊gui介面顯示串列埠GUI
- AndroidSerialPort:安卓串列埠通訊庫Android安卓串列埠
- C# 串列埠通訊利器 SerialPortStream庫C#串列埠
- STM32串列埠通訊串列埠
- .net和java串列埠通訊壓力測試對比, java完勝Java串列埠
- 如何使用Java串列埠進行資料通訊及應用案例Java串列埠
- STMF4串列埠通訊使用串列埠
- 串列埠通訊與其他通訊方式相比有什麼優勢?串列埠
- 串列埠通訊利器:SerialPortStream庫詳解,輕鬆實現C#串列埠開發串列埠C#
- C#實現掃碼槍串列埠通訊C#串列埠
- 打工筆記--------------------------c#實現串列埠通訊筆記C#串列埠
- ros中使用serial包實現串列埠通訊ROS串列埠
- Arduino下的STM32的串列埠通訊UI串列埠
- 樹莓派已經通過網路連線通過串列埠通訊在串列埠除錯小助手列印與操作樹莓派串列埠除錯
- 基於WebSocket的modbus通訊(三)- websocket和串列埠Web串列埠
- 串列埠通訊上位機資料傳輸協議串列埠協議
- 定位模組LuatOS快速入門:源UART串列埠通訊串列埠
- 串列埠通訊常見的錯誤和故障排除方法串列埠
- 使用Modbus4J進行RTU模式串列埠通訊模式串列埠
- 一種MODBUS RTU擴充套件串列埠通訊協議套件串列埠協議
- 超級乾貨!Air780E的串列埠通訊分享AI串列埠
- 張高興的 MicroPython 入門指南:(三)使用串列埠通訊Python串列埠
- 初步使用Ardunio IDE實現STM32的串列埠通訊IDE串列埠