我正在使用Eclipse通过Arduino平台与Arduino进行通信。我可以使用Eclipse或Arduino打开一个端口,但我无法使用Eclipse连接到打开的端口。我想通过Arduino平台使用Eclipse发送整数,然后由Arduino接收。 如何将Eclipse连接到Arduino的串口?
这是我的实际代码(我在 Utilisation des ports COM en Java avec RXTX [Résolu] 中找到了它(用法语)):
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;
}
}