How communicate with VIRTUAL tty like pty with java on linux ?

P

pcouas

Hi,

How could i communicate on Java with linux VIRTUAL tty, for example
/dev/ptyqf .
I have tried with javacom without result ?
I have tried too with FileOutputStream without succes ?


Regards
Philippe
 
W

Wesley Hall

pcouas said:
Hi,

How could i communicate on Java with linux VIRTUAL tty, for example
/dev/ptyqf .
I have tried too with FileOutputStream without succes ?

Could you be more specific? FileOutputStream should work. What error did
you get? I believe that you need to have root privileges to write
arbeitry data to a terminal.
 
P

pcouas

Wesley Hall a écrit :
Could you be more specific? FileOutputStream should work. What error did
you get? I believe that you need to have root privileges to write
arbeitry data to a terminal.
 
P

pcouas

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 = (byte)from;
}

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());}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top