繁体   English   中英

Java发票打印CommPortIdentifier与无符号字节问题

[英]java invoice print CommPortIdentifier with unsigned bytes issue

嘿,我需要在热发票打印机中打印发票,而我编写此程序就是为了做到这一点(请参见下文)。但是,由于本地化的原因,我需要将值范围为0x80 – 0x102的字符发送到打印机,但是我发现这是不可能的,因为我无法使用此值发送字节(这是Java中不存在的无符号字节)

关于如何将此值添加到将发送到打印机的字符串的任何想法? 谢谢

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

当然,您可以发送一个十六进制值为0x80的字节。

Java中byte的值范围是:

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

您可以使用\\u\u003c/code>转义以十六进制表示形式发送值,例如:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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