Reception port UDP écriture Port COM
Bonsoir,
j'essaye désespérément d’écrire sur un port COM des données reçu via un port UDP.
Code UDP
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
| import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPReceive {
public static void main(String args[]) throws Exception {
UtilisationFlux toto = new UtilisationFlux();
try {
int port = 6020;
DatagramSocket dsocket = new DatagramSocket(port);
byte[] buffer = new byte[2048];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
dsocket.receive(packet);
String msg = new String(buffer, 0, packet.getLength());
// System.out.println(packet.getAddress().getHostName() + ": "
// + msg);
toto.essai(msg);
packet.setLength(buffer.length);
}
} catch (Exception e) {
System.err.println(e);
}
}
} |
Class COM
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
| import javax.comm.*;
import java.io.OutputStream;
import java.util.Enumeration;
public class UtilisationFlux {
static Enumeration ports;
static CommPortIdentifier pID;
static OutputStream outStream;
SerialPort serPort;
public UtilisationFlux() throws Exception {
serPort = (SerialPort)pID.open("PortWriter",100);
outStream = serPort.getOutputStream();
serPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} /**
* @param args the command line arguments
*/
public void essai(String message) throws Exception{
ports = CommPortIdentifier.getPortIdentifiers();
while(ports.hasMoreElements())
{
pID = (CommPortIdentifier)ports.nextElement();
System.out.println("Port " + pID.getName());
if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
if (pID.getName().equals("COM1"))
{
UtilisationFlux pWriter = new UtilisationFlux();
System.out.println("COM1 found");
}
}
}
outStream.write(message.getBytes());
serPort.close();
}
} |
Et J'obtiens ca comme erreur:
Citation:
Exception in thread "main" java.lang.NullPointerException
at modeevenement.UtilisationFlux.<init>(UtilisationFlux.java:27)
at modeevenement.UDPReceive.main(UDPReceive.java:14)
Java Result: 1
soit les lignes :
Code:
serPort = (SerialPort)pID.open("PortWriter",100);
et
Code:
UtilisationFlux toto = new UtilisationFlux();
Si quelqu'un a une idée.:calim2: