【问题标题】:java invoice print CommPortIdentifier with unsigned bytes issuejava发票打印带有无符号字节问题的CommPortIdentifier
【发布时间】:2011-08-04 15:07:17
【问题描述】:

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

【问题讨论】:

    标签: java printing byte unsigned invoice


    【解决方案1】:

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

    byte在Java中的取值范围是:

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

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 谢谢,但这似乎不起作用..我得到了我想要的字母,但由于无符号字节类型的长度变化以及和Unicode 长度
      • 您的函数正在使用 getBytes() 从字符串中获取数据,采用默认编码,对于您感兴趣的字符显然是多字节的。您应该使用 getBytes( ) 采用编码名称,并使用正确编码这些字符的东西——即ISO-8859-1
      • 嘿,谢谢欧内斯特,但似乎情节很厚,我正在使用的打印机具有专有的字符集。所以我认为我需要做两者之一,要么发送一个 ascii 而不是 unicode(我找不到这样做的方法),要么实现新的字符集(在这种情况下我可以使用一些帮助:-) ),任何想法(ps我真的很感谢你的帮助,非常感谢:-))
      • 最终你只需要弄清楚打印机想要查看哪些字节,然后发送它们。阅读所有可以阅读的文档,祝你好运。
      猜你喜欢
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 2020-06-26
      • 2014-11-23
      相关资源
      最近更新 更多