Re: How communicate with VIRTUAL tty like pty with java on linux ?
Hi,
I tried now to comunicate with same pty in ouput and input, but i have
Bad Descriptor File, when i tried it ?
Do you have an idea ?
Regards
Philippe
import java.io.*;
public class Class15
{
String portName=null;
FileOutputStream fos2 = null;
FileInputStream fis2 = null;
public byte[] toBytes(char[] from) {
byte[] result = new byte[from.length];
for (int i = 0; i < from.length; i++) {
result[i] = (byte)from[i];
}
return result;
}
public Class15(String mportName) throws Exception
{
portName=mportName;
System.out.println("cl01 portName "+portName);
fos2 = new FileOutputStream(portName);
System.out.println("cl02 portName "+portName);
// fis2 = new FileInputStream(portName); // Ne
fonctionne pas
FileDescriptor fd = fos2.getFD();
fis2 = new FileInputStream(fd);
//fis2 = new FileInputStream(fd.in);
System.out.println("cl03 portName "+portName);
System.out.println("cl04 portName fis2
"+fis2.available());
}
public void go(String xx)
{
try{
fos2.write(toBytes(xx.toCharArray()));
fos2.write('\n');
fos2.flush();
} catch(Exception e)
{
System.out.println("Erreur1 "+e.getMessage());
}
}
public String read(){
String rt=null;
try{
System.out.println("r01 *"+portName+"*");
System.out.println("r03 available
*"+fis2.available());
System.out.println("r04 valid
*"+fis2.getFD().valid());
while (true) {
System.out.println("r05 *"+portName+"*");
byte b[] = new byte[100];
// Read some data from the modem.
int nbytes = fis2.read(b);
System.out.println("r06 *"+nbytes+"*");
System.out.println (new String (b, 0, nbytes));
}
}
catch(Exception e1){
System.out.println(e1.getMessage());
}
return rt;
}
public void close() {
try{
fos2.close();
fis2.close();
}catch(Exception e1){}
}
/**
*
* @param args
*/
public static void main(String[] args)
{
String portName=new String("/dev/ptyqf");
String xx=new String("COUCOU DE JAVA"); // Demande de l'Heure
if(args.length>0)
portName = args[0]; // ex /dev/ttyq0
try{
Class15 x=new Class15(portName);
System.out.println("PORT OUVERT, lancer le BASIC avant de
Valider!");
System.out.println("APPUYER SUR ENTREE pour l'envoi");
System.out.println("fis2 "+x.fis2.available());
//Lecture du clavier
BufferedReader entree=new BufferedReader(new
InputStreamReader(System.in));
while(entree.readLine()!=null &&
!entree.readLine().equalsIgnoreCase("f"))
{
System.out.println("GO");
System.out.println("fis2B "+x.fis2.available());
x.go(xx);
System.out.println("fis2c "+x.fis2.available());
//Test de lecture depuis le BASIC sur le pty
String lu=x.read();
System.out.println("LU depuis le BASIC "+lu);
}
System.out.println("FERMETURE DU FLUX!");
x.close();
}
catch(Exception e1){System.out.println(e1.getMessage());}
}
}