熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

通過JAVA與串口(RS232)通信實例

2022-06-13   來源: Java核心技術 

  最近了解到的需求是需要需激光打刻機進行(RS)串口通信這裡使用的是RXTX開源包實現的

  之前並沒有用java做過串口通信而且這方面資料不是很多

  項目實際應用中可能會采用VB開發(這個我就不會了)

  只不過用java嘗試一下記個筆記希望可以對相關開發用些幫助

  下面是實現代碼

  Java代碼

  package test;

  import javaioIOException;

  import javaioInputStream;

  import javaioInputStreamReader;

  import javaioOutputStream;

  import javautilDate;

  import javautilEnumeration;

  import javautilTooManyListenersException;

  import gnuioCommPortIdentifier;

  import gnuioPortInUseException;

  import gnuioSerialPort;

  import gnuioSerialPortEvent;

  import gnuioSerialPortEventListener;

  import gnuioUnsupportedCommOperationException;

  public class CommUtil implements SerialPortEventListener {

  InputStream inputStream; // 從串口來的輸入流

  OutputStream outputStream;// 向串口輸出的流

  SerialPort serialPort; // 串口的引用

  CommPortIdentifier portId;

  public CommUtil(Enumeration portList String name) {

  while (portListhasMoreElements()) {

  CommPortIdentifier temp = (CommPortIdentifier) portListnextElement();

  if (tempgetPortType() == CommPortIdentifierPORT_SERIAL) {// 判斷如果端口類型是串口

  if (tempgetName()equals(name)) { // 判斷如果端口已經啟動就連接

  portId = temp;

  }

  }

  }

  try {

  serialPort = (SerialPort) portIdopen(My+name );

  } catch (PortInUseException e) {

  }

  try {

  inputStream = serialPortgetInputStream();

  outputStream = serialPortgetOutputStream();

  } catch (IOException e) {

  }

  try {

  serialPortaddEventListener(this); // 給當前串口天加一個監聽器

  } catch (TooManyListenersException e) {

  }

  serialPortnotifyOnDataAvailable(true); // 當有數據時通知

  try {

  serialPortsetSerialPortParams( SerialPortDATABITS_ // 設置串口讀寫參數

  SerialPortSTOPBITS_ SerialPortPARITY_NONE);

  } catch (UnsupportedCommOperationException e) {

  }

  }

  public void serialEvent(SerialPortEvent event) {

  switch (eventgetEventType()) {

  case SerialPortEventBI:

  case SerialPortEventOE:

  case SerialPortEventFE:

  case SerialPortEventPE:

  case SerialPortEventCD:

  case SerialPortEventCTS:

  case SerialPortEventDSR:

  case SerialPortEventRI:

  case SerialPortEventOUTPUT_BUFFER_EMPTY:

  break;

  case SerialPortEventDATA_AVAILABLE:// 當有可用數據時讀取數據並且給串口返回數據

  byte[] readBuffer = new byte[];

  try {

  while (inputStreamavailable() > ) {

  Systemoutprintln(inputStreamavailable());

  int numBytes = inputStreamread(readBuffer);

  Systemoutprintln(numBytes);

  }

  Systemoutprintln(new String(readBuffer)trim());

  } catch (IOException e) {

  eprintStackTrace();

  }

  break;

  }

  }

  public void send(String content){

  try {

  outputStreamwrite(contentgetBytes());

  } catch (IOException e) {

  eprintStackTrace();

  }

  }

  public void ClosePort() {

  if (serialPort != null) {

  serialPortclose();

  }

  }

  }

  測試

  Java代碼

  package test;

  import gnuioCommPortIdentifier;

  import javautilEnumeration;

  public class Test {

  public static void main(String[] args) throws InterruptedException {

  Enumeration portList = CommPortIdentifiergetPortIdentifiers(); //得到當前連接上的端口

  CommUtil comm = new CommUtil(portListCOM);

  int i = ;

  while(i<)

  {

  Threadsleep();

  commsend(hello);

  i++;

  }

  commClosePort();

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25965.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.