摘要: 在系统集成开发过程中,存在着各式的传输途径,其中串口经常因其安全性高获得了数据安全传输的重用,通过串口传输可以从硬件上保证数据传输的单向性,这是其它介质所不具备的物理条件。下面我就串口java开发的过程分享一下,并分享一个SerialPortHandler串口开发帮助类,以提高串口开发效率。并附带了一些近期的总结,出差的体会。
关键词: java, 串口, 经验, 年终总结需求: 开发串口快速上手publicclassSerialPortHandlerimplementsRunnable,SerialPortEventListener{PRivateStringappName="串口通讯";privatefinalstaticinttimeout=2000;//open端口时的等待时间privatefinalstaticintdataBits=8;//数据位privatefinalstaticintstopBits=1;//停止位privatefinalstaticintparity=0;//校验位privateStringportName;privateintbaudRate;//波特率privateintthreadTime=0;privateCommPortIdentifiercommPort;privateSerialPortserialPort;privateInputStreaminputStream;privateOutputStreamoutputStream;privatestaticConcurrentHashMap<String,SerialPortHandler>instances=newConcurrentHashMap<String,SerialPortHandler>();privateSerialPortHandler(){}publicstaticsynchronizedSerialPortHandlergetInstance(StringportName,intbaudRate)throwsNoSuchPortException{SerialPortHandlerinstance;if(!instances.containsKey(portName)){instance=newSerialPortHandler();instance.portName=portName;instance.baudRate=baudRate;instances.put(portName,instance);instance.selectPort();}else{instance=instances.get(portName);}returninstance;}/***@方法名称:listPort*@功能描述:列出所有可用的串口*@返回值类型:void*/publicstaticvoidlistPort(){CommPortIdentifiercpid;Enumerationen=CommPortIdentifier.getPortIdentifiers();System.out.println("nowtolistallPortofthisPC:"+en);while(en.hasMoreElements()){cpid=(CommPortIdentifier)en.nextElement();if(cpid.getPortType()==CommPortIdentifier.PORT_SERIAL){System.out.println(cpid.getName()+","+cpid.getCurrentOwner());}}}/***@方法名称:selectPort*@功能描述:选择一个端口,比如:COM1*@返回值类型:void*@paramportName*@throwsNoSuchPortException*/@SuppressWarnings("rawtypes")privatevoidselectPort()throwsNoSuchPortException{this.commPort=CommPortIdentifier.getPortIdentifier(portName);openPort();}/***@方法名称:openPort*@功能描述:打开SerialPort*@返回值类型:void*/privatevoidopenPort(){if(commPort==null)log(String.format("无法找到名字为'%1$s'的串口!",commPort.getName()));else{log("端口选择成功,当前端口:"+commPort.getName()+",现在实例化SerialPort:");try{serialPort=(SerialPort)commPort.open(appName,timeout);serialPort.setSerialPortParams(baudRate,dataBits,stopBits,parity);log("实例SerialPort成功!");}catch(PortInUseExceptione){thrownewRuntimeException(String.format("端口'%1$s'正在使用中!",commPort.getName()));}catch(UnsupportedCommOperationExceptione){thrownewRuntimeException(String.format("端口'%1$s'操作命令不支持!",commPort.getName()));}}}/***@方法名称:checkPort*@功能描述:检查端口是否正确连接*@返回值类型:void*/privatevoidcheckPort(){if(commPort==null)thrownewRuntimeException("没有选择端口,请使用"+"selectPort(StringportName)方法选择端口");if(serialPort==null){thrownewRuntimeException("SerialPort对象无效!");}}/***@方法名称:write*@功能描述:向端口发送数据,请在调用此方法前先选择端口,并确定SerialPort正常打开!*@返回值类型:void*@parammessage*/publicvoidwrite(Stringmessage){checkPort();try{outputStream=newBufferedOutputStream(serialPort.getOutputStream());}catch(IOExceptione){thrownewRuntimeException("获取端口的OutputStream出错:"+e.getMessage());}try{outputStream.write(message.getBytes());log("信息发送成功!");}catch(IOExceptione){thrownewRuntimeException("向端口发送信息时出错:"+e.getMessage());}finally{try{outputStream.close();}catch(Exceptione){}}}} |
publicstaticvoidsendBySerialPorts(Stringcontant){contant=contant.replaceAll("//$","");// 这里使用$符号作为终止符contant=contant.concat("$");SerialPortHandlersph;try{sph=SerialPortHandler.getInstance(DriveOutConductConfig.getPortName(),DriveOutConductConfig.getBitRate());sph.write(contant);}catch(NoSuchPortExceptione){e.printStackTrace();}} |
新闻热点
疑难解答