Problème envoi Serial javax.comm
Bonjour,
Voici le code au plus simple, afin de se connecter au port série :
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
| public class SerialPortManager
{
private Win32Driver w32Driver = new Win32Driver();
private SerialPort portSerie;
private SerialPortManager()
{
w32Driver.initialize();
}
private static class Loader
{
private static final SerialPortManager instance = new SerialPortManager();
}
public static SerialPortManager getInstance()
{
return Loader.instance;
}
public SerialPort getPort()
{
CommPortIdentifier portId=null;
try
{
portId=CommPortIdentifier.getPortIdentifier("COM1");
}
catch(NoSuchPortException ex)
{
System.err.println("Impossible to connect to the port");
}
try
{
portSerie=(SerialPort)portId.open("RealTime", 2000);
}
catch (PortInUseException ex)
{
System.err.println("Port already in use.");
}
return portSerie;
}
public SerialPort putConfiguration(SerialPort port)
{
try
{
port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
port.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException ex)
{
System.err.println("Impossible to configure the port that way.");
}
return port;
}
} |
Ici j'envoi une commande à l'appareil branché ("start") par exemple et je lis en boucle les résutats. Le problème est qu'il semble y avoir un problème au niveau de l'envoi de la commande car la carte ne répond rien. Si je fais un start à partir d'un hyperterminal, alors là l'appareil me retourne des valeurs.
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
| public void getDataFromSerial() throws InterruptedException, IOException
{
String message;
SerialPortManager serialPortManager = SerialPortManager.getInstance();
SerialPort serialPort = serialPortManager.getPort();
serialPort = serialPortManager.putConfiguration(serialPort);
InputStreamReader inputSerial = new InputStreamReader(serialPort.getInputStream());
BufferedReader bfInSerial = new BufferedReader(inputSerial);
OutputStream outputSerial = serialPort.getOutputStream();
PrintWriter pwSerial = new PrintWriter(outputSerial);
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader bReader = new BufferedReader(reader);
String s = bReader.readLine();
pwSerial.println(s);
pwSerial.flush();
while(true)
{
try
{
System.out.println("Reading serial..." );
try
{
while((message = bfInSerial.readLine())!= null)
{
if(!message.equals(" "))
{
System.out.println("Serial message: " + message + " (length: " + message.length() + ")");
}
}
}
catch (IOException ioe)
{
System.err.println("Impossible to read from the serial port" );
serialPort.close();
inputSerial.close();
outputSerial.close();
}
}
catch (IOException ex)
{
System.err.println("Cannot instanciate the serial connection");
}
}
} |