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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
| package javatool;
import com.sun.comm.Win32Driver;
import java.io.*;
import java.util.*;
import javax.comm.*;
import javax.swing.*;
public class JavaSerialPort extends Thread implements Runnable,SerialPortEventListener {
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// Variables declaration - do not modify
private javax.swing.JComboBox PortList;
private javax.swing.JToggleButton StartCapture;
private javax.swing.JTextField Traffic;
private javax.swing.JFrame JFrame;
private javax.swing.GroupLayout Layout;
private CommPortIdentifier portId;
private SerialPort serialPort;
private BufferedReader fluxLecture;
private boolean running;
Thread readthread;
InputStream inputstream;
static String Port;
// End of variables declaration
/**
* Constructeur qui récupère l'identifiant du port et lance l'ouverture.
*/
public JavaSerialPort(String port ) {
initComponents();
//initialisation du driver
Win32Driver w32Driver = new Win32Driver();
w32Driver.initialize();
//récupération de l'identifiant du port
try {
portId = CommPortIdentifier.getPortIdentifier(port);
} catch (NoSuchPortException e) {
}
//ouverture du port
try {
serialPort = (SerialPort) portId.open("Micropixel", 2000);
} catch (PortInUseException e) {
}
//récupération du flux
try {
inputstream = serialPort.getInputStream();
} catch (IOException e) {
}
//ajout du listener
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
}
//paramétrage du port
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(
serialPort.getBaudRate(),
serialPort.getDataBits(),
serialPort. getStopBits(),
serialPort.getParity());
} catch (UnsupportedCommOperationException e) {
}
System.out.println("port ouvert, attente de lecture");
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents () {
PortList = new javax.swing.JComboBox();
StartCapture = new javax.swing.JToggleButton();
Traffic = new javax.swing.JTextField();
JFrame = new javax.swing.JFrame();
JFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
JFrame.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
PortList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "COM1", "COM2" }));
PortList.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PortListActionPerformed(evt);
}
});
StartCapture.setText("Start Capture");
StartCapture.setToolTipText("");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(46, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(StartCapture)
.addGap(95, 95, 95))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(PortList, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Traffic, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(38, 38, 38))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(122, 122, 122)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(PortList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Traffic, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(88, 88, 88)
.addComponent(StartCapture)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
PortList.setSelectedIndex(0);
pack();
}// </editor-fold>
private void PortListActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String SI ;
SI = (String)PortList.getSelectedItem();
Port = SI;
}
public void TrafficShow (String S)
{
}
/**
* @param args the command line arguments
*/
public void run() {
running = true;
while (running) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
//fermeture du flux et port
try {
inputstream.close();
} catch (IOException e) {
}
serialPort.close();
}
/**
* Méthode de gestion des événements.
*/
public void serialEvent(SerialPortEvent event) {
//gestion des événements sur le port :
//on ne fait rien sauf quand les données sont disponibles
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 nymBytes = inputstream.read(readbuffer);
}
System.out.printf(new String(readbuffer));
}catch (IOException e) {System.out.println(e);}
break;
}
}
/*
* Permet l'arrêt du thread
*/
public void stopThread() {
running = false;
}
/**
* Méthode principale de l'exemple.
*/
public static void main(String[] args) {
//Récuperation du port en argument
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TrafficSerialPort().setVisible(true);}});
//lancement de l'appli
JavaSerialPort modeEve=new JavaSerialPort(Port);
modeEve.start();
//"interface utilisateur"
System.out.println("taper q pour quitter");
//construction flux lecture
BufferedReader clavier =
new BufferedReader(new InputStreamReader(System.in));
//lecture sur le flux entrée.
try {
String lu = clavier.readLine();
while (!lu.equals("q")) {
}
} catch (IOException e) {
}
modeEve.stopThread();
}
} |
Partager