hvordan bruger man swingwork threads
Goddag eksperterJeg har en GUI application som kan læse og skrive på serial porten. Når jeg skal sende en fil til serial porten, sender jeg en byte adgang og der bliver modtaget en respons på serial porten hver gang jeg sender en byte. Mit problem er at disse responser bliver først viste i GUI'en efter alle bytes er sendt. Jeg vil have at hver respons bliver viste i GUI'en når den er modtaget men ikke når sendning er færdig. hva gør jeg galt? nedunder er min class der skriver/læser på porten og opdaterer GUI'en med aflæst data.
import java.io.*;
import java.util.*;
import javax.comm.*;
import javax.swing.*;
//import javax.swing.SwingWorker;
public class SimpleRead extends SwingWorker <String, String> implements SerialPortEventListener {
//public class SimpleRead implements SerialPortEventListener {
private static final String SERIALPORT = "COM1";
private CommPortIdentifier portId;
private Enumeration portList;
public InputStream inputStream;
private SerialPort serialPort;
public static String data = "";
private ttteer parent;
public OutputStream outputStream ;
public static contents_of_afile content = new contents_of_afile();
public static browse11 browse = new browse11();
static byte[] dataVector = new byte[10000];
Thread readThread;
public SimpleRead(ttteer parent){
this.parent = parent;
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(SERIALPORT)) {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public void serialEvent(final SerialPortEvent event) {
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[1];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
data = new String(readBuffer);
//System.out.print(data);
//parent.setData(data);
publish(data);
nothing(numBytes);
}
} catch (IOException e) {
e.printStackTrace();
}
break;
}
try{
inputStream.close();
}catch (IOException e){
}
}
int nothing(int ui){
return ui;
}
public void writeToport(){
String sfile = browse.onBrowseSwing();//gets the method that returns the file name
//String sfile = file1;
if(sfile == null){
System.out.println("sfile er null");
}
File file = new File(sfile);//get the file to be sent... this is actually supposed to be from browse
if(file == null){
System.out.println("file er null");
}
try{
dataVector = contents_of_afile.getBytesFromFile(file);//the file is loaded in the dataVector
} catch(IOException e){
}
try{
int len = dataVector.length;
for(int i = 0; i < len; i++ ) {
byte sent_byte = dataVector[i];
if(dataVector[i] == 0x0D && dataVector[i+1] == 0x0A){
try{
Thread.sleep(190);
}
catch(InterruptedException e){
}
if(dataVector == null){
System.out.println("dataVector er null");
}
}else{
//javax.swing.JOptionPane.showMessageDialog(null, "outputStream null: " + (outputStream == null));
outputStream.write(sent_byte);
}
}
}catch(IOException e){
}
}
public String doInBackground() {
// do the serial port writing
writeToport();
// when the session finishes, return whatever result you want,
// & retrieve it with a SwingWorker.get() method
return "";
}
protected void process(final List<String> eventDataChunks) {
// send each piece of event data to the GUI
for (String eventData : eventDataChunks) {
parent.setData(eventData);
//System.out.print(eventData);
}
}
}