Bonjour,
Je débute avec la programmation JAVA,
J'ai un PFE contenant une interface graphique avec java,
Cette interface va communiquer avec une carte externe à travers port série.
Mon problème apparait lorsque je choisis un port à travers ComboBox puis je clique sur le bouton Connect,
il me donne l'erreur suivant :
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to javax.comm.CommPortIdentifier.
l'instruction fausse est :
portIdSelected = (CommPortIdentifier)comboCOM.getSelectedItem();
voila le code source:
import com.sun.comm.Win32Driver;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import javax.comm.*;
public class ExempleCOM extends JFrame {
static Enumeration portList;
static CommPortIdentifier portId;
static CommPortIdentifier portIdSelected;
static String messageString = "Hello World !";
static SerialPort serialPort;
static OutputStream outputStream;
static JButton bConnect = new JButton("Connect");
JButton bDeconnect = new JButton("Deconnect");
JLabel lCOM = new JLabel("COM port");
static JComboBox comboCOM = new JComboBox();
SerialPort sPort;
Win32Driver w32Driver = new Win32Driver();
JPanel mainPane = new JPanel();
JPanel eastPane = new JPanel();
JPanel westPane = new JPanel();
JPanel northPane = new JPanel();
JPanel southPane = new JPanel();
JPanel centerPane = new JPanel();
public ExempleCOM () {
listePort();
this.setTitle("Superviseur Poste Transformateur");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setPreferredSize(new Dimension(900, 400));
westPane.setLayout(new GridLayout(2, 2, 5, 5));
westPane.add(lCOM);
westPane.add(comboCOM);
westPane.add(bConnect);
westPane.add(bDeconnect);
setLayout(new BorderLayout());
add(westPane,"West");
add(eastPane,"East");
add(southPane,"South");
add(northPane,"North");
add(centerPane,"Center");
pack();
setVisible(true);
}
public void listePort ()
{
Enumeration portListe=CommPortIdentifier.getPortIdentifiers();
if (portListe==null)
{
System.err.println("Aucun port de communication détecté");
return;
}
while (portListe.hasMoreElements())
{
portId = (CommPortIdentifier) portListe.nextElement();
comboCOM.addItem(portId.getName());
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new ExempleCOM();
portList = CommPortIdentifier.getPortIdentifiers();
bConnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
while (portList.hasMoreElements()) {
portIdSelected = (CommPortIdentifier)comboCOM.getSelectedItem();
try {
serialPort = (SerialPort)
portIdSelected.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) {}
}
}});
}
}
Rq: j'ai essayé un exemple simple de communication série, et ça marche bien.
Afficher la suite