java实现串口pos机打印

1、首先下载Javax.comm 下载地址:http://mdubuc.freeshell.org/Jolt/javacomm20-win32.zip

2、将文件win32comm.dll拷贝到%JAVA_HOME%\bin

3、在项目中导入comm.jar

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;

import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;

public class PrintTest implements SerialPortEventListener {
	InputStream inputStream; // 从串口来的输入流
	OutputStream outputStream;// 向串口输出的流
	SerialPort serialPort; // 串口的引用
	CommPortIdentifier portId;

	public PrintTest(Enumeration portList, String name) {
		while (portList.hasMoreElements()) {
			CommPortIdentifier temp = (CommPortIdentifier) portList
					.nextElement();
			if (temp.getPortType() == CommPortIdentifier.PORT_SERIAL) {// 判断如果端口类型是串口
				if (temp.getName().equals(name)) { // 判断如果端口已经启动就连接
					portId = temp;
				}
			}
		}
		try {
			serialPort = (SerialPort) portId.open("My" + name, 2000);
		} catch (PortInUseException e) {

		}
		try {
			inputStream = serialPort.getInputStream();
			outputStream = serialPort.getOutputStream();
		} catch (IOException e) {
		}
		try {
			serialPort.addEventListener(this); // 给当前串口天加一个监听器
		} catch (TooManyListenersException e) {
		}
		serialPort.notifyOnDataAvailable(true); // 当有数据时通知
		try {
			serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, // 设置串口读写参数
					SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
		} catch (UnsupportedCommOperationException e) {
		}
		System.out.println(portId);
		System.out.println(serialPort);
	}

	public void serialEvent(SerialPortEvent event) {
		switch (event.getEventType()) {
		case SerialPortEvent.BI:
		case SerialPortEvent.OE:
		case SerialPortEvent.FE:
		case SerialPortEvent.PE:
		case SerialPortEvent.CD:
		case SerialPortEvent.CTS:
		case SerialPortEvent.DSR:
		case SerialPortEvent.RI:
		case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
			break;

		case SerialPortEvent.DATA_AVAILABLE:// 当有可用数据时读取数据,并且给串口返回数据
			byte[] readBuffer = new byte[20];

			try {
				while (inputStream.available() > 0) {
					System.out.println(inputStream.available());
					int numBytes = inputStream.read(readBuffer);
					System.out.println(numBytes);
				}
				System.out.println(new String(readBuffer).trim());
			} catch (IOException e) {
				e.printStackTrace();
			}
			break;
		}
	}

	public void send(String content) {
		try {
			outputStream.write((content).getBytes("gbk"));
			// outputStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void ClosePort() {
		System.out.println(serialPort);
		if (serialPort != null) {
			serialPort.close();
		}
	}

}


import java.util.Enumeration;

import javax.comm.CommPortIdentifier;


public class Test {

	   public static void main(String[] args) throws InterruptedException {  
	        Enumeration portList = CommPortIdentifier.getPortIdentifiers(); //得到当前连接上的端口  
	          
	        PrintTest comm3 = new PrintTest(portList,"COM4");  
			int lineOffset=5;
			StringBuffer cmd_sb=new StringBuffer();
			cmd_sb.append(0x1b+" DOWNLOAD \"XJMEI.BAS\"\r");
			cmd_sb.append("CLS\r");
			cmd_sb.append("SET COUNTER @1 1\r");
			cmd_sb.append("@1=\"xjmei\"\r");
			cmd_sb.append("TEXT 60,10,\"3\",0,1,1,@1\r");
			cmd_sb.append("PRINT 1,1\r");
			cmd_sb.append("EOP\r");
	        comm3.send(cmd_sb.toString());  
	        comm3.ClosePort();  
	    }  
}


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值