问题描述
我在写一个串口通信的java程序。我在窗口包中写窗口类(main),在串口包中写串口通信类。现在串口通信类里的接受监听器有数据反映了。我怎么将数据写到屏幕上呢?我不想在串口类中写窗口程序,怎么返回监听器的数据呢?怎么在串口类中接受呢???请求大神指点迷经?新手
解决方案
解决方案二:
java串口通信类程序packagefirst;/**@(#)SimpleRead.java1.1298/06/25SMI**Copyright(c)1998SunMicrosystems,Inc.AllRightsReserved.**Sungrantsyou("Licensee")anon-exclusive,royaltyfree,license*touse,modifyandredistributethissoftwareinsourceandbinary*codeform,providedthati)thiscopyrightnoticeandlicenseappear*onallcopiesofthesoftware;andii)Licenseedoesnotutilizethe*softwareinamannerwhichisdisparagingtoSun.**Thissoftwareisprovided"ASIS,"withoutawarrantyofanykind.*ALLEXPRESSORIMPLIEDCONDITIONS,REPRESENTATIONSANDWARRANTIES,*INCLUDINGANYIMPLIEDWARRANTYOFMERCHANTABILITY,FITNESSFORA*PARTICULARPURPOSEORNON-INFRINGEMENT,AREHEREBYEXCLUDED.SUNAND*ITSLICENSORSSHALLNOTBELIABLEFORANYDAMAGESSUFFEREDBY*LICENSEEASARESULTOFUSING,MODIFYINGORDISTRIBUTINGTHE*SOFTWAREORITSDERIVATIVES.INNOEVENTWILLSUNORITSLICENSORS*BELIABLEFORANYLOSTREVENUE,PROFITORDATA,ORFORDIRECT,*INDIRECT,SPECIAL,CONSEQUENTIAL,INCIDENTALORPUNITIVEDAMAGES,*HOWEVERCAUSEDANDREGARDLESSOFTHETHEORYOFLIABILITY,ARISING*OUTOFTHEUSEOFORINABILITYTOUSESOFTWARE,EVENIFSUNHASBEEN*ADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGES.**Thissoftwareisnotdesignedorintendedforuseinon-linecontrol*ofaircraft,airtraffic,aircraftnavigationoraircraft*communications;orinthedesign,construction,operationor*maintenanceofanynuclearfacility.Licenseerepresentsand*warrantsthatitwillnotuseorredistributetheSoftwareforsuch*purposes.*/importjava.io.*;importjava.util.*;importjavax.comm.*;publicclassRS232ReadandWriteimplementsRunnable,SerialPortEventListener{staticCommPortIdentifierportId;staticEnumerationportList;staticInputStreaminputStream;staticOutputStreamoutputStream;staticSerialPortserialPort;staticThreadreadThread;staticintRX_numBytes;staticintmessageString=7;publicStringALL_COMString;publicStringWORKing_COMString;byte[]readBuffer;publicRS232ReadandWrite(){ALL_COMString=newString();WORKing_COMString=newString();portList=CommPortIdentifier.getPortIdentifiers();while(portList.hasMoreElements()){portId=(CommPortIdentifier)portList.nextElement();if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL){ALL_COMString+=""+portId.getName().toString();if(portId.getName().equals("COM2")){System.out.println("所有的COM:"+ALL_COMString);WORKing_COMString=portId.getName().toString();try{serialPort=(SerialPort)portId.open("SimpleReadApp",2000);}catch(PortInUseExceptione){}try{inputStream=serialPort.getInputStream();}catch(IOExceptione){}try{serialPort.addEventListener(this);}catch(TooManyListenersExceptione){}try{outputStream=serialPort.getOutputStream();}catch(IOExceptione){}serialPort.notifyOnDataAvailable(true);try{serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);}catch(UnsupportedCommOperationExceptione){}try{outputStream.write(messageString);}catch(IOExceptione){}try{outputStream.write(messageString);}catch(IOExceptione){}readThread=newThread(this);readThread.start();}}}}publicvoidrun(){try{Thread.sleep(20000);}catch(InterruptedExceptione){}}publicvoidserialEvent(SerialPortEventevent){switch(event.getEventType()){caseSerialPortEvent.BI:System.out.print("通讯中断");caseSerialPortEvent.OE:System.out.print("溢位错误");caseSerialPortEvent.FE:System.out.print("帧错误");caseSerialPortEvent.PE:System.out.print("奇偶校验错误");caseSerialPortEvent.CD:System.out.print("载波错误");caseSerialPortEvent.CTS:System.out.print("清除发送");caseSerialPortEvent.DSR:System.out.print("数据设备准备好");caseSerialPortEvent.RI:System.out.print("振铃指示");caseSerialPortEvent.OUTPUT_BUFFER_EMPTY:System.out.print("输出缓冲区已清空");break;caseSerialPortEvent.DATA_AVAILABLE:System.out.print("有数据到达");readBuffer=newbyte[11];try{while(inputStream.available()>0){RX_numBytes=inputStream.read(readBuffer);System.out.println("接收计数器:"+RX_numBytes);}}catch(IOExceptione){}Stringstr=newString();for(inti=0;i<RX_numBytes;i++){str+=readBuffer[i];}//jlabel_txd.setText("接收到的数据为:"+str);System.out.println("接收到的数据为:"+str.toString());intiii=(readBuffer[0]&256);System.out.println("接收到的数据为:"+iii);break;}}}