hi, i'm khifa. i'm newbie in java. i have a problem with my java application. the button not working well. i need to press the button until 6 times then the data will be fully write at the comport. can some one help how can i just click one then the data will be write and the output will just appear? thank you. your help are very needed and very appreciated.![]()
my code is
if anyone need to see th code please email me at khifa_monqius@yahoo.comCode://this is the button action listener. i put the button in open connection. public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String t=subnet.getText(); String n=node.getText(); int ct = Integer.parseInt(t); int cn = Integer.parseInt(n); vt = Integer.toHexString(ct); vn = Integer.toHexString(cn); String s; if(cn<16){ s = "323F010"+vn+"2304"+vt+"0000000000000400003088010300"; }else{ s = "323F01"+vn+"2304"+vt+"0000000000000400003088010300"; } System.out.println(s); try{ System.out.println("button"); os.write(s.getBytes()); os.flush(); System.out.println("button off"); } catch(IOException ex){ System.out.println("Cannot send"); } messageAreaIn.append("\nRead total Energy\n"); } public void serialEvent(SerialPortEvent e) { // Create a StringBuffer and int to receive input data. StringBuffer inputBuffer = new StringBuffer(); int newData = 0; // Determine type of event. switch (e.getEventType()) { // Read data until -1 is returned. If \r is received substitute // \n for correct newline handling. case SerialPortEvent.DATA_AVAILABLE: System.out.println("data available"); while (newData != -1) { try { newData = is.read(); if (newData == -1) { break; } if ('\r' == (char)newData) { inputBuffer.append('\n'); } else { inputBuffer.append((char)newData); } } catch (IOException ex) { System.err.println(ex); return; } } // Append received data to messageAreaIn. System.out.println(new String(inputBuffer)); //String str = new StringBuffer(new StringBuffer(new String(inputBuffer)).reverse().toString().substring(0, 8)).reverse().toString(); String str =new StringBuffer(inputBuffer.reverse().toString().substring(5, 7)).reverse().toString(); System.out.println(str); String vtr =new StringBuffer(inputBuffer.toString().substring(7, 9)).reverse().toString(); System.out.println(vtr); String ktr =new StringBuffer(inputBuffer.toString().substring(1, 2)).reverse().toString(); System.out.println(ktr); int k= Integer.parseInt(ktr,16); String low = str+vtr; System.out.println(low); int j1=0; try{ j1 = Integer.parseInt(low,16); } catch(NumberFormatException nfe){ System.out.println("NumberFormatException: " + nfe.getMessage()); } j=j1; System.out.println("Before bahagi\n"+j); float scale = parameters.getScale(); System.out.println("Scale = " +scale); last = j/scale; System.out.println("After bahagi\n"+last); DecimalFormat percy = new DecimalFormat("#.00"); fin = percy.format(last); if((last>0)&&(j!=2)&&(k!=15)) { messageAreaIn.append("\n"+fin+"kW.h"+"\n"); } else { messageAreaIn.append("\nerror"+"\n"); } break; // If break event append BREAK RECEIVED message. case SerialPortEvent.BI: messageAreaIn.append("\n--- BREAK RECEIVED ---\n"); } }![]()


Reply With Quote


Bookmarks