java invoice print打印带有无符号字节问题的CommPortIdentifier

时间:2011-04-13 11:14:01

标签: java printing byte unsigned invoice

嘿 我需要在热敏发票打印机中打印发票 我写这个程序就是为了做到这一点(见下文) 但是,出于本地化的原因,我需要将值范围为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");
    }
}

2 个答案:

答案 0 :(得分:1)

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

Java中byte的值范围是:

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

您可以使用\u转义符以十六进制表示法发送值,例如:

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

答案 1 :(得分:0)

您可以使用Unicode转义在源代码中编写此类字符,例如

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