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 }
}
}