Anomalie lors de l'affichage d'un message via port RS232 .
Salut!
Dans mon projet, je vais crées un programme qui permet la lecture des messages à travers un port série RS232.
en fait, j'ai créé un port serie RS232 virtuel "COM3", et à travers ce port et en utilisant le Hyper-terminal, et avec le code ci-joins en Java sur éclipse, je veux afficher les messages écrits sur l'Hyper-terminal, sur le console d'éclipse.
Mais mon problème c'est qu'il existe un problème lors du transfère, s'il y a 3 caractère consécutive identique (comme par exemple "aaa sss hhh ooo" ), il affiche incorrectement la chaîne (dans le cas de "aaa sss hhh ooo" il m'affiche "aaá ssó hhè ooï"!!
Voici le code Java:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| package RS232_;
import javax.comm.*;
import java.io.*;
import java.util.TooManyListenersException;
public class SerialPortRS232 extends Thread implements SerialPortEventListener {
private CommPortIdentifier portId;
private SerialPort serialPort;
private BufferedReader fluxLecture;
private boolean running;
private PrintWriter out;
//constructeur
public SerialPortRS232(String port) {
//initialisation du driver
//récupération de l'identifiant du port
try {
portId = CommPortIdentifier.getPortIdentifier(port);
System.out.println(portId);
} catch (NoSuchPortException e) {
}
//ouverture du port
try {
serialPort = (SerialPort) portId.open("ModeEvenement", 1000);
} catch (PortInUseException e) {
}
//récupération du flux
try {
fluxLecture =new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
} catch (IOException e) {
}
//ajout du listener
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
}
//paramétrage du port
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_7,SerialPort.STOPBITS_1,SerialPort.PARITY_EVEN);
} catch (UnsupportedCommOperationException e) {
}
System.out.println("port ouvert, attente de lecture");
try {
out = new PrintWriter(serialPort.getOutputStream());
} catch (IOException e) {e.printStackTrace();}
}
/** methode qui permet la conversion du type String To Date (faire un appel a la classe Dates.java)
*
*/
Dates date= new Dates();
/**
* fin Du methode de conversion
* */
//running de thread
public void run() {
running = true;
while (running) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
//fermeture du flux et port
try {
fluxLecture.close();
} catch (IOException e) {
}
serialPort.close();
}
public void serialEvent(SerialPortEvent event) {
//gestion des événements sur le port :
String codeBarre = new String();
//lecture et affichage
try {
codeBarre = (String) fluxLecture.readLine();
} catch (IOException e) {e.printStackTrace();
}
System.out.println(codeBarre);
}
//Fonction envoyer
public void send(String sendMsg) // envoyer un message par le port serie (sendMsg comme parametre)
{
try
{
out.write(sendMsg);
}
catch(Exception e)
{
System.out.println("Est ce que le port ouvert et disponible?\n"+e.getMessage());
System.exit(1); // Exit de l'application
}
}
//l'arret de thread
public void stopThread() {
running = false;
}
// Méthode Main
public static void main(String[] args) throws IOException {
SerialPortRS232 RS232=new SerialPortRS232("COM3");
RS232.start();
//construction flux lecture
BufferedReader lire = new BufferedReader(new InputStreamReader(System.in));
RS232.stopThread();
}
} |
S'il vous plait, vous pouvez m'aider ????