public class Bean{ /* 检测系统中可用的通讯端口类 */
static CommPortIdentifier portId; /* Enumeration 为枚举型类,在java.util中 */
static Enumeration portList;

InputStream inputStream;
OutputStream outputStream;
/* 声明RS-232串行端口的成员变量 */
SerialPort serialPort;
// 检测所有存在串口
public String[] getPort(){
/* 获取系统中所有的通讯端口 */
portList = CommPortIdentifier.getPortIdentifiers();
int i=0;
String[] PortName = new String[10];
/* 用循环结构找出串口 */
while (portList.hasMoreElements()) {
/* 强制转换为通讯端口类型 */
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
PortName[i]=portId.getName();
i++;
}
}
return PortName;
}

//打开串口,并监听串口,设置串口参数
public SerialPort OpenPort(String port){
try{
portId = CommPortIdentifier.getPortIdentifier(port);
}catch(Exception e){
System.out.println("选择串口错误");
e.printStackTrace();
}
try {
serialPort = (SerialPort) portId.open("Comm", 2000);
} catch (PortInUseException e) {
}

/* 设置串口输出流输出流 */
try {
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
} catch (IOException e) {
}
/* 设置串口通讯参数 */
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
return serialPort;
}
public int getConnection(){
byte[] response=new byte[24];
byte cmdBuf[] = new byte[24];
byte[] b=new byte[18];
try {
response[0] = (byte)0x01;
response[1] = (byte)0x11;
response[2] = (byte)0x22;
response[3] = (byte)0x33;
response[4] = (byte)0x44;
response[5] = (byte)0x55;
short e=EncodeCmd((byte)0x00,(short)10,(short)10,response,cmdBuf);
byte cmd[] = new byte[e];
OutputStream os = new BufferedOutputStream(serialPort.getOutputStream());
StringBuffer sb = new StringBuffer();
for(int i=0;i<e;i++){
System.out.print(cmdBuf[i] );
cmd[i]=cmdBuf[i];
byte2hex(cmdBuf[i],sb);
}
System.out.println(sb+"1");
//发送建立连接指令
os.write(cmd);
System.out.println(sb+"2");
inputStream = serialPort.getInputStream();
System.out.println(sb+"3");
int aa=0;
//读取返回信息
                           //-------------------------------------------------------------------此处读取信息始终过不去,请问改如何接收
aa = inputStream.read(b);
                        //-------------------------------------------------------------------
System.out.println(sb+"4");
System.out.println(aa);
return aa;
} catch (IOException e) {
e.printStackTrace();
close();
return -1;
}
}
//  将APDU命令编码成符合JN-POP手机与外部设备之间的通讯协议规范
//  连接指令编码
public short EncodeCmd(byte ins, short dataLgth, short responseLgth, byte[] data, byte[] cmdBuf){
byte temp;
short max;
short sum = 0; temp = (byte) (dataLgth>>8);
cmdBuf[0] = (byte) 0xA3;            //帧标识
cmdBuf[1] = (byte) ins;             //命令INS
cmdBuf[2] = (byte)(dataLgth & 0x00ff); cmdBuf[3] = (byte) (temp & 0x0f);
temp = (byte) (responseLgth >> 4);
temp &= 0xf0;
cmdBuf[3] |= temp;
cmdBuf[4] = (byte) (responseLgth & 0x00ff);
for(short i=0;i<dataLgth;i++){
cmdBuf[5+i] +=data[i];
}
max = (short) (5 + dataLgth); for (int i = 0; i < max; i++) // 计算数据校验和
{
sum += cmdBuf[i];
} cmdBuf[max] = (byte) (sum & 0x00ff); // 帧校验
return (short) (max + 1);
}
public void close(){
serialPort.close();
}
public void byte2hex(byte b,StringBuffer buf){   
  char[] hexChars = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};   
  int high = ((b&0xf0)>>4);   
  int low = (b&0x0f);   
  buf.append(hexChars[high]);   
  buf.append(hexChars[low]);   

请问
//-------------------------------------------------------------------此处读取信息始终过不去,请问改如何接收
aa = inputStream.read(b);
//-------------------------------------------------------------------

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【knight14】截止到2008-06-27 16:22:33的历史汇总数据(不包括此帖):
    发帖数:2                  发帖分:11                 
    结贴数:2                  结贴分:11                 
    未结数:0                  未结分:0                  
    结贴率:100.00%            结分率:100.00%            
    敬礼!
      

  2.   


    public void serialEvent(SerialPortEvent e){
         StringBuffer stringBuffer= new StringBuffer();
         int readData= 0;
         switch(e.getEventType()){
         case SerialPortEvent.DATA_AVAILABLE:
         while(readData!= -1){
         try{
         readData= inStream.read();
         if(readData== -1){
         break;
         }
        
         String value;  //转换数据显示,用十六进制输出
         if(readData>= 16){
         value= Integer.toHexString(readData).toUpperCase();
         }else if(readData>= 0){
         value= "0".concat(Integer.toHexString(readData).toUpperCase());
         }else{
         value= Integer.toHexString(readData).substring(6).toUpperCase(); //当数据小于0时转换成十六进制显示时取后两位数据位,忽略符号位
         }
        
         stringBuffer.append(value+ " "); //将数据转换为十六进制显示
         }catch(IOException ex){
         ex.printStackTrace();
         }
         }
         }
        }
      

  3.   

    非常感谢的回复,
    因为不太明白如何读取串口返回的东西,所以read这里一直比较模糊,你说的serialEvent(SerialPortEvent e)这个监听是自动监听还是要调用啊?有没有一个完整的例子?谢谢
      

  4.   

    1 此处读取信息始终过不去,请问改如何接收 
    你确认对面有信息吗?去看看数据源吧!2 你发送指令
    //发送建立连接指令
    os.write(cmd); 后面你 flush()了吗?有一些输出是缓冲的。
      

  5.   

    public void serialEvent(SerialPortEvent e){
            StringBuffer stringBuffer= new StringBuffer();
            int readData= 0;
            switch(e.getEventType()){
            case SerialPortEvent.DATA_AVAILABLE:
                while(readData!= -1){
                    try{
                        readData= inStream.read();
                        if(readData== -1){
                            break;
                        }
                        
                        String value;  //转换数据显示,用十六进制输出
                        if(readData>= 16){
                            value= Integer.toHexString(readData).toUpperCase();
                        }else if(readData>= 0){
                            value= "0".concat(Integer.toHexString(readData).toUpperCase());
                        }else{
                            value= Integer.toHexString(readData).substring(6).toUpperCase(); //当数据小于0时转换成十六进制显示时取后两位数据位,忽略符号位
                        }
                        
                        stringBuffer.append(value+ " "); //将数据转换为十六进制显示
                    }catch(IOException ex){
                        ex.printStackTrace();
                    }
                }
            }
        }
      

  6.   

    这有个例子 希望对你有帮助
    http://blog.csdn.net/joney1/articles/24856.aspx
      

  7.   


    public class PortConn implements SerialPortEventListener
    定义类的时候实现这个接口
    serialEvent(SerialPortEvent e)实现接收的实现