java串口通信,接受到数据和发送的数据一直一样,请高手看看!
最近在做关于Intermec读写器的开发,它是通过串口连接的,但我的代码中一直有这样一个问题:接受到的数据和发送的数据一样,没有变,然后我的代码是这样的:
package com.xhq.rfid;
import java.io.*;
import java.net.Socket;
import java.util.*;
import javax.comm.*;
public class IntermecReader {
private String comname;//串口名字
private SerialPort serialPort;
private CommPortIdentifier myPort;
private String comnum;//串口号
private int time;//串口等待时间
private Thread readThread;
private DataOutputStream output; // output stream to Reader
private DataInputStream input; // input stream from Reader
public IntermecReader(String name,int p,String c)
{
this.comname=name;
this.time =p;
this.comnum=c;
}
public void open() throws Exception
{
openConnection();
getStreams();
}
private void openConnection() throws Exception
{
try
{
System.out.println( "Attempting connection" );
myPort =CommPortIdentifier.getPortIdentifier(this.comnum);
System.out.println ("连接" + myPort.getName() + "成功");
}
catch(Exception e)
{
System.out.println( "Attempting connection error!\n" );
}
try
{
serialPort = (SerialPort) myPort.open(this.comname,this.time);
System.out.println ("连接" + myPort.getCurrentOwner() + "成功");
}
catch (PortInUseException e)
{
e.printStackTrace();
}
}
private void getStreams() throws Exception//打开数据流
{
try
{
System.out.println( "Try to get I/O streams" );
// set up output stream for objects
output = new DataOutputStream( serialPort.getOutputStream());
output.flush(); // flush output buffer to send header information
Thread.sleep(200);
// set up input stream for objects
input = new DataInputStream( serialPort.getInputStream());
if(input.available() > 0)
{
byte[] line = new byte[input.available()];
System.out.println( input.read(line));
}
System.out.println( "Got I/O streams" );
}
catch(Exception e)
{
System.out.println( "Got I/O streams Error!" );
}
try {
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE );
}
catch (UnsupportedCommOperationException e)
{
System.out.println("configu error");
}
readThread = new Thread();
readThread.start();
}
public void closeConnection() throws Exception
{
System.out.println( "\nClosing connection..." );
serialPort.close(); // close SerialPort
System.out.println( "The connection has been closed." );
} // end method closeConnection
public void attrib() throws InterruptedException//向读写器发送命令,并获取它的回复
{
String instring="ATTRIB";
// byte[] test={0x06, 0x00, 0x00, 0x00, 0x00, 0x00};
byte[] line={};
String stmp="";
String db="";
try
{
System.out.println("Try to know UNSELTRIES!");
output=new DataOutputStream( serialPort.getOutputStream());
// output.write(instring.getBytes());
output.writeBytes(instring);
// output.write(test);
// System.out.println(test);
output.flush();
Thread.sleep(200);
// input = new DataInputStream( serialPort.getInputStream());
if(input.available()>0)
{
line = new byte[input.available()];
input.read(line);
for(int i=0;i<line.length;i++)
{
stmp=Integer.toHexString(line[i]&0xff);
db+=stmp;
}
System.out.println(db);
System.out.println(new String(line));
}
else
System.out.println("Get Nothing!");
}
catch(IOException e)
{
System.out.println("Command Error!");
}
}
}
然后写了一个main函数来测试它:
package com.xhq.rfid;
public class testintermec {
public static void main(String[] args) throws Exception{
IntermecReader reader=new IntermecReader("SimpleRead",2000,"COM1");
reader.open();
reader.attrib();
// reader.configurachecksum();
Thread.sleep(1000);
reader.closeConnection();
}
}