



import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Enumeration;

import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;





public class chuankou {


	public  chuankou(String CK,int BL ) throws IOException{
		Enumeration<?> en = CommPortIdentifier.getPortIdentifiers();
		CommPortIdentifier portId = null;
		CommPortIdentifier portId2;
		String portname;
		String rate;
		String data;
		String stop;
		String parity;
		SerialPort  serialPort = null;
		// TODO Auto-generated method stub
		
	/*
	 * 串口程序
	 * 
	 * */
		while (en.hasMoreElements()) {
//			System.out.println("还没找到");
			portId = (CommPortIdentifier) en.nextElement();
			// 如果端口类型是串口，则打印出其端口信息
			if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
				System.out.println(portId.getName());
				if(portId.getName().equalsIgnoreCase("COM1"))//判断端口是否与需要的端口一样{
					
					try {
						serialPort = (SerialPort) portId.open("JavaRs232", 5);
						 serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);//串口频率，位数等串口需要的参数
						 
							/*
							 * 循环读取端口信息
							 * */	
						 while(true){
									InputStream    inputStream = serialPort.getInputStream();//输入流
									BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));//字节读写器
									String s=Utils.getTextFromStream(inputStream);//自定义字符串输出类
									
									System.out.println(s);
									br.close();
									inputStream.close();
						 }
						 
						 
						
					}
					catch (PortInUseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (UnsupportedCommOperationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			}
		

//		 try {
//				portId = (CommPortIdentifier) en.nextElement();
				// 如果端口类型是串口，则打印出其端口信息
//				if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
//					System.out.println(portId.getName());
//				
//				serialPort = (SerialPort) portId.open("JavaRs232", 5);
//				 serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
				
		
//				 read();
//				　serialPort.setFlowControlMode(FlowControlIn | FlowControlOut);

//				 InputStream is = new BufferedInputStream(serialPort.getInputStream());
//				int i=is.read();
//				
//				InputStream    inputStream = serialPort.getInputStream();
//				    String s = null;
//				    int i=0;
//				    i=inputStream.read();
				  
//					byte[] readBuffer = new byte[50];
//					while (inputStream.available() > 0) {
////						s=String.valueOf(inputStream.read(readBuffer));
//						i=inputStream.read();
//					}
			
			
			
//				while(true){
//					BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
//					String s=br.readLine();
//					
//					System.out.println(s);
//					br.close();
//					inputStream.close();
//				}

					
//					inputStream.close();
//OutputStream os=new BufferedOutputStream(serialPort.getOutputStream());
//
//				os.write("0".getBytes());
//				os.flush();
//				os.close();
//				serialPort.close();
//				System.out.println(portId.getName()+"端口已被占用,请检查!");
//				while(true){
//					System.out.println(portId.getName());
//				}
			
//				}
//			} catch (PortInUseException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (UnsupportedCommOperationException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
	
	
	
//	public static void write(){
//		try {
//			OutputStream os = new BufferedOutputStream(serialPort.getOutputStream());
//			byte[] text=new byte[200];
//			String b="0\r\n";
//    text=b.getBytes();
////    text[0]=Integer.valueOf("0x30",16).byteValue();            				
//						os.write(text);
//			System.out.println(text);
//			os.close();
//			os.flush();
//			serialPort.close();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
//	}
//	public  void close(){
//		try {
//			OutputStream os = new BufferedOutputStream(serialPort.getOutputStream());
//			byte[] text=new byte[200];
//			String b="1\r\n";
//text=b.getBytes();
////text[0]=Integer.valueOf("0x30",16).byteValue();            				
//						os.write(text);
//			System.out.println(text);
//			os.close();
//			os.flush();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
//	public  void read(){
//		try {
//			while(true){
//			InputStream    inputStream = serialPort.getInputStream();
//			BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
//			String string=Utils.getTextFromStream(inputStream);
////			byte[]   c =   new   byte[8]; 
////			c[0]   =   (byte)   0xf5; 
////			c[1]   =   (byte)   0x0c; 
////			c[2]   =   (byte)   0x00; 
////			c[3]   =   (byte)   0x01; 
////			c[4]   =   (byte)   0x01; 
////			c[5]   =   (byte)   0x00; 
////			c[6]   =   (byte)   0x0c; 
////			byte[]   d =   new   byte[8]; 
////			d[0]   =   (byte)   0xf5; 
////			d[1]   =   (byte)   0x0c; 
////			d[2]   =   (byte)   0x00; 
////			d[3]   =   (byte)   0x02; 
////			d[4]   =   (byte)   0x01; 
////			d[5]   =   (byte)   0x00; 
////			d[6]   =   (byte)   0x0f;
//////			c[7]   =   (byte)   0xf; 
////			String cc=Utils.bytesToHexString(c);
////			String dd=Utils.bytesToHexString(d);
////			if(string.equals(cc)){
////				
////				System.out.print("恭喜你这是你的拇指指纹");
////				break;
////				
////			}
////			else if(string.equals(dd)){
////				System.out.print("恭喜你这是你的食指指纹");
////				break;
////				
////			}
//			System.out.println(string);
////			String s=br.readLine();
////			Utils.bytesToHexString(br);
////			if(s!=null){
////				System.out.println("ok");
////				System.out.println(s);
////				break;
////			}
//			
//			
////			System.out.println(s);
//		} }catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}

//	private static byte[] HexString2Bytes(String src) {
//		// TODO Auto-generated method stub
//		if (null == src || 0 == src.length()) 
//		{ return null; }
//		byte[] ret = new byte[src.length() / 2];
//		byte[] tmp = src.getBytes();
//		for (int i = 0; i < (tmp.length / 2); i++)
//		{ ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
//		}
//		return ret;
//	}
	public static  String hexString2binaryString(String hexString) {
		  if (hexString == null || hexString.length() % 2 != 0)
			 return null;
			 String bString = "", tmp;
			 for (int i = 0; i < hexString.length(); i++) {
			 tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
				  bString += tmp.substring(tmp.length() - 4);
			  }
//			 System.out.println(bString);
			 return bString;
		  } 
	
		}
		
		
	
			

	


		
		
	
	
//	}
