Cześć,napisałem program w javie ktory ma za zadanie komunikowac sie z mikrokontrolerem .Dane maja byc wysylane jak i odbierane .
Problem w tym ze klasa odpowiedzalna za obsługe portu szeregowego a dokladnie metoda serialEvent ktora "nasluchuje"kiedy dane sa dostarczane do portu nie wykonuje sie. Dodam jeszcze ze:
-interfejs został zaimlementowany (SerialPortEventListener)
-metody odpowiedzialne za wyszukiwanie portu ,połączenie z portem ,rozłączenie z portem ,jak i wysyłanie danych działaja ,gdyz podłaczałem arduino jak konwerter usb- uart zwierajac piny rx i tx ,
-dane na pewno sa podsyłane a metoda serialevent nie uruchamia sie ,
Nie skupiał bym sie tutaj na szczegołach co ja wysyłam ,jakie zmienne itp ,na razie chciałbym aby po otrzymaniu danych metoda serialEvent sie wykonala :)
package KNArms.GcodeEditor.pkg;
import gnu.io.*;
import java.awt.Color;
import static java.awt.SystemColor.window;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.TooManyListenersException;
import javax.swing.JComboBox;
/**
*
* @author Maciej Tkacz
*/
public class Serial_port implements SerialPortEventListener
{
private Enumeration ports = null;
private HashMap portMap = new HashMap();
private JComboBox combobox;
private CommPortIdentifier selectedPortIdentifier = null;
private gnu.io.SerialPort serialPort = null;
private InputStream input = null;
private OutputStream output = null;
private boolean bConnected = false;
private String temp_1,state;
private byte singleData;
String logText = "";
String messageString=",,,,,";
final static int TIMEOUT = 2000;
public Serial_port(JComboBox combobox,String temp_1,String comunication_state)
{
this.combobox=combobox;
this.temp_1=temp_1;
this.state=comunication_state;
}
public void searchPorts()
{
ports = CommPortIdentifier.getPortIdentifiers();
while (ports.hasMoreElements())
{
CommPortIdentifier curPort = (CommPortIdentifier)ports.nextElement();
if (curPort.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
combobox.addItem(curPort.getName());
portMap.put(curPort.getName(), curPort);
}
}
}
public String connect()
{
String selectedPort = (String)combobox.getSelectedItem();
selectedPortIdentifier = (CommPortIdentifier)portMap.get(selectedPort);
CommPort commPort = null;
try
{
commPort = selectedPortIdentifier.open("TigerControlPanel", TIMEOUT);
serialPort = (gnu.io.SerialPort)commPort;
setConnected(true);
logText = selectedPort + " opened successfully.";
state=logText;
}
catch (PortInUseException e)
{
logText = selectedPort + " is in use. (" + e.toString() + ")";
state=logText;
}
catch (Exception e)
{
logText = "Failed to open " + selectedPort + "(" + e.toString() + ")";
state=logText;
}
initIOStream();
return logText;
}
final public boolean getConnected()
{
return bConnected;
}
public void setConnected(boolean bConnected)
{
this.bConnected = bConnected;
}
public boolean initIOStream()
{
boolean successful = false;
try {
//
input = serialPort.getInputStream();
output = serialPort.getOutputStream();
successful = true;
return successful;
}
catch (IOException e) {
logText = "I/O Streams failed to open. (" + e.toString() + ")";
return successful;
}
}
public String disconnect()
{
//close the serial port
try
{
serialPort.removeEventListener();
serialPort.close();
input.close();
output.close();
setConnected(false);
logText = "Disconnected.";
}
catch (Exception e)
{
logText = "Failed to close " + serialPort.getName() + "(" + e.toString() + ")";
}
return logText;
}
/**
*
* @param evt
*/
@Override
public void serialEvent(SerialPortEvent evt) {
if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE)
{
byte[] readBuffer = new byte[20];
try
{
temp_1="sdasd";
while (input.available() > 0) {
int numBytes = input.read(readBuffer);
}
temp_1=new String(readBuffer);
System.exit(1);
System.out.print("jest");
}
catch (IOException e)
{
}
}
}
public void writeData(int leftThrottle,int rightThrottle)
{
try
{
output.write(messageString.getBytes());
output.flush();
}
catch (Exception e)
{
logText = "Failed to write data. (" + e.toString() + ")";
}
}
public String getValue()
{
return temp_1;
}
}