Witam,
skleiłem kod do nauki i jest spoko. (pierwsza wersja testowa więc kod jest niechlujny)
Zainstalowałem Serial Port Driver i na nim działam.
Wybieram w programie port COM3 lub COM4, wpisuję coś do textArea i wysyłam.
Jednak za pierwszym wysłaniem nie znajduje portu COM3 tylko muszę najpierw do COM4 przesłać (bo go znajduje od razu) i po tym
dopiero znajduje COM3 i działa wszystko dobrze.
Możecie powiedzieć w czym błąd ?
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class MojPrzesyl extends JFrame {
static Enumeration portList;
static CommPortIdentifier portId;
// public static String messageString;
static SerialPort serialPort;
static OutputStream outputStream;
static boolean outputBufferEmptyFlag = false;
boolean portFound = false;
String defaultPort;
String it;
JComboBox item = new JComboBox();
JPanel panel = new JPanel();
JButton pokaz = new JButton("Wyślij");
JTextArea area = new JTextArea(10, 50);
JScrollPane jsp = new JScrollPane(area);
public static void main(String[] args) {
MojPrzesyl sw = new MojPrzesyl();
sw.pack();
}
String str;
public void message() {
str = area.getText();
}
public void send() {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println("Found port " + defaultPort);
portFound = true;
try {
serialPort =
(SerialPort) portId.open("MojPrzesyl", 2000);
} catch (PortInUseException e) {
System.out.println("Port in use.");
continue;
}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
try {
serialPort.notifyOnOutputEmpty(true);
} catch (Exception e) {
System.out.println("Error setting event notification");
System.out.println(e.toString());
// System.exit(-1);
}
System.out.println(
"Writing \"" + str + "\" to "
+ serialPort.getName());
try {
outputStream.write(str.getBytes());
} catch (IOException e) {
}
try {
Thread.sleep(2000); // Be sure data is xferred before closing
} catch (Exception e) {
}
serialPort.close();
// System.exit(1);
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}
}
public MojPrzesyl() {
setVisible(true);
setSize(800, 300);
setDefaultCloseOperation(3);
item.addItem("COM3");
item.addItem("COM4");
panel.add(pokaz);
panel.add(jsp);
panel.add(item);
getContentPane().add(panel);
item.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
it = e.getItem().toString();
if(it.equals("COM3")){
defaultPort = it;
}
if(it.equals("COM4")){
defaultPort = it;
}
}
});
pokaz.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
message();
send();
}
});
}
}