刷卡机JAVA对接_java 怎么连接pos机啊,这是厂商给的例子但是我怎么不对,他的貌似也有错...

这是一个使用Java通过JNA库连接和通信SPECTRA Terminal ECR的测试程序。代码展示了如何打开、关闭连接,发送和接收数据。适用于开发者参考,以理解如何将POS机与Java应用程序集成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

人一定要靠自己 发布于 2014/03/04 17:28

阅读 1K+

收藏 0

/*

* SPECTRA Terminal ECR Interface testing program for Win32 Java

* Author: Samuel Fok San Ho (samuel.fok@spectratech.com)- SPECTRA Technologies Holdings Ltd

*

* Compile: javac -cp jna.jar SPT_ECR.java

* Run:     java -cp .;.\jna.jar SPT_ECR

*

* This is the testing program for testing ECR connection for SPECTRA EFT on

* java. This program is used for reference for developers only and without any

* warrenty. Developers are free to modify the source code and plug it to their

* program

*

* This program had been tested on JDK V1.7.0_25, JNA V4.0.0(b2)

*

* VERSIONS

* Date         Who         Desc

* ============================================================================

* 12-Jul-2013  Samuel Fok  Initial Version

*

*/

import com.sun.jna.Library;

import com.sun.jna.Native;

import com.sun.jna.NativeLong;

import com.sun.jna.Platform;

import com.sun.jna.PointerType;

import com.sun.jna.*;

public class SPT_ECR {

public interface sptEcrDLL extends Library {

public static final int RC_FAIL = -2;

public static final int RC_DATA_ERROR = -1;

public static final int RC_OK = 1;

public static final int RC_IDLE = 0;

sptEcrDLL INSTANCE = (sptEcrDLL) Native.loadLibrary ("ECR32XP", sptEcrDLL.class);

short OpenConnection(String lpszDevControl, String lpszDef);

//Example: OpenConnection("COM1","9600,n,8,1");

//Return code: 1: success, -2: fail

short CloseConnection();

//Example: CloseConnection();

//Return code: 1: success, -2: fail

int SendComm(String lpvBuf, int cbWrite);

//Example: SendComm("0ECR_REFERENCE_NUMBER000000000010000000000000", 41)

//Return code: success: number of bytes, -2: unopened port, other: fail

int ReceiveComm(Pointer lpvBuf);

//Example:

//Pointer p = new Memory(2048);

//rc = ecrdll.ReceiveComm(p);

//Return code success will return number of bytes, 0 for no message, -2 for fail

short ReceiveStatus();

//Example: ReceiveStatus();

//Return code: Message available = 1, Idle = 0, data error = -1, fail = -2

short SendStatus();

//Example: SendStatus();

//Return code: Message pending = 1, Idle = 0, data error = -1, fail = -2

}

public static void main(String[] args) {

int rc;

sptEcrDLL ecrdll = sptEcrDLL.INSTANCE;

//TODO modify comport

if (ecrdll.OpenConnection("COM1", "9600,n,8,1") != sptEcrDLL.RC_OK) {

System.out.println("OpenConnection Fail");

return;

}

while (ecrdll.SendStatus() != sptEcrDLL.RC_IDLE) {

//TODO Add sleep

}

//Send Command

//TODO modify command, please refer to ECR specification

String ecrCmd = "0ECR_REFERENCE_NO000000000010000000000000";

rc = ecrdll.SendComm(ecrCmd, ecrCmd.length());

if (rc <= 0) {

System.out.println("SendComm Fail");

return;

} else {

System.out.println("SendComm-> Len[" + rc + "]:" + ecrCmd);

}

//Receive

Pointer recBuf = new Memory(2048);

while (true) {

rc = ecrdll.ReceiveStatus();

if (rc == sptEcrDLL.RC_IDLE) {

//idle

//TODO: Add sleep

} else if (rc == sptEcrDLL.RC_OK) {

//data available

rc = ecrdll.ReceiveComm(recBuf);

if (rc > 0) {

System.out.println("ReceiveComm-> Len[" + rc + "]:" + recBuf.getString(0));

}

//if (recBuf.getChar(0) != 'P') {

if (rc > 17) {

//may be skip polling message

break;

}

}

}

//Close Connection

ecrdll.CloseConnection();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值