java - i am trying to sent sms through serial port event listener but i am grtting this error NoSuchPortException -
java - i am trying to sent sms through serial port event listener but i am grtting this error NoSuchPortException -
week before code working after formating scheme giving nosuchportexception while trying send sms connect nokia c3 cell phone via usb
error log as
enter code here`info: server startup in 17344 ms +919032055002 null problem here in getting port number javax.comm.nosuchportexception port id : null @ javax.comm.commportidentifier.getportidentifier(commportidentifier.java:105) @ com.gces.sms.serialconnection.openconnection(serialconnection.java:73) @ com.gces.sms.sender.send(sender.java:68) @ com.gces.sms.smsclient.run(smsclient.java:38) @ java.lang.thread.run(unknown source) in sms client & msg : null java.lang.nullpointerexception @ com.gces.sms.serialconnection.openconnection(serialconnection.java:90) @ com.gces.sms.sender.send(sender.java:68) @ com.gces.sms.smsclient.run(smsclient.java:38) @ java.lang.thread.run(unknown source)
and code here
package com.gces.sms; import javax.comm.*; import java.io.*; import java.awt.textarea; import java.awt.event.*; import java.util.toomanylistenersexception; public class serialconnection implements serialporteventlistener, commportownershiplistener { private serialparameters parameters; private outputstream os; private inputstream is; private keyhandler keyhandler; private commportidentifier portid; private serialport sport; private boolean open; private string receptionstring=""; public string getincommingstring(){ byte[] bval= receptionstring.getbytes(); receptionstring=""; homecoming new string (bval); } public serialconnection(serialparameters parameters) { this.parameters = parameters; open = false; } public void openconnection() throws serialconnectionexception { seek { portid = commportidentifier.getportidentifier(parameters.getportname()); } grab (nosuchportexception e) { system.out.println("problem here in getting port number"); e.printstacktrace(); }catch(exception e) { e.printstacktrace(); } system.out.println("port id : "+portid); seek { sport = (serialport)portid.open("smsconnector", 30000); } grab (portinuseexception e) { throw new serialconnectionexception(e.getmessage()); } sport.sendbreak(1000); seek { setconnectionparameters(); } grab (serialconnectionexception e) { sport.close(); throw e; } seek { os = sport.getoutputstream(); = sport.getinputstream(); } grab (ioexception e) { sport.close(); throw new serialconnectionexception("error opening i/o streams"); } seek { sport.addeventlistener(this); } grab (toomanylistenersexception e) { sport.close(); throw new serialconnectionexception("too many listeners added"); } sport.notifyondataavailable(true); seek { sport.enablereceivetimeout(30); } grab (unsupportedcommoperationexception e) { } portid.addportownershiplistener(this); open = true; } public void setconnectionparameters() throws serialconnectionexception { int oldbaudrate = sport.getbaudrate(); int olddatabits = sport.getdatabits(); int oldstopbits = sport.getstopbits(); int oldparity = sport.getparity(); int oldflowcontrol = sport.getflowcontrolmode(); seek { sport.setserialportparams(parameters.getbaudrate(), parameters.getdatabits(), parameters.getstopbits(), parameters.getparity()); } grab (unsupportedcommoperationexception e) { parameters.setbaudrate(oldbaudrate); parameters.setdatabits(olddatabits); parameters.setstopbits(oldstopbits); parameters.setparity(oldparity); throw new serialconnectionexception("unsupported parameter"); } seek { sport.setflowcontrolmode(parameters.getflowcontrolin() | parameters.getflowcontrolout()); } grab (unsupportedcommoperationexception e) { throw new serialconnectionexception("unsupported flow control"); } } public void closeconnection() { if (!open) { return; } if (sport != null) { seek { os.close(); is.close(); } grab (ioexception e) { system.err.println(e); } sport.close(); portid.removeportownershiplistener(this); } open = false; } public void sendbreak() { sport.sendbreak(1000); } public boolean isopen() { homecoming open; } public void serialevent(serialportevent e) { stringbuffer inputbuffer = new stringbuffer(); int newdata = 0; switch (e.geteventtype()) { case serialportevent.data_available: while (newdata != -1) { seek { newdata = is.read(); if (newdata == -1) { break; } if ('\r' == (char)newdata) { inputbuffer.append('\n'); } else { inputbuffer.append((char)newdata); } } grab (ioexception ex) { system.err.println(ex); return; } } receptionstring=receptionstring+ (new string(inputbuffer)); break; case serialportevent.bi: receptionstring=receptionstring+("\n--- break received ---\n"); } } public void ownershipchange(int type) { /* if (type == commportownershiplistener.port_ownership_requested) { portrequesteddialog prd = new portrequesteddialog(parent); } */ } class keyhandler extends keyadapter { outputstream os; /** creates keyhandler. @param os outputstream port. */ public keyhandler(outputstream os) { super(); this.os = os; } /** handles keyevent. gets <code>char</char> generated <code>keyevent</code>, converts <code>int</code>, writes <code> outputstream</code> port. */ public void keytyped(keyevent evt) { char newcharacter = evt.getkeychar(); if ((int)newcharacter==10) newcharacter = '\r'; system.out.println ((int)newcharacter); seek { os.write((int)newcharacter); } grab (ioexception e) { system.err.println("outputstream write error: " + e); } } } public void send(string message) { byte[] thebytes= (message+"\n").getbytes(); (int i=0; i<thebytes.length;i++){ char newcharacter = (char)thebytes[i]; if ((int)newcharacter==10) newcharacter = '\r'; seek { os.write((int)newcharacter); } grab (ioexception e) { system.err.println("outputstream write error: " + e); } } //system.out.println (">'" +message +"' sent"); } }
and sec file code
package com.gces.sms; import java.io.bufferedreader; import java.io.file; import java.io.fileinputstream; import java.io.inputstreamreader; import java.util.date; import com.gces.sms.serialconnection; import com.gces.sms.serialconnectionexception; import com.gces.sms.serialparameters; public class sender implements runnable { private static final long standard=500; private static final long long=2000; private static final long verylong=20000; serialconnection myserial =null; static final private char cntrlz=(char)26; string in, out; thread athread=null; private long delay=standard; string recipient=null; string message=null; //private string csca="+919084051550"; // message center uninor //private string csca="+919837099995"; // message center thought private string csca="+919032055002"; // message center docomo private serialparameters defaultparameters= new serialparameters ("com4",9600,0,0,8,1,0); public int step; public int status=-1; public long messageno=-1; public sender(string recipient, string message){ this.recipient=recipient; this.message=message; } public int send () throws exception { file file = new file("c:\\db_backup\\sms.txt"); bufferedreader br = new bufferedreader(new inputstreamreader(new fileinputstream(file))); string line = null; string port = null; int flag=0; while( (line = br.readline())!= null ){ if(flag==0) port=line; else if(flag==1) csca=line; else br.close(); flag++; } //br.close(); defaultparameters= new serialparameters (port,9600,0,0,8,1,0); serialparameters params = defaultparameters; system.out.println(csca+" "+port); myserial =new serialconnection (params); myserial.openconnection(); athread=new thread(this); athread.start() ; //log("start"); homecoming 0; } public void run(){ boolean timeout=false; long starttime=(new date()).gettime(); while ((step <7) && (!timeout)){ timeout=((new date()).gettime() - starttime)>delay; //if atz not work, type send cntrlz , retry, in case message stuck if (timeout && (step==1)) { step=-1; myserial.send( ""+cntrlz); } //read incoming string string result= myserial.getincommingstring() ; //system.out.println("<- in rrrrrrrrrr "+result+"\n--------"); int expectedresult=-1; try{ //log ("step:"+step); switch (step){ case 0: myserial.send("atz"); delay=long; starttime=(new date()).gettime(); break; case 1: delay=standard; myserial.send("ath0"); starttime=(new date()).gettime(); break; case 2: expectedresult=result.indexof("ok"); //log ("received ok ="+expectedresult); if (expectedresult>-1){ myserial.send("at+cmgf=1"); starttime=(new date()).gettime(); }else{ step=step-1; } break; case 3: expectedresult=result.indexof("ok"); // log ("received ok ="+expectedresult); if (expectedresult>-1){ myserial.send("at+csca=\""+csca+"\""); starttime=(new date()).gettime(); }else{ step=step-1; } break; case 4: expectedresult=result.indexof("ok"); // log ("received ok ="+expectedresult); if (expectedresult>-1){ myserial.send("at+cmgs=\""+recipient+"\""); starttime=(new date()).gettime(); }else{ step=step-1; } break; case 5: expectedresult=result.indexof(">"); //log ("received ok ="+expectedresult); if (expectedresult>-1){ myserial.send(message+cntrlz); starttime=(new date()).gettime(); }else{ step=step-1; } delay=verylong;//waiting message ack break; case 6: expectedresult=result.indexof("ok"); //read message number if (expectedresult>-1){ int n=result.indexof("cmgs:"); result=result.substring(n+5); //system.out.println(result); n=result.indexof("\n"); status=0; // result="1 11"; try{ messageno=long.parselong(result.substring(0,n).trim() ); } catch(numberformatexception e){ //result="1 "; messageno=1; } log ("sent message no:"+messageno); }else{ step=step-1; } break; } step=step+1; athread.sleep(100); }catch (exception e){ e.printstacktrace(); } } myserial.closeconnection() ; //if timed out set status if (timeout ) { status=-2; log("*** time out @ step "+step+"***"); } } private void log(string s){ system.out.println (new java.util.date()+":"+this.getclass().getname()+":"+s); } }
this code working fine after formating scheme gives these error ...
java sms
Comments
Post a Comment