lecture et ecriture sur un meme /dev/pty sans succes
Bonjour
J'essaye via un programme Java de lire et d'ecrire sur un pty sous Linux, afin que cela soit recuperable par un autre langage de l'autre cote.
J'ai fait deux tests qui ne fonctionne pas
Auriez vous une autre idée ???
Le 1er Class15 fonctionne en Output mais pas en Input ou le Message Bad Descriptor est génére
Le voici
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| 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();
// fos2.close();
} 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());}
}
} |
---------------------------------------------------------------
Le 2eme avec les NIO s'arrete pendant l'ecriture
au niveau du buf.hasRemaining() et l'ecriture ne se fait pas
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
public class Class20 {
WritableByteChannel channel =null;
String portName=new String("/dev/ptyqf");
String xx=new String("COUCOU DE JAVA"); // Demande de l'Heur
public Class20() throws Exception {
System.out.println("cl01 portName "+portName+"
"+this.getClass().getName());
channel = new FileOutputStream( portName).getChannel();
System.out.println("cl02 portName "+portName+"
"+this.getClass().getName());
}
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 void write()
{
System.out.println("write01");
try {
Character CR=new Character('\n');
String zz=new String(xx);
zz=zz.concat(CR.toString());
ByteBuffer buf = ByteBuffer.allocateDirect(zz.length());
System.out.println("write02");
buf.put(toBytes(xx.toCharArray()));
System.out.println("write03");
System.out.println("write04");
// Set the limit to the current position and the
position to 0
// making the new bytes visible for write()
buf.flip();
System.out.println("write05");
// Write the bytes to the channel
int numWritten = channel.write(buf);
System.out.println("write06 "+numWritten );
// Check if all bytes were written
if (buf.hasRemaining()) {
System.out.println("write07");
// If not all bytes were written, move the
unwritten bytes
// to the beginning and set position just after the
last
// unwritten byte; also set limit to the capacity
buf.compact();
} else {
System.out.println("write08");
// Set the position to 0 and the limit to capacity
buf.clear();
}
// }
} catch (Exception e) {
}
System.out.println("write09");
}
public void closewrite() throws Exception
{
// Close the file
channel.close();
}
public static void main(String[] args)
{
try
{
Class20 x=new Class20();
//Lecture du clavier pour etre sur que le prog de test de
l'autre cote est ouvert
BufferedReader entree=new BufferedReader(new
InputStreamReader(System.in));
while(entree.readLine()!=null &&
!entree.readLine().equalsIgnoreCase("f"))
{
System.out.println("GO");
x.write();
}
x.closewrite();
}
catch(Exception e1) {
System.out.println(e1.getMessage());
}
}
} |
Cordialement
Philippe