人一定要靠自己 发布于 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();
}
}