We help IT Professionals succeed at work.

Serial port printer problem

aponcealbuerne
on
Hi:
This is in WinNT and W2K i'm using MS J++
I'm writing to a serial port (com1) in this port is a serial printer connected.
My problem is where the data that i'm writing to the port is too large, the printer stops printing before ending all the bytes i'm writing. There is no exception thrown.

The print method is called for every byte, but i store
them in a byte array and when it reaches to 511 bytes I send it to the printer.


My code:
protected void print(byte byte0)
{
  FileOutputStream dc2;
  BufferedOutputStream pw;

  byArrSend[count] = byte0;
  count++;
 //When I receive 511 bytes then write to the printer.
 //or byte 25 which means cut paper.
  if(count == 511 || byte0 == 25)
  {
     boolean blnError = false;
     while(!blnError)
     {    
     try
     {
     dc2 = new FileOutputStream("Com" + intNumPort);
     pw = new BufferedOutputStream(dc2);
     Util.sleep(250L);
     char chr[] = new  char[511];
     dc2.write(byArrSend,0,count);
     dc2.flush();
     pw.flush();
     Util.sleep(10L);
     try
     {
       dc2.getFD().sync();
     }
     catch(SyncFailedException es)
     {
       System.out.println(es.toString());
     }
     Util.sleep(3500L);
     dc2.close();
     pw.close();          
     byArrSend = new  byte[511];
     count = 0;
     blnError = true;
     }
     catch(IOException e)
     {
          System.out.println("Write error");
     }
     }

Notes:
If i increase de sleep time, I got more characters writed in the printer, but if the document is too large anyway the printer stops printing.
I think maybe I have to check for the CTS (clear to send) or something like that, but how do i do that??

Thanks in advance.
Comment
Watch Question

Reading and writing to serial ports in Java should really use the Java Communications API.

See http://java.sun.com/products/javacomm/index.html for information.

This API gives better access to the serial ports allowing for setting and checking of serial parameters and port states (such as CTS).

HTH

--Steve

Author

Commented:
sdussinger:

I downloaded some code from your link, but I can't make it work.
When i use the portList = CommPortIdentifier.getPortIdentifiers() it seems to doesn't find any ports.

because when
portList.hasMoreElements() is called there are no elements?
What should I do?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
You need to install the javacomm dll and properties files in the correct locations.
dll in <jdk>/bin and properties in <jdk>/lib I think.
objects is right. The dll which is part of the Javacomm install needs to be in the runtime bin. If you have your jdk installed in C:\jdk then copy the win32com.dll to *both* C:\jdk\bin and C:\jdk\jre\bin and copy the javax.comm.properties to C:\jdk\lib and C:\jdk\jre\lib.

This worked for me...

--Steve

Author

Commented:
Ok, yes I wasn't installed correctly the win32com.dll.
Now I did it. But still having problems with the isCTS(), it always return false. How should I call this method?
Have you set the flow control using setFlowControlMode? This controls whether the CTS/RTS handshaking is enabled.

--Steve

Author

Commented:
Ok I set:
try
{
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_OUT);
}
catch(UnsupportedCommOperationException e1)
{
System.out.println("no flow");    
}

like this, there is no exception thrown, but it still return false in isCTS()
Are you sure that the device you're connected to is using RTS/CTS?

That's the only reason I can see for that to fail.

--Steve

Author

Commented:
Well is using DTR/DSR so what should i do?
In that case I believe you want to remove the flow control stuff and use the isDSR to determine if the device is ready.

You could also set a listener for the DSR flag and when it gets set, send data to the device.

--Steve

Author

Commented:
ok,i'll try and let you know.

Author

Commented:
Steve:

If I set a listener for the DSR flag or any other the only thing i get is a "WaitCommEvent: Error 1" and my
serialEvent(SerialPortEvent event) function is never reached.
Have you tried just using the isDSR function to see if the port is ready?

I have no clue about the error you're getting from the listener. I'll have to do a little more digging to figure that one out.

--Steve

Author

Commented:
Steve:
Ok, I solved the problem, it was my fault, there was a generic printer attached to the port ("com1") so that was the problem. Everything is working now. Now the isCTS() function works perfect!! thank you the points are yours.

Author

Commented:
Steve:
Thanks for your time!
Glad I could help...

--Steve

Explore More ContentExplore courses, solutions, and other research materials related to this topic.