簡體   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