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

Check on this link

http://java.sun.com/developer/Books/javaprogramming/cookbook/11.pdf

hope it helps

oh, my
that code there is even more complex than mine
using gui just makes undestanding worse
why do i care about parallel ports there?
no parallel ports, no gui...
wow, i didn't know a class Frame existed...
i'll stay with byte[]

aaahh... this high level programming is driving me nuts

well nevermind - half - my previous post, i undestood already where and how is the port open. but it seems this thing transmits a message as soon as the port is ready, that's why it closes right away in the main. but i have another doubt there

boolean openSerialP(){
  portList = CommPortIdentifier.getPortIdentifiers();
  while (portList.hasMoreElements()){
   portId = (CommPortIdentifier)portList.nextElement();
   if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
    String nombre = portId.getName();
    if (!(nombre.equals("COM3"))){
     System.out.println("ENTRANDO AL IF QUE COMPARA EL COM3..." + nombre);
     System.out.println(portId.getName()  +  " - " +  getPortTypeName(portId.getPortType()));

     try{
      serialPort = (SerialPort)portId.open("Read/Write", 2000); 
     }catch (PortInUseException e){
      System.out.println("Port in use.");
      System.out.println(e.toString());
     }

     try{
      outputStream = serialPort.getOutputStream();
      inputStream = serialPort.getInputStream();
     }catch (IOException e)
     {System.out.println(e.toString());}

     try{
        serialPort.addEventListener(this);
     } catch (TooManyListenersException e) 
     {System.out.println(e.toString());}

     try{
      serialPort.notifyOnDataAvailable(true);
      serialPort.notifyOnOutputEmpty(true);
     }catch (Exception e){
      System.out.println("Error setting event notification");
      System.out.println(e.toString());
      System.exit(-1);
     }

     try{
      serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
     }catch (UnsupportedCommOperationException e)
     {System.out.println(e.toString());}



//since the sending starts here...


     eventThread = new Thread(this);
     eventThread.start();
     System.out.println("writing \""+messageString+"\" to " +serialPort.getName());

     try{
      outputStream.write(messageString.getBytes());
     }catch (IOException e)
      {System.out.println(e.toString());}
     break;
    }


// how do i take it out to a method i can call from any other random part - let's make that "main" - of the code?

/* would it be something like this?

public void writingS(String stuff){
     eventThread = new Thread(this);
     eventThread.start();
     System.out.println("writing \""+stuff+"\" to " +serialPort.getName());

     try{
      outputStream.write(stuff.getBytes());
     }catch (IOException e)
      {System.out.println(e.toString());}
}


*/

   }
  }
  return (true);
 }

PLEASE HELP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.