2 pièce(s) jointe(s)
Interface graphique avec communication série
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 coode soure
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
|
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.
Vous trouverez en pièce jointe.
Le fichier comm.jar vous devez l'ajouter au librairie du projet
et le fichier win32com.dll au dossier systeme32 de mon windows7.