Read/Write to a Character device /dev/usb/lp0
807605Sep 6 2007 — edited Sep 6 2007Hello
Any one has an idea of how to read/write to /dev/usb/lp0 which is a thermo usb printer. I am trying to query the printer.
I have been doing the following but the read is not working.
String DefaultPort = "/dev/usb/lp0";
int query=9;
int cmdStatus[] = {27,5,9};
int i;
int retval;
File outputFile = new File(DefaultPort);
RandomAccessFile out;
//Try writing to /dev/usb/lp0
try{
out = new RandomAccessFile(outputFile,"rw");
for(i=0;i<cmdStatus.length;i++){
out.write(((Integer)cmdStatus).byteValue());
}
out.close();
}catch(Exception e){
System.out.println("Failed to write bytes to /dev/usb/lp0 \n"+e);
}
//Try reading from /dev/usb/lp0
int BUFSIZE = 2048;
byte data[]=new byte[BUFSIZE];//size of your buffer
int datalen = 100; //maximum number of bytes to read
FileInputStream os;
DataInputStream dis;
try{
os=new FileInputStream(outputFile);
dis=new DataInputStream(os);
System.out.print("Trying to read from "+DefaultPort+"\n");
retval=dis.read(data,0,datalen);
System.out.print("retval after reading is "+retval+"\n");
dis.close();
os.close();
}catch(Exception e){
System.out.print("Good write, but could not read data!\n"+e+"\n");
}
The last thing that i get after running the above is
Trying to read from /dev/usb/lp0
This shows im able to write to the printer but not read from it. So my guess is that there is a special way of reading a character device.
Any suggestions?