hello
i need to send and get one char from serial port
but i'm making a mess of it
so i kept reading a few tutorials in the web
in the end i dowloaded and tried a code i found,
it works
but this thing only sends one string and gets one string back, then closes all communication.
i need to leave the port open because i'll be receiving commands from a microcontroller. it is the micro the one who should say when to close the comunications
but in this code i can't find exactly how and why the port is closed
can you explain it to me?
first things first
//all this is quite clear
import java.io.*;
import java.util.*;
import gnu.io.*;
public class WriteReadSerial implements Runnable, SerialPortEventListener{
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "b";
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static boolean outputBufferEmptyFlag = false;
Thread eventThread;
the main...
public static void main(String[] args) {
boolean portFound = false;
String defaultPort = "/dev/term/a";
//I know args[] contains all arguments found when executing, but how comes the first argument is a port by default?
//anyways here they store it
if (args.length > 0) {
defaultPort = args[0];
}
//why are they doing this?
WriteReadSerial puertoSerial = new WriteReadSerial();
//here, it is reading from console? it is not the serial port, right?
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String linea = br.readLine();
//this is the port clossing, i guess
puertoSerial.closeSerialP();
System.out.println(linea);
}catch(Exception e){
e.printStackTrace(); puertoSerial.closeSerialP();
}
}
then, somewhere else in the class, there's this
static void closeSerialP(){
try{
Thread.sleep(2000); // Be sure data is xferred before closing
}catch (Exception e)
{System.out.println(e.toString());}
//THIS IS THE CLOSURE
serialPort.close();
System.out.println("Puerto cerrado...");
System.exit(1);
}
So
what should i do?
delete the call to this method in the previous one and instead place the call in the method that process the microcontroller orders?
other thing i dont understand is: i dont see calls to the reading and writing because in the rest of the code, that is done by event. so why is the port being closed there?
thank you