最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
JAVA编程中串口通信的方法介绍
时间:2022-11-14 23:25:24 编辑:袖梨 来源:一聚教程网
以前经常用C语言操作串口,实现一些发送接收之类的小功能,最近项目中也用到了串口通信,不过语言变成了Java,这个搞起来略蛋疼。
Java说起来就是各种库各种引用,所以得到某些需求先google搜一把~~比如之前搞半天的读取网页内容,自己边琢磨边写,又是 charset,又是proxy,又是什么browsertype的,晕头转向,最后拿Jsoup一会儿就搞定了……不过使用之前要充分考虑可扩展性和 API的丰富程度,不然后面程序体积变大集成起来很蛋疼。好了废话不说,讲讲串口的问题……
总述
Java常用的(我用到的)串口工具有俩:javacomm和RXTX,前者只支持win32,后者32和64位和其他版本都有。使用方法类似,所以就放到一起说:
下载到压缩包会看到readme之类的提示,然后把对应的dll文件和jar文件分别拷贝到/bin和/lib目录下,然后在出问题的时候继续查看readme。接着跑一下附带的例程。
具体使用过程就是:配置端口波特率、校验位等信息;打开串口;写数据;读输出;关闭。这一系列步骤在写程序的时候一定不要弄颠倒了。
串口配置
需要配置的有4项:
波特率(baudrate)– 一般9600、115200即可
(数据位)dataBits– 一般8 bits
DATABITS_5: 5 bits
DATABITS_6: 6 bits
DATABITS_7: 7 bits
DATABITS_8: 8 bits
(停止位)stopBits —— 一般1 stop bit
STOPBITS_1: 1 stop bit
STOPBITS_2: 2 stop bits
STOPBITS_1_5: 1.5 stop bits
(校验位)parity —— 不需校验位的话,选择SerialPort.PARITY_NONE
PARITY_NONE: no parity
PARITY_ODD: odd parity
PARITY_EVEN: even parity
PARITY_MARK: mark parity
PARITY_SPACE: space parity
流量控制模式(FlowControlMode) —— 一般FLOWCONTROL_NONE
FLOWCONTROL_NONE: no flow control
FLOWCONTROL_RTSCTS_IN: RTS/CTS (hardware) flow control for input
FLOWCONTROL_RTSCTS_OUT: RTS/CTS (hardware) flow control for output
FLOWCONTROL_XONXOFF_IN: XON/XOFF (software) flow control for input
FLOWCONTROL_XONXOFF_OUT:XON/XOFF (software) flow control for output
事件监听
addEventListener(SerialPortEventListener lsnr)
用于监听串口事件(SerialEvents),可监听的事件有:
BI(Break interrupt)
CD(Carrier detect)
CTS(Clear to send)
DATA_AVAILABLE(Data available at the serial port)
DSR(Data set ready)
eventType(Deprecated. Replaced by getEventType method)
FE(Framing error)
OE(Overrun error)
OUTPUT_BUFFER_EMPTY(Output buffer is empty)
PE(Parity error)
RI(Ring indicator)
读数据
通过SerialPortEventListener来,读取到DATA_AVAILABLE,保存并处理串口内容。
写数据
使用getOutputStream来向串口发送数据,是发送数据的唯一方法。使用时,向OutPutStream中写入byte串即可,之后用SerialPortEventListener获取回馈结果。
RXTX例程(来自这里 ):
public class USBComm implements SerialPortEventListener {
public static final String MESSAGE = "MESSAGE TO BE SEND";
/**
* Stream for the storage of incoming data
*/
private InputStream inputStream;
/**
* Stream for the dispatching of data
*/
private OutputStream outputStream;
/**
* Timeout of the USB port
*/
private final int PORT_TIMEOUT = 2000;
/**
* Representation of the serial port used for the communication
*/
public SerialPort serialPort;
/**
* Buffer that stores the received bytes from the media
*/
protected LinkedBlockingQueue
/**
* Builds a new manager for the communication via USB port.
* @exception IOException if an error occurred during the opening of the USB port
*/
public USBComm(String comm) throws IOException {
receivedBytes = new LinkedBlockingQueue
String port = comm; //place the right COM port here, OS dependent
//Check that the USB port exists and is recognized:
Enumeration> portList = CommPortIdentifier.getPortIdentifiers();
boolean portFound = false;
CommPortIdentifier portId = null;
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(port)) {
Log.logAndDisplay(LogLevel.INFO, "SerialConnection", "Found port: " + port);
portFound = true;
break;
}
}
}
if (!portFound)
throw new IOException("port " + port + " not found.");
try {
Log.logAndDisplay(LogLevel.INFO, "SerialConnection", "USB port " + port + " opening...");
serialPort = (SerialPort) portId.open("USBCommunicator", PORT_TIMEOUT);
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
Thread.sleep(1000);
serialPort.setSerialPortParams(serialPort.getBaudRate(),
serialPort.getDataBits(),
serialPort.getStopBits(),
serialPort.getParity());
serialPort.setFlowControlMode(serialPort.getFlowControlMode());
} catch (Exception e) {
System.err.println(e.getMessage());
throw new IOException(e.getMessage());
}
}
public void closeUSB(){
//close the streams for serial port:
try {
inputStream.close();
outputStream.close();
} catch (IOException e) {
System.err.println("Cannot close streams:" + e.getMessage());
}
}
/**
* Listener for USB events
*
* @param event new event occurred on the USB port
*/
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:
//nothing to do...
break;
case SerialPortEvent.DATA_AVAILABLE:
byte received = -1;
do {
try {
received = (byte)inputStream.read();
} catch (IOException e) {
System.err.println("Error reading USB:" + e.getMessage());
}
synchronized (receivedBytes) {
try {
receivedBytes.add(received);
} catch (IllegalStateException ew) {
System.err.println(ew.getMessage());
receivedBytes.poll();
receivedBytes.add(received);
}
}
} while(received != -1);
break;
}
}
public void write(byte[] buffer){
try {
outputStream.write(buffer);
//outputStream.flush();
} catch (IOException e) {
System.err.println("Cannot write:" + e.getMessage());
}
}
public static void main(String[] args){
USBComm usb;
try {
usb = new USBComm("COM73"); // change to your port
byte[] b = MESSAGE.getBytes();
usb.write(b);
usb.serialPort.close();
usb.closeUSB();
} catch (IOException e) {
System.err.println("IOException:" + e.getMessage());
}
}
}
相关文章
- 《潜行者2:切尔诺贝利之心》借刀杀人成就攻略分享 11-21
- 《潜行者2:切尔诺贝利之心》游戏保存方法介绍 11-21
- 《潜行者2:切尔诺贝利之心》游戏支线任务作用介绍 11-21
- 《潜行者2:切尔诺贝利之心》异常现象特点介绍 11-21
- 《潜行者2:切尔诺贝利之心》随机事件避免原因介绍 11-21
- 《潜行者2:切尔诺贝利之心》随机事件应对方法推荐 11-21