eclipse - Connect to serial port in Java using RXTX -
i'm using eclipse communicate arduino, through arduino platform. can open port using eclipse or arduino, can't connect opened port using eclipse. send integers using eclipse through arduino platform, , received arduino. how can connect eclipse arduino's serial port?
this actual code (i found in utilisation des ports com en java avec rxtx [résolu] (is in french)):
import gnu.io.unsupportedcommoperationexception; import java.io.ioexception; import java.util.scanner; public class testserialport { public static void main(string[] args) { scanner sc = new scanner(system.in); system.out.println("le port est-il ouvert? "); string str = sc.nextline(); system.out.println("vous avez saisi : " + str); string s1 = "non"; if ( s1.equals(str)) { final rs232 serial = new rs232(); system.out.println("- ouverture du port -"); serial.setport("com3"); serial.setvitesse(4800); serial.setnbbits(8); serial.setparite("aucune"); serial.setstop("1"); serial.setcontrole("matériel"); serial.ouvrirport(); system.out.println("- envoie de l'information -"); serial.ecrireint(9); system.out.println(" le recu " + serial.getcrecu()); serial.effacercrecu(); serial.fermerport(); } else { } } } import gnu.io.*; import java.io.*; import java.util.logging.level; import java.util.logging.logger; public class rs232 { private outputstream outstream; //flux d'écriture du port private inputstream in; //flux de lecture du port private commportidentifier portid; //identifiant du port private rxtxport sport; //le port série private string s; //string de retour d'info private int vitesse = 4800; private int nbbits = serialport.databits_8; private string nomport = "com3"; private int parite = serialport.parity_none; private int stop = serialport.stopbits_1; private int controle = serialport.flowcontrol_none; private rxtxcommdriver w32driver = new rxtxcommdriver(); private ecouteport ecouteport1; private bufferedreader bufread; public rs232(){ } //recupération du nom du port public string setport(string port){ nomport = port; s = ""; try { portid = commportidentifier.getportidentifier(nomport); s = "port ok"; } catch (nosuchportexception e) { s = "port innexistant"; } return s; } //renvoi du nom du port public string getport(){ return portid.tostring(); } //recuperation de la vitesse de transmission public void setvitesse(int v){ vitesse = v; } //retour de la vitesse de transmission public int getvitesse(){ return vitesse; } //recupertion du nombre de bits de donées public string setnbbits(int b){ s = ""; if (b == 5){ nbbits = rxtxport.databits_5; s = "5 bits de donnée"; } else if (b == 6){ nbbits = rxtxport.databits_6; s = "6 bits de donnée"; } else if (b == 7){ nbbits = rxtxport.databits_7; s = "7 bist de donnée"; } else if (b == 8){ nbbits = rxtxport.databits_8; s = "8 bits de donnée"; } else { s = "nombre de bits de donnée incompatible"; } // system.out.println(s); return s; } //retour du nombre de bits de données public int getnbbits(){ int b = 0; if ( nbbits == rxtxport.databits_5){ b = 5; } else if (nbbits == rxtxport.databits_6){ b = 6; } else if ( nbbits == rxtxport.databits_7){ b = 7; } else if ( nbbits == rxtxport.databits_8){ b = 8 ; } return b; } //recuperation de la parite public string setparite(string parite){ s = ""; if (parite.equals("aucune")){ parite = rxtxport.parity_none; s = "pas de parité"; } else if (parite.equals("paire")){ parite = rxtxport.parity_even; s = "parité paire"; } else if (parite.equals("impaire")){ parite = rxtxport.parity_odd; s = "parité impaire"; } else if (parite.equals("marque")){ parite = rxtxport.parity_mark; s = "parité marquée"; } else if (parite.equals("espace")){ parite = rxtxport.parity_space; s = "parité espace"; } else { s = "parité inconnue"; } // system.out.println(s); return s; } //retour de la parite public string getparite(){ s = ""; if (parite == rxtxport.parity_none){ s = "aucune"; } else if (parite == rxtxport.parity_even){ s = "paire"; } else if (parite == rxtxport.parity_odd){ s = "impaire"; } else if (parite == rxtxport.parity_mark){ s = "marque"; } else if (parite == rxtxport.parity_space){ s = "espace"; } return s; } //recuperation du nombre de bits de stop public string setstop(string stop){ s = ""; if (stop.equals("1")){ stop = rxtxport.stopbits_1; s = "1 bit de stop"; } else if (stop.equals("2")){ stop = rxtxport.stopbits_2; s = "2 bits de stop"; } else if (stop.equals("1,5")){ stop = rxtxport.stopbits_1_5; s = "bit de stop 1,5"; } else { s = "bit de stop inconnu"; } // system.out.println(s); return s; } //retour du nombre de bits de stop public string getstop(){ s = ""; if (stop == rxtxport.stopbits_1){ s = "1"; } else if (stop == rxtxport.stopbits_2){ s = "2"; } else if (stop == rxtxport.stopbits_1_5){ s = "1,5"; } return s; } //recuperation du controle public void setcontrole(string controle){ if (controle.equals("matériel")){ controle = serialport.flowcontrol_rtscts_in; } else if (controle.equals("xon/xoff")){ controle = serialport.flowcontrol_xonxoff_in; } else { controle = serialport.flowcontrol_none; } } //retour du controle public string getcontrole(){ string c = null; if (controle == serialport.flowcontrol_none){ c = "aucun"; } if (controle == serialport.flowcontrol_rtscts_in){ c = "matériel"; } if (controle == serialport.flowcontrol_xonxoff_in){ c = "xon/xoff"; } return c; } //ouvrir le port et les flux public void ouvrirport(){ w32driver.initialize(); //ouverture du port try { sport = (rxtxport) portid.open("java_rs232", 2000); } catch (portinuseexception e) { system.err.println(e); } if (sport != null) { //règle les paramètres de la connexion try { sport.setflowcontrolmode(controle); sport.setserialportparams(vitesse, nbbits, stop, parite); //récupération du flux de lecture et écriture du port outstream = sport.getoutputstream(); in = sport.getinputstream(); bufread = new bufferedreader(new inputstreamreader(sport.getinputstream())); ecouteport1 = new ecouteport(in); (new thread(ecouteport1)).start(); } catch (unsupportedcommoperationexception e) { system.err.println(e); } } } // retour de la chaine recu public string getcrecu(){ string c = ""; if (sport != null) { c = ecouteport1.recu(); } return c; } //effacer la chaine recu public void effacercrecu(){ if (sport != null) { ecouteport1.effacerrecu(); } } // ecrire un caractere public void ecrirechar(char envoie){ if (sport != null) { try { //demande de poids outstream.write((int) envoie); outstream.close(); } catch (ioexception e) { } } } //ecrire un tablea de bytes public boolean ecrirebytes(byte[] envoie){ boolean b = false; if (sport != null) { try { outstream.write(envoie); outstream.close(); b = true; } catch (ioexception e) { system.err.println(e); } } return b; } public boolean ecrireint(int envoie){ boolean b = false; if (sport != null) { try { outstream.write(envoie); outstream.close(); b = true; } catch (ioexception e) { system.err.println(e); } } return b; } //fermer le port public void fermerport(){ if (sport != null) { try { outstream.close(); } catch (ioexception e) { system.err.println(e); } ecouteport1.stopthread(); sport.close(); } } } //thread d'ecoutedu port class ecouteport implements runnable{ private boolean running = true; private inputstream in; private string caractrecu; private string srecu = new string(); public ecouteport(inputstream in){ this.in = in; } @override public void run(){ while (running){ byte[] buffer = new byte[1024]; int len = -1; try { while ( ( len = this.in.read(buffer)) > -1 ) { caractrecu = new string(buffer,0,len); srecu = srecu.concat(caractrecu); //byte[] retourtrame = caractrecu.getbytes(); //for (int t = 0;t<retourtrame.length;t++){ // system.out.println((int)retourtrame[t]& 0xff); //} try { // pause thread.sleep(100); } catch (interruptedexception ex) {} } } catch ( ioexception e ) { e.printstacktrace(); } } } //arrette le thread public void stopthread(){ this.running = false; } //eface les caractere recu public void effacerrecu(){ srecu = ""; } //renvoi les caractere recu public string recu(){ return srecu; } }
Comments
Post a Comment