环境:
笔记本-虚拟机-红旗系统-实达金融手柄(串口)和打印机(并口)
场景:
写一个java的demo界面程序,演示操作金融手柄,返回数据后用打印机打印,演示结束。
思路:
java没有直接提供串口和并口开发的包,可以用sum提供的RXTXcomm.jar包
开发步骤:
.安装 rxtx-2.1-7
---------------------------------
(1) Download rxtx:
建议下载 rxtx-2.1-7-bins-r2.zip
网址:http://rxtx.qbang.org/wiki/index.php/Download
(2) 解压 rxtx:
jar -xf rxtx-2.1-7-bins-r2.zip
(3) 安装 rxtx(注意可能需要root权限):
(A) 拷贝文件
32位系统:
拷贝 x86.32/RXTXcomm.jar 至 /opt/java/jre/lib/ext
拷贝 x86.32/librxtxParallel.so、x86.32/librxtxSerial.so 至 /usr/lib
64位系统:
拷贝 x86.64/RXTXcomm.jar 至 /opt/java/jre/lib/ext
拷贝 x86.64/librxtxParallel.so、x86.64/librxtxSerial.so 至 /usr/lib
2,红旗系统的并口驱动没有默认加载.
解决办法:
打开终端, 执行以下命令:
sudo modprobe lp
另外,红旗linux下,进行以下操作时可能需要root权限,必要时用sudo passwd root命令设置root用户密码,再用su命令切到root用户
3,开发demo程序
4,用eclipse打包成demo.jar包,怎么打包jar含第三方的,可以参考上一篇
5,终端: java -jar demo.jar
demo程序:
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
/**
* 实达POS机 串口
*
* @author root
*
*/
public class PosPort implements SerialPortEventListener {
protected static CommPortIdentifier portid = null;
protected static SerialPort comPort = null;
protected int port = 1;
protected int baud = 9600;
protected int dataBits = 8;
protected int stopBits = 1;
protected int parity = 0;
private String getSerialData;
private static OutputStream oStream;
private static InputStream iStream;
private static boolean isInitsucsess;
static byte[] checkCmd = new byte[] { 0x30, 0x30, 0x30, 0x34, 0x30, 0x30,
0x30, 0x31 }; // 查询余额
static byte[] comsumeCmd = new byte[] { 0x30, 0x30, 0x31, 0x36, 0x30, 0x30,
0x30, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30,
0x30, 0x30, 0x30 };// 消费
static final int type_cmd_check = 0x1;
static final int type_cmd_comsume = 0x2;
int current_type_cmd = 0;
private static PosPort mInstance;
public static PosPort getInstance(){
if(mInstance==null){
mInstance = new PosPort();
}
return mInstance;
}
private PosPort() {
// 查看所有串口
// this.listPortChoices();
this.setSerialPortNumber(1);
this.setSerialPortParams();
}
/**
* 查询余额
*/
public void onCheck() {
try {
current_type_cmd = type_cmd_check;
oStream.write(checkCmd);
oStream.flush();
oStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 消费
*/
public void onComsume() {
try {
current_type_cmd = type_cmd_comsume;
oStream.write(comsumeCmd);
oStream.flush();
oStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private int setSerialPortNumber(int Port) {
String defaultPort = null;
String osname = System.getProperty("os.name", "").toLowerCase();
if (osname.startsWith("windows")) {
// windows
defaultPort = "COM2";
} else if (osname.startsWith("linux")) {
// linux
defaultPort = "/dev/ttyS1";
} else if (osname.startsWith("mac")) {
// mac
defaultPort = "????";
} else {
System.out.println("Sorry, your OS isn't supported.");
}
System.out.println("Default port is: " + defaultPort);
try {
portid = CommPortIdentifier.getPortIdentifier(defaultPort);
// portid = CommPortIdentifier.getPortIdentifier(Port);
comPort = (SerialPort) portid.open("Controller", 1000);
} catch (NoSuchPortException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return 1;
} catch (PortInUseException e) {
e.printStackTrace();
return 1;
}
try {
comPort.addEventListener(this);// 增加一个监听
comPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
oStream = comPort.getOutputStream();
iStream = comPort.getInputStream();
comPort.notifyOnDataAvailable(true);
} catch (Exception e) {
if (comPort != null)
comPort.close();
isInitsucsess = false;
return 1;
}
this.port = Port;
isInitsucsess = true;
return 0;
}
private int setSerialPortParams() {
try {
comPort.setSerialPortParams(this.baud, this.dataBits,
this.stopBits, this.parity);
} catch (Exception e) {
System.out.print("串口不支持的操作");
}
return 0;
}
/**
* 读取所有串口名字
*/
private void listPortChoices() {
CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
// iterate through the ports.
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
}
}
}
public static void main(String[] args) {
PosPort s = new PosPort();
//s.onCheck();
s.onComsume();
}
@Override
public void serialEvent(SerialPortEvent event) {
// TODO Auto-generated method stub
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[1000];
try {
while (iStream.available() > 0) {
int numBytes = iStream.read(readBuffer);
}
System.out.println(new String(readBuffer));
//发送打印
this.sendPrint(new String(readBuffer));
} catch (IOException e) {
System.out.println(e.toString());
}
break;
}
}
/**
* 拆字符串,并且发送打印命令
*
* @param d
*/
private void sendPrint(String d) {
String data = d;
switch (current_type_cmd) {
case type_cmd_check:// 查询余额
String[] strs = data.split(" ");
String num = strs[0];
String money = strs[2];
data = "卡号为:" + num.substring(10, num.length()) + "\n可用余额为:"
+ money.substring(7, money.length());
System.out.println(data);
break;
case type_cmd_comsume:// 消费
break;
default:
break;
}
//PrinterPort.getInstance().print(data);
}
}
l