java 實現微控制器與PC串列埠通訊

opshres169發表於2016-06-28
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   } 
 } 
}

相關文章