Chinaunix首页 | 论坛 | 博客
  • 博客访问: 42837
  • 博文数量: 7
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-16 13:14
文章分类

全部博文(7)

文章存档

2011年(1)

2010年(2)

2009年(4)

我的朋友
最近访客

分类: Java

2010-02-21 12:04:28

顾客显示屏操作代码


顾客显示屏控制的原理比较简单:
1.打开顾显的串行口
2.设置波特率,这个波特率需要与顾显的一致
3.使用ESC/POS指令进行数据传输。
要结合“EPSON/POS指令集封装”这BLOG文章看。

Java代码
  1. import gnu.io.CommPort;  
  2. import gnu.io.CommPortIdentifier;  
  3. import gnu.io.RXTXPort;  
  4. import gnu.io.SerialPort;  
  5.   
  6. import java.io.IOException;  
  7. import java.io.OutputStream;  
  8. import java.util.HashMap;  
  9. import java.util.Map;  
  10.   
  11. public class ClientDisplay {  
  12.   
  13.     /** 串行端口设置 */  
  14.     public static final String PARAM_PORT_STR = "port";  
  15.     /** 串口波特率设置 */  
  16.     public static final String PARAM_BAUD_RATE_STR = "baudRate";  
  17.     /** 顾客显示屏与串口对应的波特率设置位 */  
  18.     public static final String PARAM_DISPLAY_RATE_STR = "displayRate";  
  19.     /** 需要显示数值的参数 */  
  20.     public static final String PARAM_DATA_STR = "data";  
  21.     /** 状态灯参数 */  
  22.     public static final String PARAM_STATE_STR = "state";  
  23.     /** 状态灯全灭 */  
  24.     public static final String DISPLAY_STATE_OFF = "0";  
  25.     /** 单价状态灯 */  
  26.     public static final String DISPLAY_STATE_PRICE = "1";  
  27.     /** 总计状态灯 */  
  28.     public static final String DISPLAY_STATE_TOTAL = "2 ";  
  29.     /** 收款状态灯 */  
  30.     public static final String DISPLAY_STATE_AMOUNT = "3";  
  31.     /** 找零状态灯 */  
  32.     public static final String DISPLAY_STATE_CHAGNE = "4";  
  33.     /** 顾客显示屏显示的字符 */  
  34.     public static final String PRINTABLE_STR = "0123456789.";  
  35.   
  36.     //public static final String   
  37.   
  38.     public static void main(String[] args) {  
  39.         String port = "COM1";  
  40.         String baudRate = null;  
  41.         String displayRate = null;  
  42.         String data = "1222222";  
  43.         String state = "1111";  
  44.         if (args != null) {  
  45.             for (int i = 0; i < args.length; i++) {  
  46.                 if (args[i].startsWith("-p")) {  
  47.                     port = args[i].substring(2);  
  48.                 } else if (args[i].startsWith("-br")) {  
  49.                     baudRate = args[i].substring(2);  
  50.                 } else if (args[i].startsWith("-dr")) {  
  51.                     displayRate = args[i].substring(2);  
  52.                 } else if (args[i].startsWith("-d")) {  
  53.                     data = args[i].substring(2);  
  54.                 } else if (args[i].startsWith("-s")) {  
  55.                     state = args[i].substring(2);  
  56.                 }  
  57.             }  
  58.         }  
  59.         Map map = new HashMap();  
  60.         map.put(PARAM_PORT_STR, port);  
  61.         map.put(PARAM_BAUD_RATE_STR, baudRate);  
  62.         map.put(PARAM_DISPLAY_RATE_STR, displayRate);  
  63.         map.put(PARAM_DATA_STR, data);  
  64.         map.put(PARAM_STATE_STR, state);  
  65.         try {  
  66.             sendDisplay(map);  
  67.         } catch (Exception e) {  
  68.             e.printStackTrace();  
  69.         }  
  70.     }  
  71.   
  72.     /** 
  73.      * 方法用途和描述:顾客显示屏的输出。 
  74.      * 方法的实现逻辑描述: 
  75.      * @param displayMap 
  76.      * PARAM_PORT_STR         :端口名称,Windows:COM1,Linux:ttyS0  
  77.      * PARAM_BAUD_RATE_STR    :串行口波特率,默认:2400 
  78.      * PARAM_DISPLAY_RATE_STR :串行口波特率需要与顾客显示屏的波特设置对应,默认:0。 
  79.      * (如一些顾显波特率为“2400”对应设置位为“0”,“4800”对应设置位为“1”,“9600”对应设置位为“2”,具体设置需要看其规格文档。) 
  80.      * PARAM_DATA_STR         :需要显示的数值字符串,只能显示8位:“0123456789.”。 
  81.      * PARAM_STATE_STR        :设置“0 全暗”、“1 单价”、“2 总计”、“3 收款”、“4 找零”等显示。(具体设置需要看其规格文档。) 
  82.      * @throws Exception 
  83.      * @author Tyler Chen 新增日期:2008-10-17 
  84.      * @author Tyler Chen 修改日期:2008-10-17 
  85.      */  
  86.     public static void sendDisplay(Map displayMap) throws Exception {  
  87.         Object param1 = displayMap.get(PARAM_PORT_STR);  
  88.         Object param2 = displayMap.get(PARAM_BAUD_RATE_STR);  
  89.         Object param5 = displayMap.get(PARAM_DISPLAY_RATE_STR);  
  90.         Object param3 = displayMap.get(PARAM_DATA_STR);  
  91.         Object param4 = displayMap.get(PARAM_STATE_STR);  
  92.         if (param1 == null || !(param1 instanceof String)) {  
  93.             throw new IllegalArgumentException("PARAM_PORT is not set value!");  
  94.         }  
  95.         String port = param1.toString();  
  96.         int baudRate = 0;  
  97.         String data = "";  
  98.         String state = "";  
  99.         String displayRate = "";  
  100.         if (param2 != null) {  
  101.             try {  
  102.                 baudRate = Integer.valueOf(param2.toString());  
  103.             } catch (Exception e) {}  
  104.         }  
  105.         if (param3 != null) {  
  106.             data = param3.toString();  
  107.         }  
  108.         if (param4 != null) {  
  109.             state = param4.toString();  
  110.         }  
  111.         if (param5 != null) {  
  112.             displayRate = param5.toString();  
  113.         }  
  114.         output(port, baudRate, displayRate, data, state);  
  115.     }  
  116.   
  117.     /** 
  118.      * 方法用途和描述:初始化顾客显示屏。 
  119.      * 方法的实现逻辑描述: 
  120.      * @param os 顾客显示屏输出流 
  121.      * @throws Exception 
  122.      * @author Tyler Chen 新增日期:2008-10-17 
  123.      * @author Tyler Chen 修改日期:2008-10-17 
  124.      */  
  125.     public static void initDisplay(OutputStream os) throws Exception {  
  126.         os.write(EpsonPosPrinterCommand.ESC_INIT);  
  127.     }  
  128.   
  129.     /** 
  130.      * 方法用途和描述:清空顾客显示屏 
  131.      * 方法的实现逻辑描述: 
  132.      * @param os 顾客显示屏输出流 
  133.      * @throws Exception 
  134.      * @author Tyler Chen 新增日期:2008-10-17 
  135.      * @author Tyler Chen 修改日期:2008-10-17 
  136.      */  
  137.     public static void clearDisplay(OutputStream os) throws Exception {  
  138.         os.write(EpsonPosPrinterCommand.CLR);  
  139.     }  
  140.   
  141.     /** 
  142.      * 方法用途和描述:向顾客显示屏输出需要显示的数值字符串 
  143.      * 方法的实现逻辑描述: 
  144.      * @param os 顾客显示屏输出流 
  145.      * @param data 需要显示的数值字符串,只能显示8位:“0123456789.”。 
  146.      * @throws Exception 
  147.      * @author Tyler Chen 新增日期:2008-10-17 
  148.      * @author Tyler Chen 修改日期:2008-10-17 
  149.      */  
  150.     public static void outputData(OutputStream os, String data)  
  151.             throws Exception {  
  152.         if (data == null || data.length() == 0) {  
  153.             return;  
  154.         }  
  155.         char[] chars = data.toCharArray();  
  156.         boolean hasDot = false;  
  157.         StringBuilder sb = new StringBuilder();  
  158.         for (int i = 0; i < chars.length; i++) {  
  159.             char c = chars[i];  
  160.             if (c == '.' && !hasDot) {  
  161.                 sb.append(c);  
  162.             } else if (PRINTABLE_STR.indexOf(c) > -1) {  
  163.                 sb.append(c);  
  164.             }  
  165.         }  
  166.         if (hasDot && sb.length() > 9) {  
  167.             sb.setLength(9);  
  168.         } else if (sb.length() > 8) {  
  169.             sb.setLength(8);  
  170.         }  
  171.         os.write(EpsonPosPrinterCommand.sendDisplayData(sb.toString()));  
  172.     }  
  173.   
  174.     /** 
  175.      * 方法用途和描述:设置“0 全暗”、“1 单价”、“2 总计”、“3 收款”、“4 找零”等显示。(具体设置需要看其规格文档。) 
  176.      * 方法的实现逻辑描述: 
  177.      * @param os 顾客显示屏输出流 
  178.      * @param state “0 全暗”、“1 单价”、“2 总计”、“3 收款”、“4 找零” 
  179.      * @throws Exception 
  180.      * @author Tyler Chen 新增日期:2008-10-17 
  181.      * @author Tyler Chen 修改日期:2008-10-17 
  182.      */  
  183.     public static void setDisplayStateLight(OutputStream os, String state)  
  184.             throws Exception {  
  185.         if (state.length() > 0) {  
  186.             os.write(EpsonPosPrinterCommand.setDisplayState(state.charAt(0)));  
  187.         }  
  188.     }  
  189.   
  190.     /** 
  191.      * 方法用途和描述:设定串行口波特率,默认:2400 
  192.      * 方法的实现逻辑描述: 
  193.      * @param os 顾客显示屏输出流 
  194.      * @param rate 具体需要看顾客显示屏规格文档 
  195.      * @throws Exception 
  196.      * @author Tyler Chen 新增日期:2008-10-17 
  197.      * @author Tyler Chen 修改日期:2008-10-17 
  198.      */  
  199.     public static void setDisplayBaudRate(OutputStream os, String rate)  
  200.             throws Exception {  
  201.         if (rate.length() > 0) {  
  202.             os.write(EpsonPosPrinterCommand.setDisplayRate(rate.charAt(0)));  
  203.         }  
  204.     }  
  205.   
  206.     /** 
  207.      * 方法用途和描述:打开顾客显示屏的串行端口,用定串行口后记得关闭打开的输入、输出流和串行口CommPort,否则端口将会被一直占用。 
  208.      * 方法的实现逻辑描述: 
  209.      * @param portName 端口名称,Windows:COM1,Linux:ttyS0  
  210.      * @param rate  设定串行口的波特率,具体需要看顾客显示屏规格文档 
  211.      * @return 
  212.      * @throws Exception 
  213.      * @author Tyler Chen 新增日期:2008-10-17 
  214.      * @author Tyler Chen 修改日期:2008-10-17 
  215.      */  
  216.     public static CommPort openConnection(String portName, int rate)  
  217.             throws Exception {  
  218.         CommPortIdentifier port = null;  
  219.         CommPort open = null;  
  220.         port = CommPortIdentifier.getPortIdentifier(portName);  
  221.         open = port.open(portName, 100);  
  222.         System.out.println(open.getClass().getName());  
  223.         if (open instanceof RXTXPort) {  
  224.             RXTXPort rxtx = (RXTXPort) open;  
  225.             rxtx.setSerialPortParams(getBaudRate(rate), SerialPort.DATABITS_8,  
  226.                                         SerialPort.STOPBITS_1,  
  227.                                         SerialPort.PARITY_NONE);  
  228.             System.out.println("baud rate:" + rxtx.getBaudRate());  
  229.         } else {  
  230.             throw new IOException(portName + " is not a CommPort port!");  
  231.         }  
  232.         return open;  
  233.     }  
  234.   
  235.     /** 
  236.      * 方法用途和描述:向顾客显示屏输出显示数据 
  237.      * 方法的实现逻辑描述: 
  238.      * @param portName :端口名称,Windows:COM1,Linux:ttyS0  
  239.      * @param rate :串行口波特率,默认:2400 
  240.      * @param displayRate :串行口波特率需要与顾客显示屏的波特设置对应,默认:0。 
  241.      * (如一些顾显波特率为“2400”对应设置位为“0”,“4800”对应设置位为“1”,“9600”对应设置位为“2”,具体设置需要看其规格文档。) 
  242.      * @param data :需要显示的数值字符串,只能显示8位:“0123456789.”。 
  243.      * @param state :设置“0 全暗”、“1 单价”、“2 总计”、“3 收款”、“4 找零”等显示。(具体设置需要看其规格文档。) 
  244.      * @throws Exception 
  245.      * @author Tyler Chen 新增日期:2008-10-17 
  246.      * @author Tyler Chen 修改日期:2008-10-17 
  247.      */  
  248.     public static void output(String portName, int rate, String displayRate,  
  249.             String data, String state) throws Exception {  
  250.         CommPort open = null;  
  251.         try {  
  252.             open = openConnection(portName, rate);  
  253.             OutputStream os = open.getOutputStream();  
  254.             setDisplayBaudRate(os, displayRate);  
  255.             initDisplay(os);  
  256.             clearDisplay(os);  
  257.             outputData(os, data);  
  258.             setDisplayStateLight(os, state);  
  259.             os.flush();  
  260.             os.close();  
  261.         } catch (Exception e) {  
  262.             System.out.println(e.getMessage() + "====\n\n\n\n====");  
  263.             e.printStackTrace();  
  264.             if (e instanceof IOException  
  265.                 && e.getMessage()  
  266.                         .startsWith("Resource temporarily unavailable")) {  
  267.   
  268.             } else {  
  269.                 throw e;  
  270.             }  
  271.         } finally {  
  272.             if (open != null) {  
  273.                 open.close();  
  274.             }  
  275.         }  
  276.     }  
  277.   
  278.     private static int getBaudRate(int rate) {  
  279.         switch (rate) {  
  280.         case 9600:  
  281.             return 9600;  
  282.         case 4800:  
  283.             return 4800;  
  284.         case 2400:  
  285.             return 2400;  
  286.         case 1200:  
  287.             return 1200;  
  288.         case 600:  
  289.             return 600;  
  290.         case 300:  
  291.             return 300;  
  292.         default:  
  293.             return 2400;  
  294.         }  
  295.     }  
  296.   
  297.     private static char getRateByte(int rate) {  
  298.         char rateByte = '2';  
  299.         switch (rate) {  
  300.         case 9600:  
  301.             rateByte = '0';  
  302.             break;  
  303.         case 4800:  
  304.             rateByte = '1';  
  305.             break;  
  306.         case 2400:  
  307.             rateByte = '2';  
  308.             break;  
  309.         case 1200:  
  310.             rateByte = '3';  
  311.             break;  
  312.         case 600:  
  313.             rateByte = '4';  
  314.             break;  
  315.         case 300:  
  316.             rateByte = '5';  
  317.             break;  
  318.         default:  
  319.             break;  
  320.         }  
  321.         return rateByte;  
  322.     }  
  323. }  
阅读(2365) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~