简体   繁体   中英

java invoice print CommPortIdentifier with unsigned bytes issue

Hey, I need to print invoices in a thermal invoice printer I wrote this program to do just that (see below) However, for localization reasons I need send to the printer characters with values range of 0x80 – 0x102 , but I find it impossible since I cannot send a byte with this values (this are unsigned bytes which do not exist in java)

Any ideas on how to add this values to a string that will be sent to the printer ??? Thanks

import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.OutputStream;

public class SimpleWrite {
    static CommPortIdentifier portId;
    static SerialPort serialPort;
    static OutputStream outputStream;

        public static void print(String messageString){
        try {
            portId = CommPortIdentifier.getPortIdentifier( "COM1");
            serialPort = (SerialPort) portId.open("SimpleWrite", 2000);
            outputStream = serialPort.getOutputStream();
            serialPort.setSerialPortParams( 9600,
                                            SerialPort.DATABITS_8,
                                            SerialPort.STOPBITS_1,
                                            SerialPort.PARITY_NONE);
            outputStream.write(messageString.getBytes());
            serialPort.close();
        }catch (IOException e) {                        System.out.println(e.getMessage());
        }catch (PortInUseException e) {                 System.out.println(e.getMessage());
        }catch (UnsupportedCommOperationException e) {  System.out.println(e.getMessage());
        }catch (NoSuchPortException e) {                System.out.println(e.getMessage());}
    }

    public static void main(String[] args) throws IOException{
        print("Hello , world \n\r");
    }
}

Sure you can sent a byte with the hex value 0x80 .

The value ranges for byte in Java is:

-128 .. 127   (in decimal)
0x00 .. 0xff  (in hexadecimal)

You can send values in hex notation with the \\u\u003c/code> escape, like:

print("\u0080");   // sends 0x80 to your thermal printer

您可以使用Unicode转义在源代码中写这样的字符,例如

print("Eighty: \u0080\n");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM