POS顾客显示器金额

来源:互联网 发布:mysql 5.5.22.tar.gz 编辑:程序博客网 时间:2024/04/28 01:32

 

import java.io.OutputStream;

import javax.comm.CommPort;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;

import com.isoftstone.supermarket.service.api.IClientDispalyService;

/**
 * 顾客显示器服务类
 */
public class ClientDispalyService implements IClientDispalyService
{

 public static final byte STX = 0x02;
 public static final byte ESC = 0x1B;
 public static final byte CLR = 0x0C;
 public static final byte CR = 0x0D;

 /** 顾客显示屏显示的字符 */
 public static final String PRINTABLE_STR = "0123456789.";

 public static final byte[] ESC_INIT = new byte[] { ESC, '@' };

 @Override
 public void displayClient(String total)
 {
  try
  {
   setOutput("COM1", 2400, "", total, "2");
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 }

 /**
  *
  * @Title: output
  * @Description: 输出数据至串口端
  *
  * @param portName
  *            串口端口
  * @param rate
  *            串行口波特率,默认2400
  * @param displayRate
  *            串行口波特率需要与顾客显示屏的波特设置对应,默认:0。
  * @param data
  *            显示的数据
  * @param state
  *            设置“0 全暗”、“1 单价”、“2 总计”、“3 收款”、“4 找零”等显示。
  * @return: void
  * @Author: liubo
  * @date: 2014年7月21日 下午5:08:03
  */
 private void setOutput(String portName, int rate, String displayRate,
   String data, String state)
 {
  CommPort open = null;
  try
  {
   open = openConnection(portName, rate);
   if(open != null){
    OutputStream os = open.getOutputStream();
    setDisplayBaudRate(os, displayRate);
    initDisplay(os);
    clearDisplay(os);
    outputData(os, data);
    setDisplayStateLight(os, state);
    os.flush();
    os.close();
   }
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  finally
  {
   if (open != null)
   {
    open.close();
   }
  }
 }

 /**
  *
  * @Title: initDisplay
  * @Description: 初始化顾客显示屏
  *
  * @param os
  * @throws Exception
  * @return: void
  * @Author: liubo
  * @date: 2014年7月21日 下午4:11:14
  */
 private static void initDisplay(OutputStream os) throws Exception
 {
  os.write(ESC_INIT);
 }

 /**
  *
  * @Title: clearDisplay
  * @Description: 清空顾客显示屏
  *
  * @param os
  * @throws Exception
  * @return: void
  * @Author: liubo
  * @date: 2014年7月21日 下午4:13:18
  */
 private void clearDisplay(OutputStream os) throws Exception
 {
  os.write(CLR);
 }

 /**
  *
  * @Title: outputData
  * @Description: 向顾客显示屏输出需要显示的数值字符串
  *
  * @param os
  * @param data
  * @throws Exception
  * @return: void
  * @Author: liubo
  * @date: 2014年7月21日 下午4:15:03
  */
 private void outputData(OutputStream os, String data) throws Exception
 {
  if (data == null || data.length() == 0)
  {
   return;
  }
  char[] chars = data.toCharArray();
  boolean hasDot = false;
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < chars.length; i++)
  {
   char c = chars[i];
   if (c == '.' && !hasDot)
   {
    sb.append(c);
   }
   else if (PRINTABLE_STR.indexOf(c) > -1)
   {
    sb.append(c);
   }
  }
  if (hasDot && sb.length() > 9)
  {
   sb.setLength(9);
  }
  else if (sb.length() > 8)
  {
   sb.setLength(8);
  }
  os.write(sendDisplayData(sb.toString()));
 }

 /**
  *
  * @Title: openConnection
  * @Description: 打开顾客显示屏的串行端口,用定串行口后记得关闭打开的输入、输出流和串行口CommPort,否则端口将会被一直占用
  *
  * @param portName
  * @param rate
  * @return
  * @return: CommPort
  * @Author: liubo
  * @date: 2014年7月21日 下午4:12:11
  */
 private CommPort openConnection(String portName, int rate)
 {
  CommPort port = null;
  try
  {
   CommPortIdentifier comm = CommPortIdentifier
     .getPortIdentifier(portName);

   port = comm.open(portName, rate);
   if (port instanceof SerialPort)
   {
    SerialPort rxtx = (SerialPort) port;
    rxtx.setSerialPortParams(rate, SerialPort.DATABITS_8,
      SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
   }
  }
  catch (Exception e)
  {
   System.out.println("open connection exception!。。。");
   e.printStackTrace();
  }
  return port;
 }

 /**
  *
  * @Title: setDisplayStateLight
  * @Description: 设置“0 全暗”、“1 单价”、“2 总计”、“3 收款”、“4 找零”等显示。
  *
  * @param os
  *            顾客显示屏输出流
  * @param state
  *            “0 全暗”、“1 单价”、“2 总计”、“3 收款”、“4 找零”
  * @throws Exception
  * @return: void
  * @Author: liubo
  * @date: 2014年7月21日 下午4:17:12
  */
 private void setDisplayStateLight(OutputStream os, String state)
   throws Exception
 {
  if (state.length() > 0)
  {
   os.write(setDisplayState(state.charAt(0)));
  }
 }

 private byte[] setDisplayState(char n)
 {
  return new byte[] { ESC, 's', (byte) n };
 }

 private void setDisplayBaudRate(OutputStream os, String rate)
   throws Exception
 {
  if (rate.length() > 0)
  {
   os.write(setDisplayRate(rate.charAt(0)));
  }
 }

 private byte[] setDisplayRate(char n)
 {
  return new byte[] { STX, 'B', (byte) n };
 }

 private byte[] sendDisplayData(String data)
 {
  if (data == null || data.length() == 0)
  {
   return new byte[0];
  }
  byte[] bytes = data.getBytes();
  int len = bytes.length + 4;
  byte[] bs = new byte[len];
  bs[0] = ESC;
  bs[1] = 'Q';
  bs[2] = 'A';
  bs[len - 1] = CR;
  for (int i = 0; i < bytes.length; i++)
  {
   bs[i + 3] = bytes[i];
  }
  return bs;
 }

}

将以下文件拷贝到JDK相应目录下
comm.jar
%JAVA_HOME%/lib
%JAVA_HOME%/jre/lib/ext

win32com.dll
%JAVA_HOME%/bin
%JAVA_HOME%/jre/bin
%windir%System32

javax.comm.properties
%JAVA_HOME%/lib
%JAVA_HOME%/jre/lib

0 0
原创粉丝点击