java串口读写问题 写了一个串口通信程序,结果得到的不是我想要的结果。本来我是想输入hello。得到的也是 10
hello但是得到却是hello程序是这样的请各位高手帮我看看怎么才能得到想要的结果importjavax.comm.*;importjavax.swing.*;impo...
hello 但是得到却是
h
e
l
l
o
程序是这样的 请各位高手帮我看看怎么才能得到想要的结果
import javax.comm.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Test implements SerialPortEventListener {
public SerialPort serialport;
public CommPortIdentifier portId;
public Enumeration portList;
public InputStream inputStream;
public OutputStream outputStream;
public void find(String s)
{portList=CommPortIdentifier.getPortIdentifiers();
while(portList.hasMoreElements()){
CommPortIdentifier temp=(CommPortIdentifier) portList.nextElement();
if(temp.getPortType()==CommPortIdentifier.PORT_SERIAL){
if(temp.getName().equals(s))//对串口进行匹配
{portId=temp;}
}
}
OpenPort(portId);//打开串口
}
/*
* 打开串口并进行配置
*/
public void OpenPort(CommPortIdentifier portId)
{
try {serialport = (SerialPort) portId.open("mytest", 2000);
} catch (PortInUseException e) {
System.out.println("端口被占用");
}
try { //设置输入输出流
inputStream = serialport.getInputStream();
outputStream=serialport.getOutputStream();
} catch (IOException e) {e.printStackTrace();}
try { //设置监听
serialport.addEventListener(this);
} catch (TooManyListenersException e) {e.printStackTrace();}
serialport.notifyOnDataAvailable(true);
try { //配置串口
serialport.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);}
catch (UnsupportedCommOperationException e) {System.out.println("初始化串口失败");}
}
/*
* 发送数据
*/
public void send(String data)
{
try {outputStream.write(data.getBytes());} catch (IOException e) {e.printStackTrace();}
}
/*
* 事件判断
*/
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[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.println(new String(readBuffer).trim());
} catch (IOException e) {e.printStackTrace()}
break; } }
/*
* 关闭串口
*/
public void ClosePort()
{
serialport.close();
} 展开
h
e
l
l
o
程序是这样的 请各位高手帮我看看怎么才能得到想要的结果
import javax.comm.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Test implements SerialPortEventListener {
public SerialPort serialport;
public CommPortIdentifier portId;
public Enumeration portList;
public InputStream inputStream;
public OutputStream outputStream;
public void find(String s)
{portList=CommPortIdentifier.getPortIdentifiers();
while(portList.hasMoreElements()){
CommPortIdentifier temp=(CommPortIdentifier) portList.nextElement();
if(temp.getPortType()==CommPortIdentifier.PORT_SERIAL){
if(temp.getName().equals(s))//对串口进行匹配
{portId=temp;}
}
}
OpenPort(portId);//打开串口
}
/*
* 打开串口并进行配置
*/
public void OpenPort(CommPortIdentifier portId)
{
try {serialport = (SerialPort) portId.open("mytest", 2000);
} catch (PortInUseException e) {
System.out.println("端口被占用");
}
try { //设置输入输出流
inputStream = serialport.getInputStream();
outputStream=serialport.getOutputStream();
} catch (IOException e) {e.printStackTrace();}
try { //设置监听
serialport.addEventListener(this);
} catch (TooManyListenersException e) {e.printStackTrace();}
serialport.notifyOnDataAvailable(true);
try { //配置串口
serialport.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);}
catch (UnsupportedCommOperationException e) {System.out.println("初始化串口失败");}
}
/*
* 发送数据
*/
public void send(String data)
{
try {outputStream.write(data.getBytes());} catch (IOException e) {e.printStackTrace();}
}
/*
* 事件判断
*/
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[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.println(new String(readBuffer).trim());
} catch (IOException e) {e.printStackTrace()}
break; } }
/*
* 关闭串口
*/
public void ClosePort()
{
serialport.close();
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询