java从串口读取固定数据_java 串口编程—-设置缓冲屈来读取串口数据 | 学步园...

package test;

import java.io.InputStream;

import java.io.OutputStream;

import javax.comm.CommPortIdentifier;

import javax.comm.SerialPort;

//串口操作主类

public class SerialBean {

static String portName;

CommPortIdentifier portId;

SerialPort serialPort;

static InputStream in;

static OutputStream out;

SerialBuffer sb;

ReadSeriral rt;

public SerialBean(int portId){

portName="COM"+String.valueOf(portId);

}

public int Initialize(){

int initsuccess=1;

int initfail=-1;

try {

portId=CommPortIdentifier.getPortIdentifier(portName);

try {

serialPort=(SerialPort)portId.open("serial_communication",2000);

in=serialPort.getInputStream();

out=serialPort.getOutputStream();

serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

} catch (Exception e) {

System.out.println("初始化串口失败!");

return initfail;

}

sb=new SerialBuffer();

rt=new ReadSeriral(sb,in);

rt.start();

return initsuccess;

} catch (Exception e) {

System.out.print("初始化串口失败!");

return initfail;

}

}

public String ReadPort(int length){

String msg="";

msg=sb.getMsg(length);

return msg;

}

public void WritePort(String msg){

try {

for(int i=0;i

out.write(msg.charAt(i));

}

} catch (Exception e) {

e.printStackTrace();

}

}

public void ClosePort(){

try {

rt.stop();

serialPort.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public String getPortName(){

return portName;

}

public static void main(String[] args) {

SerialBean sb=new SerialBean(1);

String msg;

sb.Initialize();

System.out.println("串口"+sb.getPortName()+"初始化成功!");

for (int i = 5; i <= 10; i++) {

msg=sb.ReadPort(i);

sb.WritePort("reply:"+msg);

}

sb.ClosePort();

}

}

=============================================================================

package test;

public class SerialBuffer {

private String contentString="";

private String currentMsg,tempContent;

private boolean available=false;

private int lengthNeeded=1;

public synchronized String getMsg(int length) {

lengthNeeded=length;

notifyAll();

if(lengthNeeded>contentString.length()){

available=false;

while(available==false){

try {

wait();

} catch (Exception e) {

e.printStackTrace();

}

}

}

currentMsg=contentString.substring(0,lengthNeeded);

tempContent=contentString.substring(lengthNeeded);

contentString=tempContent;

lengthNeeded=1;

notifyAll();

return currentMsg;

}

public synchronized void putChar(int c) {

Character d=new Character((char)c);

contentString=contentString.concat(d.toString());

if(lengthNeeded

available=true;

}

notifyAll();

}

}

===================================================================

package test;

import java.io.InputStream;

public class ReadSeriral extends Thread{

private SerialBuffer commBuffer;

private InputStream commPort;

public ReadSeriral(SerialBuffer sb,InputStream port){

commBuffer=sb;

commPort=port;

}

public void run(){

int c;

try {

while(true){

c=commPort.read();

commBuffer.putChar(c);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于Java读取串口设备信息的问题,可以通过Java提供的串口通信API来实现。下面是一个简单的示例代码,可以帮助你了解如何使用Java读取串口设备信息。 ```java import java.io.*; import java.util.*; import gnu.io.*; public class SerialTest implements SerialPortEventListener { SerialPort serialPort; public void initialize() { CommPortIdentifier portId = null; Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); while (portEnum.hasMoreElements()) { CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement(); if (currPortId.getName().equals("COM1")) { portId = currPortId; break; } } if (portId == null) { System.out.println("Could not find COM port."); return; } try { serialPort = (SerialPort) portId.open(this.getClass().getName(), 2000); serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); InputStream in = serialPort.getInputStream(); serialPort.addEventListener(this); serialPort.notifyOnDataAvailable(true); } catch (Exception e) { System.err.println(e.toString()); } } public synchronized void close() { if (serialPort != null) { serialPort.removeEventListener(); serialPort.close(); } } public synchronized void serialEvent(SerialPortEvent oEvent) { if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { String inputLine = ""; BufferedReader reader = new BufferedReader(new InputStreamReader(serialPort.getInputStream())); inputLine = reader.readLine(); System.out.println("Received: " + inputLine); } catch (Exception e) { System.err.println(e.toString()); } } } public static void main(String[] args) { SerialTest main = new SerialTest(); main.initialize(); System.out.println("Started"); } } ``` 该示例代码通过Java串口通信API来与串口设备进行通信,包括设置串口参数、监听串口事件、读取串口数据等。其中,`initialize()`方法用于初始化串口,`close()`方法用于关闭串口,`serialEvent()`方法用于处理串口事件,`main()`方法用于启动程序并初始化串口。 需要注意的是,在使用Java串口通信API时,需要先安装对应的驱动程序,并且需要在代码中指定正确的串口名称(例如COM1)。此外,不同的串口设备可能需要不同的串口参数设置,需要根据实际情况进行调整。 另外,如果需要读取扫描枪等设备的数据,则需要根据设备的数据格式进行解析。一般来说,扫描枪会将扫描的条码数据以字符串的形式发送到串口设备,因此可以通过读取串口数据来获取扫描的条码数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值