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
    评论
Java可以通过串口通信实现与外部设备的数据交互。下面是Java实现串口通信的基本步骤: 1. 导入扩展包和依赖库,例如RXTXcomm.jar和RXTXcomm.dll等。 2. 配置串口参数,例如波特率、数据位、停止位、校验位等。 3. 打开串口并获取输入输出流。 4. 通过输入输出流进行数据的读写操作。 5. 关闭串口。 下面是一个简单的Java串口通信示例代码: ```java import gnu.io.*; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class SerialPortTest implements SerialPortEventListener { private SerialPort serialPort; public void init() { try { // 获取系统中可用的串口列表 CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM1"); // 打开串口,并设置串口参数 serialPort = (SerialPort) portIdentifier.open("SerialPortTest", 2000); serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); // 获取串口的输入输出流 InputStream inputStream = serialPort.getInputStream(); OutputStream outputStream = serialPort.getOutputStream(); // 监听串口数据 serialPort.addEventListener(this); serialPort.notifyOnDataAvailable(true); // 发送数据 outputStream.write("Hello, Serial Port!".getBytes()); } catch (NoSuchPortException | PortInUseException | UnsupportedCommOperationException | IOException | TooManyListenersException e) { e.printStackTrace(); } } @Override public void serialEvent(SerialPortEvent serialPortEvent) { if (serialPortEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { // 读取串口数据 InputStream inputStream = serialPort.getInputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = inputStream.read(buffer)) != -1) { System.out.println(new String(buffer, 0, len)); } } catch (IOException e) { e.printStackTrace(); } } } public void close() { // 关闭串口 if (serialPort != null) { serialPort.removeEventListener(); serialPort.close(); } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值