Un probleme pour une connexion RS232 en java, c'est tres urgent!!!

cs_AudiS3 Messages postés 9 Date d'inscription mercredi 18 juin 2003 Statut Membre Dernière intervention 27 mai 2004 - 25 mai 2004 à 20:02
javalegrand Messages postés 1 Date d'inscription mercredi 9 mars 2005 Statut Membre Dernière intervention 9 mars 2005 - 9 mars 2005 à 08:43
Bonjour tt le monde !!
alors voila g un soucis pr ma rs232
tt ce que j'ai c est des exemples recupérés sur javasun

voila ce que c'est:

//simplewriter
import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleWrite
{
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!\n";
static SerialPort serialPort;
static OutputStream outputStream;

public static void main(String[] args)
{
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
if (portId.getName().equals("COM1")) {
// if (portId.getName().equals("/dev/term/a")) {
try
{
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 2000);
}
catch (PortInUseException e)
{}
try
{
outputStream = serialPort.getOutputStream();
}
catch (IOException e) {}
try
{
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {}
try
{
outputStream.write(messageString.getBytes());
}
catch (IOException e)
{}
}
}
}
}
}

//et le read

import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// if (portId.getName().equals("COM1")) {
if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
}
}
}
}

public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}

public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}

public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];

try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}

en fait ce que je voudrais c'est quand j'appui sur un bouton, je puisse envoyer des caractéres ou en recevoir.
Ces boutons se trouvent sur un autre interface et apres je ne sais pas comment faire. Alors si vous pourriez m aider ca serait cool ;-) de plus que c pour mon bts donc merci de me repondre au plus vite merci bcp !!!

1 réponse

javalegrand Messages postés 1 Date d'inscription mercredi 9 mars 2005 Statut Membre Dernière intervention 9 mars 2005
9 mars 2005 à 08:43
Bonjour,

pouriez vous me donner la correction de botre programme car je dois realiser une communication sur le port rs232 ett je n'y arrive pas.
merci c'est urgent.
Jonathan
0
Rejoignez-nous