如何在java中向串行端口写入字节序列
我是java的初学者。我必须通过com端口向正在使用的仪器发送5个连续整数。我正在使用rxtx库。仪器对我的代码没有反应。我尝试将值初始化为string,然后使用getbytes将其更改为byte。但即使这样也没用。如果有人能帮助我,那将是一个很大的帮助。该仪器在COM5处识别。我试图先写入Com端口。数据可用事件被激发。但inputstream读取的是无法识别的字符。我认为问题在于写入端口。如果有人能帮助我,那将是一个很大的帮助 设备的串行接口规格如下所示 DIR START CMD VALLO VALHI END → 53 1 4 0 83 ← 53 1 4 0 83 ← 53 27 SLO SHI 83 事务由以下数据包序列定义 •→ 软件发送一个请求包 •← 仪器发送一个响应包 •← 仪器发送一个状态包 这表示整个运行中的单个事务 每次启动;CMD;瓦洛;瓦利;上面的END表示数据包中的一个字节如何在java中向串行端口写入字节序列,java,serial-port,Java,Serial Port,我是java的初学者。我必须通过com端口向正在使用的仪器发送5个连续整数。我正在使用rxtx库。仪器对我的代码没有反应。我尝试将值初始化为string,然后使用getbytes将其更改为byte。但即使这样也没用。如果有人能帮助我,那将是一个很大的帮助。该仪器在COM5处识别。我试图先写入Com端口。数据可用事件被激发。但inputstream读取的是无法识别的字符。我认为问题在于写入端口。如果有人能帮助我,那将是一个很大的帮助 设备的串行接口规格如下所示 DIR START CMD VALL
public class ReadWrite implements SerialPortEventListener {
OutputStream outputStream;
InputStream inputStream;
static SerialPort serialPort;
public static void main(String[] args) throws Exception {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("SimpleRead Started.");
while (portList.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println("Found " + portId.getName());
if (portId.getName().equals("COM5")) {
serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
ReadWrite reader = new ReadWrite();
}
}
}
}
public ReadWrite() throws IOException {
try {
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
outputStream.write(53);
outputStream.flush();
outputStream.write(1);
outputStream.flush();
outputStream.write(4);
outputStream.flush();
outputStream.write(0);
outputStream.flush();
outputStream.write(83);
outputStream.flush();
System.out.println("The port in use is " + serialPort);
System.out.println("write done");
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
serialPort.notifyOnDataAvailable(true);
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
System.out.println("The serial port event is 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:
System.out.println("The serial port event is OUTPUT_BUFFER_EMPTY ");
break;
case SerialPortEvent.DATA_AVAILABLE:
System.out.println("The serial port event is Data available ");
byte[] readBuffer = new byte[20];
System.out.println(event.getEventType());
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
System.out.print(new String(readBuffer, 0, numBytes, "us-ascii"));
}
System.out.print(new String(readBuffer));
} catch (IOException e) {
e.printStackTrace();
}
serialPort.close();
break;
}
}
}
问题在于阅读部分。我必须使用getBytes将字符串转换为字节数组,因为有一个字节流来自模块。然后我一张一张地打印出来
public class TwoWaySerialComm {
void connect( String portName ) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier( portName );
if( portIdentifier.isCurrentlyOwned() ) {
System.out.println( "Error: Port is currently in use" );
} else {
int timeout = 10000;
CommPort commPort = portIdentifier.open( this.getClass().getName(), timeout );
if( commPort instanceof SerialPort ) {
SerialPort serialPort = ( SerialPort )commPort;
serialPort.setSerialPortParams( 38400,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE );
InputStream in = serialPort.getInputStream();
OutputStream outputStream = serialPort.getOutputStream();
outputStream.write( 53 );
outputStream.write( 1 );
outputStream.write( 20 );
outputStream.write( 0 );
outputStream.write( 83 );
CommPort port = serialPort;
System.out.println( "Write done" );
( new Thread( new SerialReader( in,port ) ) ).start();
} else {
System.out.println( "Error: Only serial ports are handled by this example." );
}
}
}
public static class SerialReader implements Runnable {
InputStream in;
CommPort serialport;
public SerialReader( InputStream in, CommPort port) {
this.in = in;
serialport = port;
}
public void run() {
byte[] buffer = new byte[ 1024 ];
int len = -1;
try {
while( ( len = this.in.read( buffer ) ) > -1 ) {
String stringis= new String( buffer, 0, len,"ASCII" );
//System.out.println( stringis );
byte[] by_new = stringis.getBytes();
System.out.println("the lenghth of byte array is "+ by_new.length);
System.out.println( "The read is " );
for(int i=0; i<by_new.length; i++)
System.out.printf( "%5s",by_new[i]);
serialport.close();
}
} catch( IOException e ) {
e.printStackTrace();
}
try {
Thread.sleep(20000);
} catch (InterruptedException e) {System.out.println(e);}
}
}
public static void main( String[] args ) {
try {
( new TwoWaySerialComm() ).connect( "COM2" );
} catch( Exception e ) {
e.printStackTrace();
}
}
}
公共类双向串行通信{
void connect(字符串portName)引发异常{
CommPortIdentifier portIdentifier=CommPortIdentifier
.getPortIdentifier(端口名称);
if(portIdentifier.isCurrentlyOwned()){
System.out.println(“错误:端口当前正在使用”);
}否则{
int超时=10000;
CommPort CommPort=portIdentifier.open(this.getClass().getName(),超时);
if(串行端口的commPort实例){
SerialPort SerialPort=(SerialPort)commPort;
serialPort.setSerialPortParams(38400,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
InputStream in=serialPort.getInputStream();
OutputStream OutputStream=serialPort.getOutputStream();
outputStream.write(53);
outputStream.write(1);
outputStream.write(20);
outputStream.write(0);
outputStream.write(83);
CommPort端口=串行端口;
System.out.println(“写入完成”);
(新线程(新SerialReader(in,port))).start();
}否则{
System.out.println(“错误:本例只处理串行端口。”);
}
}
}
公共静态类SerialReader实现可运行{
输入流输入;
串口通信;
公共串行读取器(InputStream-in、CommPort端口){
this.in=in;
串行端口=端口;
}
公开募捐{
字节[]缓冲区=新字节[1024];
int len=-1;
试一试{
而((len=this.in.read(buffer))>-1){
String stringis=新字符串(缓冲区,0,len,“ASCII”);
//系统输出打印LN(stringis);
byte[]by_new=stringis.getBytes();
System.out.println(“字节数组的长度为”+乘以新的.length);
System.out.println(“读取为”);
对于(int i=0;我可以发布您的全部Java代码以便我们可以帮助您吗?在不了解您的仪器的情况下进行调试非常困难。但是您是否flush()
outputstream在写入并检查后是否存在?没有足够的代码来解释它为什么没有达到预期效果。您上面所写的内容确实应该向某个outputstream写入5个字节,但从代码段中无法判断您是否实际打开了串行端口以及是否正在侦听来自该端口的数据。我已经包含了ent现在是ire代码。。。