You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1510 lines
56 KiB
1510 lines
56 KiB
using System;
|
|
using System.IO.Ports;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
|
|
namespace n5coredll
|
|
{
|
|
|
|
[ClassInterface(ClassInterfaceType.AutoDual)]
|
|
[Guid("92138541-8E7B-43FD-A4A4-748F28A934CD")]
|
|
[ProgId("n5coredll.N5Core")]
|
|
public class N5Core
|
|
{
|
|
private System.IO.Ports.SerialPort serialPort1;
|
|
private int sendTime;
|
|
private int mTotalRecvCount = 0;
|
|
private Byte[] currReqByteArr;
|
|
private string currResultJsonStr;
|
|
private bool isSending = false;
|
|
private int mRecvTimes;
|
|
private byte[] mTotalRecvResult = new byte[10000];
|
|
private int mRecvIndex = 0;
|
|
|
|
private bool isReadyBack = false;
|
|
private int timeOut = 1200000;
|
|
private bool isGetACK = false;
|
|
|
|
//for serial port config
|
|
private string portName = null;
|
|
private string baudRate = null;
|
|
private string dataBits = null;
|
|
private string stopBits = null;
|
|
private string parity = null;
|
|
|
|
private static String STATUS_SUCC = "00";
|
|
private static String STATUS_NO_TXN = "13";
|
|
private static String ERR_STATUS_BUSY = "104";
|
|
private static String ERR_STATUS_PARAM = "100";
|
|
private static String ERR_STATUS_SYSTEM = "101";
|
|
private static String ERR_STATUS_OPENCOM = "102";
|
|
private static String ERR_STATUS_TIMEOUT = "103";
|
|
private Boolean ifBusy = false;
|
|
|
|
private static N5Core myInstance;
|
|
public N5Core()
|
|
{
|
|
}
|
|
|
|
public static N5Core getInstance()
|
|
{
|
|
if (myInstance == null)
|
|
myInstance = new N5Core();
|
|
return myInstance;
|
|
}
|
|
|
|
public int initN5Core(string pPortName, string pBaudRate, string pDataBits, string pStopBits, string pParity, string pTimeOut)
|
|
{
|
|
if (pPortName != null && !pPortName.Equals(string.Empty))
|
|
this.portName = pPortName;
|
|
else
|
|
this.portName = "COM1";
|
|
|
|
if (pBaudRate != null && !pBaudRate.Equals(string.Empty))
|
|
this.baudRate = pBaudRate;
|
|
else
|
|
this.baudRate = "9600";
|
|
|
|
if (pDataBits != null && !pDataBits.Equals(string.Empty))
|
|
this.dataBits = pDataBits;
|
|
else
|
|
this.dataBits = "8";
|
|
|
|
if (pStopBits != null && !pStopBits.Equals(string.Empty))
|
|
this.stopBits = pStopBits;
|
|
else
|
|
this.stopBits = "One";
|
|
|
|
if (pParity != null && !pParity.Equals(string.Empty))
|
|
this.parity = pParity;
|
|
else
|
|
this.parity = "None";
|
|
|
|
if (pTimeOut != null && !pTimeOut.Equals(string.Empty))
|
|
try
|
|
{
|
|
this.timeOut = int.Parse(pTimeOut);
|
|
|
|
if (this.timeOut < 120000)
|
|
this.timeOut = 120000;
|
|
}
|
|
catch {
|
|
this.timeOut = 120000;
|
|
}
|
|
|
|
else
|
|
this.timeOut = 120000;
|
|
bool result = openSerialPort();
|
|
if (result)
|
|
{
|
|
return 0;
|
|
}
|
|
else
|
|
return -1;
|
|
}
|
|
|
|
private static string getTxnId()
|
|
{
|
|
StringBuilder txnIdSb = new StringBuilder();
|
|
DateTime dt = DateTime.Now;
|
|
txnIdSb.Append(dt.ToString("yyyyMMddHHmmssfff"));
|
|
Console.WriteLine("txnIdSb = " + txnIdSb.ToString());
|
|
int randomTime = 0;
|
|
if (dt.Ticks.ToString().Length > 8)
|
|
{
|
|
Console.WriteLine("Ticks.Length > 8 = " + dt.Ticks.ToString());
|
|
randomTime = int.Parse(dt.Ticks.ToString().Substring(0, 8));
|
|
}
|
|
else
|
|
{
|
|
randomTime = int.Parse(dt.Ticks.ToString());
|
|
}
|
|
|
|
if (randomTime > 0)
|
|
{
|
|
Random random = new Random();
|
|
for (int i = 0; i < 15; i++)
|
|
{
|
|
int thidRandom = random.Next(10);
|
|
txnIdSb.Append(thidRandom);
|
|
}
|
|
Console.WriteLine("txnIdSb.ToString = " + txnIdSb.ToString());
|
|
return txnIdSb.ToString();
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("randomTime is error = " + randomTime);
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
|
|
// For Pacific Coffee only
|
|
/*
|
|
public string requestSale(string dtxnAmt, string dtipAmt, string appId, string qrcValue)
|
|
{
|
|
return processSale(null,dtxnAmt,dtipAmt,appId,qrcValue);
|
|
}
|
|
|
|
public string requestRefund(string dtxnAmt, string appId, string qrcValue)
|
|
{
|
|
return processRefund(null, dtxnAmt, appId, qrcValue);
|
|
}*/
|
|
//For Pacific Coffee only
|
|
|
|
|
|
public string requestSaleWithTimeout(string dtxnId, string dtxnAmt, string dtipAmt, string dCashbackAmt, string ecrRefNo, string currency, string appId, string qrcValue, string pTimeout)
|
|
{
|
|
return processEPSSale(dtxnId, dtxnAmt, dtipAmt, dCashbackAmt, ecrRefNo, currency, appId, qrcValue, "", "", pTimeout);
|
|
}
|
|
|
|
|
|
|
|
private string processEPSSale(string dtxnId, string dtxnAmt, string dtipAmt, string dCashbackAmt, string ecrRefNo, string currency, string appId, string qrcValue, string pan, string expDate, string pTimeout)
|
|
{
|
|
if (ifBusy)
|
|
return RequestResult.createRespErrJson("SALE_RESP", ERR_STATUS_BUSY);
|
|
ifBusy = true;
|
|
|
|
if ("ECR timeout".Equals(sendInnerLinkTestRequest())){
|
|
return RequestResult.createRespErrJson("SALE_RESP", ERR_STATUS_OPENCOM);
|
|
}
|
|
|
|
|
|
SaleResult saleResult = null;
|
|
decimal txnAmt = decimal.Parse(dtxnAmt);
|
|
decimal tipAmt = decimal.Parse(dtipAmt);
|
|
decimal cashbackAmt = 0;
|
|
if (dCashbackAmt != "")
|
|
cashbackAmt = decimal.Parse(dCashbackAmt);
|
|
if (txnAmt.CompareTo(decimal.Parse("0")) < 1 || tipAmt.CompareTo(decimal.Parse("0")) < 0)
|
|
{
|
|
Console.WriteLine("dtxnAmt or dtipAmt parameter error");
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("SALE_RESP", ERR_STATUS_PARAM);
|
|
}
|
|
|
|
sendTime = 0;
|
|
isSending = false;
|
|
mRecvTimes = 0;
|
|
isReadyBack = false;
|
|
currReqByteArr = null;
|
|
currResultJsonStr = null;
|
|
isGetACK = false;
|
|
if (openSerialPort())
|
|
{
|
|
PaymentApp tmpAppId = ObjectUtil.getPaymentApp(appId);
|
|
/*
|
|
if (appId == "CC")
|
|
tmpAppId = PaymentApp.CC;
|
|
else if (appId == "QRC")
|
|
tmpAppId = PaymentApp.QRC;
|
|
else if (appId == "AMEX")
|
|
tmpAppId = PaymentApp.AMEX;
|
|
else if (appId == "CPN")
|
|
tmpAppId = PaymentApp.CPN;
|
|
else if (appId == "DC")
|
|
tmpAppId = PaymentApp.DC;
|
|
else if (appId == "NONE")
|
|
tmpAppId = PaymentApp.NONE;
|
|
else
|
|
tmpAppId = PaymentApp.NONE;
|
|
*/
|
|
if (dtxnId == null)
|
|
dtxnId = getTxnId();
|
|
|
|
string reqSaleStr = SaleResult.toJsonForSale(dtxnId, txnAmt, tipAmt, cashbackAmt, ecrRefNo, currency, tmpAppId, qrcValue, pan, expDate);
|
|
Console.WriteLine("reqSaleStr = " + reqSaleStr);
|
|
if (reqSaleStr == null || "".Equals(reqSaleStr))
|
|
{
|
|
Console.WriteLine("reqSaleStr is null. ");
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("SALE_RESP", ERR_STATUS_PARAM);
|
|
}
|
|
currReqByteArr = packReqMsg(reqSaleStr);
|
|
if (currReqByteArr == null || currReqByteArr.Length == 0)
|
|
{
|
|
Console.WriteLine("currReqByteArr is null. ");
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("SALE_RESP", ERR_STATUS_PARAM);
|
|
}
|
|
|
|
Console.WriteLine("currReqByteArr = " + currReqByteArr);
|
|
Console.WriteLine("currReqByteArr.Length = " + currReqByteArr.Length);
|
|
saleResult = null;
|
|
this.sendReqMsg();
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("serialPort no open. ");
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("SALE_RESP", ERR_STATUS_OPENCOM);
|
|
}
|
|
|
|
int iTimeout = int.Parse(pTimeout);
|
|
|
|
int sleepTime = iTimeout / 1000;
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
Boolean ifEnd = false;
|
|
int inqCount = 0;
|
|
DateTime lastInqTime = DateTime.Now;
|
|
string retrivReusltStr;
|
|
RetrievalResult retrievalResult;
|
|
|
|
while (!ifEnd)
|
|
{
|
|
DateTime nowTime = DateTime.Now;
|
|
TimeSpan sp = nowTime - startTime;
|
|
if (sp.TotalSeconds > sleepTime)
|
|
ifEnd = true;
|
|
else
|
|
{
|
|
if (!isSending && !isGetACK)
|
|
this.resendReqMsg();
|
|
if (isReadyBack)
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
LogUtil.getInstance().addRepeatLog("Received result:" + currResultJsonStr);
|
|
ifBusy = false;
|
|
currResultJsonStr = SaleResult.rebuildSaleRespMsg(currResultJsonStr);
|
|
return currResultJsonStr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ifBusy = false;
|
|
Console.WriteLine("Timeout return : " + DateTime.Now.ToString());
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("SALE_RESP", ERR_STATUS_TIMEOUT);
|
|
}
|
|
|
|
int linkTimeout = 10000;
|
|
public string sendLinkTestRequest()
|
|
{
|
|
if (ifBusy)
|
|
return RequestResult.createRespErrJson("ECHO_RESP", ERR_STATUS_BUSY);
|
|
ifBusy = true;
|
|
sendTime = 0;
|
|
isSending = false;
|
|
mRecvTimes = 0;
|
|
isReadyBack = false;
|
|
currReqByteArr = null;
|
|
currResultJsonStr = null;
|
|
RequestResult reqResult = null;
|
|
isGetACK = false;
|
|
if (openSerialPort())
|
|
{
|
|
currReqByteArr = this.packReqMsg(RequestResult.toJsonForLinkTest());
|
|
if (currReqByteArr == null || currReqByteArr.Length == 0)
|
|
{
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
LogUtil.getInstance().addRepeatLog("Received result:" + currResultJsonStr);
|
|
return RequestResult.createRespErrJson("ECHO_RESP", ERR_STATUS_PARAM);
|
|
//return "Request failed: params error";
|
|
}
|
|
|
|
Console.WriteLine("currReqByteArr = " + currReqByteArr);
|
|
Console.WriteLine("currReqByteArr.Length = " + currReqByteArr.Length);
|
|
this.sendReqMsg();
|
|
}
|
|
else
|
|
{
|
|
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("ECHO_RESP", ERR_STATUS_OPENCOM);
|
|
}
|
|
|
|
int sleepTime = linkTimeout / 1000;
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
Boolean ifEnd = false;
|
|
while (!ifEnd)
|
|
{
|
|
DateTime nowTime = DateTime.Now;
|
|
TimeSpan sp = nowTime - startTime;
|
|
if (sp.TotalSeconds > sleepTime)
|
|
ifEnd = true;
|
|
else
|
|
{
|
|
if (!isSending && !isGetACK)
|
|
this.resendReqMsg();
|
|
if (isReadyBack)
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
ifBusy = false;
|
|
return currResultJsonStr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ifBusy = false;
|
|
Console.WriteLine("Timeout return : " + DateTime.Now.ToString());
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("ECHO_RESP", ERR_STATUS_TIMEOUT);
|
|
}
|
|
|
|
private string sendInnerLinkTestRequest()
|
|
{
|
|
sendTime = 0;
|
|
isSending = false;
|
|
mRecvTimes = 0;
|
|
isReadyBack = false;
|
|
currReqByteArr = null;
|
|
currResultJsonStr = null;
|
|
RequestResult reqResult = null;
|
|
isGetACK = false;
|
|
if (openSerialPort())
|
|
{
|
|
currReqByteArr = this.packReqMsg(RequestResult.toJsonForLinkTest());
|
|
if (currReqByteArr == null || currReqByteArr.Length == 0)
|
|
{
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
LogUtil.getInstance().addRepeatLog("Received result:" + currResultJsonStr);
|
|
return RequestResult.createRespErrJson("ECHO_RESP", ERR_STATUS_PARAM);
|
|
//return "Request failed: params error";
|
|
}
|
|
|
|
Console.WriteLine("currReqByteArr = " + currReqByteArr);
|
|
Console.WriteLine("currReqByteArr.Length = " + currReqByteArr.Length);
|
|
this.sendReqMsg();
|
|
}
|
|
else
|
|
{
|
|
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("ECHO_RESP", ERR_STATUS_OPENCOM);
|
|
}
|
|
|
|
int sleepTime = linkTimeout / 1000;
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
Boolean ifEnd = false;
|
|
while (!ifEnd)
|
|
{
|
|
DateTime nowTime = DateTime.Now;
|
|
TimeSpan sp = nowTime - startTime;
|
|
if (sp.TotalSeconds > sleepTime)
|
|
ifEnd = true;
|
|
else
|
|
{
|
|
if (!isSending && !isGetACK)
|
|
this.resendReqMsg();
|
|
if (isReadyBack)
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
ifBusy = false;
|
|
return currResultJsonStr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ifBusy = false;
|
|
Console.WriteLine("Timeout return : " + DateTime.Now.ToString());
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("ECHO_RESP", ERR_STATUS_TIMEOUT);
|
|
}
|
|
|
|
public string sendSettlementtRequest()
|
|
{
|
|
if (ifBusy)
|
|
return RequestResult.createRespErrJson("SETTLE_RESP", ERR_STATUS_BUSY);
|
|
ifBusy = true;
|
|
sendTime = 0;
|
|
isSending = false;
|
|
mRecvTimes = 0;
|
|
isReadyBack = false;
|
|
currReqByteArr = null;
|
|
currResultJsonStr = null;
|
|
RequestResult reqResult = null;
|
|
isGetACK = false;
|
|
if (openSerialPort())
|
|
{
|
|
currReqByteArr = this.packReqMsg(RequestResult.toJsonForSettlement());
|
|
if (currReqByteArr == null || currReqByteArr.Length == 0)
|
|
{
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("SETTLE_RESP", ERR_STATUS_PARAM);
|
|
//return "Request failed: params error";
|
|
}
|
|
|
|
Console.WriteLine("currReqByteArr = " + currReqByteArr);
|
|
Console.WriteLine("currReqByteArr.Length = " + currReqByteArr.Length);
|
|
this.sendReqMsg();
|
|
}
|
|
else
|
|
{
|
|
ifBusy = false;
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("SETTLE_RESP", ERR_STATUS_OPENCOM);
|
|
}
|
|
|
|
int sleepTime = timeOut / 1000;
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
Boolean ifEnd = false;
|
|
while (!ifEnd)
|
|
{
|
|
DateTime nowTime = DateTime.Now;
|
|
TimeSpan sp = nowTime - startTime;
|
|
if (sp.TotalSeconds > sleepTime)
|
|
ifEnd = true;
|
|
else
|
|
{
|
|
if (!isSending && !isGetACK)
|
|
this.resendReqMsg();
|
|
if (isReadyBack)
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
LogUtil.getInstance().addRepeatLog("Received result:" + currResultJsonStr);
|
|
currResultJsonStr = SettleResult.rebuildSettleRespMsg(currResultJsonStr);
|
|
ifBusy = false;
|
|
return currResultJsonStr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("Timeout return : " + DateTime.Now.ToString());
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("SETTLE_RESP", ERR_STATUS_TIMEOUT);
|
|
}
|
|
|
|
public string requestRetrieval(string txnId, string traceNo, string type, int pTimeOut)
|
|
{
|
|
if (ifBusy)
|
|
return RequestResult.createRespErrJson("SETTLE_RESP", ERR_STATUS_BUSY);
|
|
RetrievalResult retrievalResult = null;
|
|
|
|
sendTime = 0;
|
|
isSending = false;
|
|
mRecvTimes = 0;
|
|
isReadyBack = false;
|
|
currReqByteArr = null;
|
|
currResultJsonStr = null;
|
|
isGetACK = false;
|
|
|
|
if (openSerialPort())
|
|
{
|
|
string reqRetrievalStr = RetrievalResult.toJsonForRetrieval(txnId, traceNo, type);
|
|
Console.WriteLine("reqRetrievalStr = " + reqRetrievalStr);
|
|
if (reqRetrievalStr == null || "".Equals(reqRetrievalStr))
|
|
{
|
|
Console.WriteLine("reqRetrievalStr is null. ");
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("RETRIEVAL_RESP", ERR_STATUS_PARAM);
|
|
}
|
|
currReqByteArr = packReqMsg(reqRetrievalStr);
|
|
if (currReqByteArr == null || currReqByteArr.Length == 0)
|
|
{
|
|
Console.WriteLine("currReqByteArr is null. ");
|
|
closeSerialPort();
|
|
LogUtil.getInstance().addRepeatLog("Received result:" + currResultJsonStr);
|
|
return RequestResult.createRespErrJson("RETRIEVAL_RESP", ERR_STATUS_PARAM);
|
|
}
|
|
|
|
Console.WriteLine("currReqByteArr = " + currReqByteArr);
|
|
Console.WriteLine("currReqByteArr.Length = " + currReqByteArr.Length);
|
|
this.sendReqMsg();
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("serialPort no open. ");
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("RETRIEVAL_RESP", ERR_STATUS_OPENCOM);
|
|
}
|
|
|
|
int sleepTime = pTimeOut / 1000;
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
Boolean ifEnd = false;
|
|
while (!ifEnd)
|
|
{
|
|
DateTime nowTime = DateTime.Now;
|
|
TimeSpan sp = nowTime - startTime;
|
|
if (sp.TotalSeconds > sleepTime)
|
|
ifEnd = true;
|
|
else
|
|
{
|
|
if (!isSending && !isGetACK)
|
|
this.resendReqMsg();
|
|
if (isReadyBack)
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
ifBusy = false;
|
|
currResultJsonStr = RetrievalResult.rebuildRetrievalRespMsg(currResultJsonStr);
|
|
return currResultJsonStr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/*
|
|
for (int i = 0; i < sleepTime; i++)
|
|
{
|
|
Console.WriteLine("retrievalResult = " + retrievalResult);
|
|
if (!isReadyBack)
|
|
{
|
|
if (i > 9 && i % 10 == 0 && !isGetACK)
|
|
{
|
|
this.resendReqMsg();
|
|
}
|
|
Console.WriteLine("Sleep i = " + i + ", " + DateTime.Now.ToString());
|
|
System.Threading.Thread.Sleep(1000);
|
|
}
|
|
else
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
return currResultJsonStr;
|
|
}
|
|
else
|
|
{
|
|
//return "Request failed: sytem error";
|
|
return RequestResult.createRespErrJson("RETRIEVAL_RESP", ERR_STATUS_SYSTEM);
|
|
}
|
|
|
|
}
|
|
}*/
|
|
|
|
//
|
|
Console.WriteLine("Timeout return : " + DateTime.Now.ToString());
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("RETRIEVAL_RESP", ERR_STATUS_TIMEOUT);
|
|
}
|
|
|
|
int abortTimeout = 10000;
|
|
public string sendAbortRequest()
|
|
{
|
|
sendTime = 0;
|
|
isSending = false;
|
|
mRecvTimes = 0;
|
|
isReadyBack = false;
|
|
currReqByteArr = null;
|
|
currResultJsonStr = null;
|
|
isGetACK = false;
|
|
|
|
if (openSerialPort())
|
|
{
|
|
string abortStr = RequestResult.toJsonForAbort();
|
|
Console.WriteLine("abortStr = " + abortStr);
|
|
if (abortStr == null || "".Equals(abortStr))
|
|
{
|
|
Console.WriteLine("abortStr is null. ");
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("ABORT_RESP", ERR_STATUS_PARAM);
|
|
}
|
|
currReqByteArr = packReqMsg(abortStr);
|
|
if (currReqByteArr == null || currReqByteArr.Length == 0)
|
|
{
|
|
Console.WriteLine("currReqByteArr is null. ");
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("ABORT_RESP", ERR_STATUS_PARAM);
|
|
}
|
|
|
|
Console.WriteLine("currReqByteArr = " + currReqByteArr);
|
|
Console.WriteLine("currReqByteArr.Length = " + currReqByteArr.Length);
|
|
this.sendReqMsg();
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("serialPort no open. ");
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("ABORT_RESP", ERR_STATUS_OPENCOM);
|
|
}
|
|
|
|
int sleepTime = abortTimeout / 1000;
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
Boolean ifEnd = false;
|
|
while (!ifEnd)
|
|
{
|
|
DateTime nowTime = DateTime.Now;
|
|
TimeSpan sp = nowTime - startTime;
|
|
if (sp.TotalSeconds > sleepTime)
|
|
ifEnd = true;
|
|
else
|
|
{
|
|
if (!isSending && !isGetACK)
|
|
this.resendReqMsg();
|
|
if (isReadyBack)
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
ifBusy = false;
|
|
return currResultJsonStr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/*
|
|
for (int i = 0; i < sleepTime; i++)
|
|
{
|
|
Console.WriteLine("retrievalResult = " + retrievalResult);
|
|
if (!isReadyBack)
|
|
{
|
|
if (i > 9 && i % 10 == 0 && !isGetACK)
|
|
{
|
|
this.resendReqMsg();
|
|
}
|
|
Console.WriteLine("Sleep i = " + i + ", " + DateTime.Now.ToString());
|
|
System.Threading.Thread.Sleep(1000);
|
|
}
|
|
else
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
return currResultJsonStr;
|
|
}
|
|
else
|
|
{
|
|
//return "Request failed: sytem error";
|
|
return RequestResult.createRespErrJson("RETRIEVAL_RESP", ERR_STATUS_SYSTEM);
|
|
}
|
|
|
|
}
|
|
}*/
|
|
|
|
//
|
|
Console.WriteLine("Timeout return : " + DateTime.Now.ToString());
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("ABORT_RESP", ERR_STATUS_TIMEOUT);
|
|
}
|
|
|
|
int reprintTimeout = 60000;
|
|
public string sendReprintRequest(string txnId, string traceNo, bool isReprint)
|
|
{
|
|
if (ifBusy)
|
|
return RequestResult.createRespErrJson("PRINT_RESP", ERR_STATUS_BUSY);
|
|
ifBusy = true;
|
|
sendTime = 0;
|
|
isSending = false;
|
|
mRecvTimes = 0;
|
|
isReadyBack = false;
|
|
currReqByteArr = null;
|
|
currResultJsonStr = null;
|
|
RequestResult reqResult = null;
|
|
isGetACK = false;
|
|
if (openSerialPort())
|
|
{
|
|
currReqByteArr = this.packReqMsg(RequestResult.toJsonForReprint(txnId,traceNo,isReprint));
|
|
if (currReqByteArr == null || currReqByteArr.Length == 0)
|
|
{
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
LogUtil.getInstance().addRepeatLog("Received result:" + currResultJsonStr);
|
|
return RequestResult.createRespErrJson("PRINT_RESP", ERR_STATUS_PARAM);
|
|
//return "Request failed: params error";
|
|
}
|
|
|
|
Console.WriteLine("currReqByteArr = " + currReqByteArr);
|
|
Console.WriteLine("currReqByteArr.Length = " + currReqByteArr.Length);
|
|
this.sendReqMsg();
|
|
}
|
|
else
|
|
{
|
|
ifBusy = false;
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("PRINT_RESP", ERR_STATUS_OPENCOM);
|
|
}
|
|
|
|
int sleepTime = reprintTimeout / 1000;
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
Boolean ifEnd = false;
|
|
while (!ifEnd)
|
|
{
|
|
DateTime nowTime = DateTime.Now;
|
|
TimeSpan sp = nowTime - startTime;
|
|
if (sp.TotalSeconds > sleepTime)
|
|
ifEnd = true;
|
|
else
|
|
{
|
|
if (!isSending && !isGetACK)
|
|
this.resendReqMsg();
|
|
if (isReadyBack)
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
ifBusy = false;
|
|
return currResultJsonStr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("Timeout return : " + DateTime.Now.ToString());
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("PRINT_RESP", ERR_STATUS_TIMEOUT);
|
|
}
|
|
|
|
public string sendScanRequest()
|
|
{
|
|
if (ifBusy)
|
|
return RequestResult.createRespErrJson("SCAN_RESP", ERR_STATUS_BUSY);
|
|
ifBusy = true;
|
|
sendTime = 0;
|
|
isSending = false;
|
|
mRecvTimes = 0;
|
|
isReadyBack = false;
|
|
currReqByteArr = null;
|
|
currResultJsonStr = null;
|
|
RequestResult reqResult = null;
|
|
isGetACK = false;
|
|
if (openSerialPort())
|
|
{
|
|
currReqByteArr = this.packReqMsg(RequestResult.toJsonForScan());
|
|
if (currReqByteArr == null || currReqByteArr.Length == 0)
|
|
{
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("SCAN_RESP", ERR_STATUS_PARAM);
|
|
//return "Request failed: params error";
|
|
}
|
|
|
|
Console.WriteLine("currReqByteArr = " + currReqByteArr);
|
|
Console.WriteLine("currReqByteArr.Length = " + currReqByteArr.Length);
|
|
this.sendReqMsg();
|
|
}
|
|
else
|
|
{
|
|
ifBusy = false;
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("SCAN_RESP", ERR_STATUS_OPENCOM);
|
|
}
|
|
|
|
int sleepTime = timeOut / 1000;
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
Boolean ifEnd = false;
|
|
while (!ifEnd)
|
|
{
|
|
DateTime nowTime = DateTime.Now;
|
|
TimeSpan sp = nowTime - startTime;
|
|
if (sp.TotalSeconds > sleepTime)
|
|
ifEnd = true;
|
|
else
|
|
{
|
|
if (!isSending && !isGetACK)
|
|
this.resendReqMsg();
|
|
if (isReadyBack)
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
ifBusy = false;
|
|
return currResultJsonStr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("Timeout return : " + DateTime.Now.ToString());
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("SCAN_RESP", ERR_STATUS_TIMEOUT);
|
|
}
|
|
|
|
public string sendReadCardNFCRequest()
|
|
{
|
|
if (ifBusy)
|
|
return RequestResult.createRespErrJson("READ_CARD", ERR_STATUS_BUSY);
|
|
ifBusy = true;
|
|
sendTime = 0;
|
|
isSending = false;
|
|
mRecvTimes = 0;
|
|
isReadyBack = false;
|
|
currReqByteArr = null;
|
|
currResultJsonStr = null;
|
|
RequestResult reqResult = null;
|
|
isGetACK = false;
|
|
if (openSerialPort())
|
|
{
|
|
currReqByteArr = this.packReqMsg(RequestResult.toJsonForReadCardNFC());
|
|
if (currReqByteArr == null || currReqByteArr.Length == 0)
|
|
{
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("READ_CARD", ERR_STATUS_PARAM);
|
|
//return "Request failed: params error";
|
|
}
|
|
|
|
Console.WriteLine("currReqByteArr = " + currReqByteArr);
|
|
Console.WriteLine("currReqByteArr.Length = " + currReqByteArr.Length);
|
|
this.sendReqMsg();
|
|
}
|
|
else
|
|
{
|
|
ifBusy = false;
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("SCAN_RESP", ERR_STATUS_OPENCOM);
|
|
}
|
|
|
|
int sleepTime = timeOut / 1000;
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
Boolean ifEnd = false;
|
|
while (!ifEnd)
|
|
{
|
|
DateTime nowTime = DateTime.Now;
|
|
TimeSpan sp = nowTime - startTime;
|
|
if (sp.TotalSeconds > sleepTime)
|
|
ifEnd = true;
|
|
else
|
|
{
|
|
if (!isSending && !isGetACK)
|
|
this.resendReqMsg();
|
|
if (isReadyBack)
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
ifBusy = false;
|
|
return currResultJsonStr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("Timeout return : " + DateTime.Now.ToString());
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("READ_CARD", ERR_STATUS_TIMEOUT);
|
|
}
|
|
|
|
public string sendAddPointRequest(string dtxnId, string dtxnAmt)
|
|
{
|
|
if (ifBusy)
|
|
return RequestResult.createRespErrJson("ADD_POINT", ERR_STATUS_BUSY);
|
|
ifBusy = true;
|
|
sendTime = 0;
|
|
isSending = false;
|
|
mRecvTimes = 0;
|
|
isReadyBack = false;
|
|
currReqByteArr = null;
|
|
currResultJsonStr = null;
|
|
isGetACK = false;
|
|
decimal txnAmt = decimal.Parse(dtxnAmt);
|
|
if (openSerialPort())
|
|
{
|
|
currReqByteArr = this.packReqMsg(RequestResult.toJsonForAddPoint(dtxnId, txnAmt));
|
|
if (currReqByteArr == null || currReqByteArr.Length == 0)
|
|
{
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("ADD_POINT", ERR_STATUS_PARAM);
|
|
//return "Request failed: params error";
|
|
}
|
|
|
|
Console.WriteLine("currReqByteArr = " + currReqByteArr);
|
|
Console.WriteLine("currReqByteArr.Length = " + currReqByteArr.Length);
|
|
this.sendReqMsg();
|
|
}
|
|
else
|
|
{
|
|
ifBusy = false;
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("ADD_POINT", ERR_STATUS_OPENCOM);
|
|
}
|
|
|
|
int sleepTime = timeOut / 1000;
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
Boolean ifEnd = false;
|
|
while (!ifEnd)
|
|
{
|
|
DateTime nowTime = DateTime.Now;
|
|
TimeSpan sp = nowTime - startTime;
|
|
if (sp.TotalSeconds > sleepTime)
|
|
ifEnd = true;
|
|
else
|
|
{
|
|
if (!isSending && !isGetACK)
|
|
this.resendReqMsg();
|
|
if (isReadyBack)
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
ifBusy = false;
|
|
return currResultJsonStr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("Timeout return : " + DateTime.Now.ToString());
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("ADD_POINT", ERR_STATUS_TIMEOUT);
|
|
}
|
|
|
|
public string sendBatchEnquiryRequest(String dsettleDate)
|
|
{
|
|
if (ifBusy)
|
|
return RequestResult.createRespErrJson("BATCH_ENQUIRY", ERR_STATUS_BUSY);
|
|
ifBusy = true;
|
|
sendTime = 0;
|
|
isSending = false;
|
|
mRecvTimes = 0;
|
|
isReadyBack = false;
|
|
currReqByteArr = null;
|
|
currResultJsonStr = null;
|
|
isGetACK = false;
|
|
if (openSerialPort())
|
|
{
|
|
currReqByteArr = this.packReqMsg(RequestResult.toJsonForBatchEnquiry(dsettleDate));
|
|
if (currReqByteArr == null || currReqByteArr.Length == 0)
|
|
{
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("BATCH_ENQUIRY", ERR_STATUS_PARAM);
|
|
//return "Request failed: params error";
|
|
}
|
|
|
|
Console.WriteLine("currReqByteArr = " + currReqByteArr);
|
|
Console.WriteLine("currReqByteArr.Length = " + currReqByteArr.Length);
|
|
this.sendReqMsg();
|
|
}
|
|
else
|
|
{
|
|
ifBusy = false;
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("BATCH_ENQUIRY", ERR_STATUS_OPENCOM);
|
|
}
|
|
|
|
int sleepTime = timeOut / 1000;
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
Boolean ifEnd = false;
|
|
while (!ifEnd)
|
|
{
|
|
DateTime nowTime = DateTime.Now;
|
|
TimeSpan sp = nowTime - startTime;
|
|
if (sp.TotalSeconds > sleepTime)
|
|
ifEnd = true;
|
|
else
|
|
{
|
|
if (!isSending && !isGetACK)
|
|
this.resendReqMsg();
|
|
if (isReadyBack)
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
ifBusy = false;
|
|
return currResultJsonStr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("Timeout return : " + DateTime.Now.ToString());
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("BATCH_ENQUIRY", ERR_STATUS_TIMEOUT);
|
|
}
|
|
|
|
public string sendSettleEnquiryRequest(string dbatchId)
|
|
{
|
|
if (ifBusy)
|
|
return RequestResult.createRespErrJson("SETTLE_ENQUIRY", ERR_STATUS_BUSY);
|
|
ifBusy = true;
|
|
sendTime = 0;
|
|
isSending = false;
|
|
mRecvTimes = 0;
|
|
isReadyBack = false;
|
|
currReqByteArr = null;
|
|
currResultJsonStr = null;
|
|
isGetACK = false;
|
|
if (openSerialPort())
|
|
{
|
|
currReqByteArr = this.packReqMsg(RequestResult.toJsonForSettleEnquiry(dbatchId));
|
|
if (currReqByteArr == null || currReqByteArr.Length == 0)
|
|
{
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("SETTLE_ENQUIRY", ERR_STATUS_PARAM);
|
|
//return "Request failed: params error";
|
|
}
|
|
|
|
Console.WriteLine("currReqByteArr = " + currReqByteArr);
|
|
Console.WriteLine("currReqByteArr.Length = " + currReqByteArr.Length);
|
|
this.sendReqMsg();
|
|
}
|
|
else
|
|
{
|
|
ifBusy = false;
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("SETTLE_ENQUIRY", ERR_STATUS_OPENCOM);
|
|
}
|
|
|
|
int sleepTime = timeOut / 1000;
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
Boolean ifEnd = false;
|
|
while (!ifEnd)
|
|
{
|
|
DateTime nowTime = DateTime.Now;
|
|
TimeSpan sp = nowTime - startTime;
|
|
if (sp.TotalSeconds > sleepTime)
|
|
ifEnd = true;
|
|
else
|
|
{
|
|
if (!isSending && !isGetACK)
|
|
this.resendReqMsg();
|
|
if (isReadyBack)
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
ifBusy = false;
|
|
return currResultJsonStr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("Timeout return : " + DateTime.Now.ToString());
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("SETTLE_ENQUIRY", ERR_STATUS_TIMEOUT);
|
|
}
|
|
|
|
public string sendSummaryEnquiryRequest(string period)
|
|
{
|
|
if (ifBusy)
|
|
return RequestResult.createRespErrJson("SUMMARY_ENQUIRY", ERR_STATUS_BUSY);
|
|
ifBusy = true;
|
|
sendTime = 0;
|
|
isSending = false;
|
|
mRecvTimes = 0;
|
|
isReadyBack = false;
|
|
currReqByteArr = null;
|
|
currResultJsonStr = null;
|
|
isGetACK = false;
|
|
if (openSerialPort())
|
|
{
|
|
currReqByteArr = this.packReqMsg(RequestResult.toJsonForSummaryEnquiry(period));
|
|
if (currReqByteArr == null || currReqByteArr.Length == 0)
|
|
{
|
|
closeSerialPort();
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("SUMMARY_ENQUIRY", ERR_STATUS_PARAM);
|
|
//return "Request failed: params error";
|
|
}
|
|
|
|
Console.WriteLine("currReqByteArr = " + currReqByteArr);
|
|
Console.WriteLine("currReqByteArr.Length = " + currReqByteArr.Length);
|
|
this.sendReqMsg();
|
|
}
|
|
else
|
|
{
|
|
ifBusy = false;
|
|
closeSerialPort();
|
|
return RequestResult.createRespErrJson("SUMMARY_ENQUIRY", ERR_STATUS_OPENCOM);
|
|
}
|
|
|
|
int sleepTime = timeOut / 1000;
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
Boolean ifEnd = false;
|
|
while (!ifEnd)
|
|
{
|
|
DateTime nowTime = DateTime.Now;
|
|
TimeSpan sp = nowTime - startTime;
|
|
if (sp.TotalSeconds > sleepTime)
|
|
ifEnd = true;
|
|
else
|
|
{
|
|
if (!isSending && !isGetACK)
|
|
this.resendReqMsg();
|
|
if (isReadyBack)
|
|
{
|
|
closeSerialPort();
|
|
if (currResultJsonStr != null && !"".Equals(currResultJsonStr))
|
|
{
|
|
ifBusy = false;
|
|
return currResultJsonStr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("Timeout return : " + DateTime.Now.ToString());
|
|
ifBusy = false;
|
|
return RequestResult.createRespErrJson("SUMMARY_ENQUIRY", ERR_STATUS_TIMEOUT);
|
|
}
|
|
|
|
private void closeSerialPort()
|
|
{
|
|
if (serialPort1 != null && serialPort1.IsOpen)
|
|
{
|
|
Console.WriteLine("SerialPort Close ");
|
|
serialPort1.Close();
|
|
}
|
|
}
|
|
|
|
private bool openSerialPort()
|
|
{
|
|
if (serialPort1 != null && serialPort1.IsOpen)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
Console.WriteLine("portName = " + portName + ", baudRate = " + baudRate + ", dataBits = " + dataBits + ", stopBits = " + stopBits + ", parity = " + parity);
|
|
if (ObjectUtil.isNull(portName) || ObjectUtil.isNull(baudRate) || ObjectUtil.isNull(dataBits) || ObjectUtil.isNull(stopBits) || ObjectUtil.isNull(parity))
|
|
{
|
|
Console.WriteLine("No config data.");
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
if (serialPort1 == null)
|
|
serialPort1 = new System.IO.Ports.SerialPort();
|
|
|
|
serialPort1.PortName = portName;
|
|
Console.WriteLine("serialPort1.PortName = " + serialPort1.PortName);
|
|
serialPort1.BaudRate = Convert.ToInt32(baudRate);
|
|
serialPort1.DataBits = Convert.ToInt32(dataBits);
|
|
serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits);
|
|
serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), parity);
|
|
serialPort1.ReceivedBytesThreshold = 1;
|
|
serialPort1.DataReceived += new SerialDataReceivedEventHandler(m_DataReceived);
|
|
|
|
if (!serialPort1.IsOpen)
|
|
{
|
|
serialPort1.Open();
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception err)
|
|
{
|
|
Console.WriteLine("Exception = " + err);
|
|
|
|
serialPort1 = null;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private void m_DataReceived(object sender, SerialDataReceivedEventArgs eventArgs)
|
|
{
|
|
Console.WriteLine("****** m_DataReceived *******");
|
|
try
|
|
{
|
|
if (serialPort1.IsOpen)
|
|
{
|
|
int recvCount = serialPort1.BytesToRead;
|
|
Console.WriteLine("recvCount = " + recvCount);
|
|
if (recvCount > 0)
|
|
{
|
|
mRecvTimes++;
|
|
Console.WriteLine("mRecvTimes = " + mRecvTimes);
|
|
byte[] recvResult = new byte[recvCount];
|
|
|
|
serialPort1.Read(recvResult, 0, recvCount);
|
|
|
|
Console.WriteLine("**********recvResult.Length = " + recvResult.Length);
|
|
StringBuilder recvResultSB = new StringBuilder();
|
|
for (int i = 0; i < recvResult.Length; i++)
|
|
{
|
|
if (recvResult[i].ToString().Length == 1)
|
|
{
|
|
recvResultSB.Append("0");
|
|
}
|
|
recvResultSB.Append(recvResult[i].ToString());
|
|
}
|
|
Console.WriteLine("**********recvResult = " + recvResultSB.ToString());
|
|
// string recvResultStr = System.Text.Encoding.Default.GetString(recvResult);
|
|
//Console.WriteLine("read result = " + recvResultStr);
|
|
//int thisRecvInt = Int32.Parse(recvResultStr, System.Globalization.NumberStyles.HexNumber);
|
|
|
|
mTotalRecvCount += recvCount;
|
|
Console.WriteLine("mTotalRecvCount = " + mTotalRecvCount);
|
|
|
|
if (recvResult[0] == 0x06 && mRecvTimes ==1)
|
|
{
|
|
isGetACK = true;
|
|
Console.WriteLine("ACK!");
|
|
reset();
|
|
if (isSending)
|
|
{
|
|
sendTime = 0;
|
|
isSending = false;
|
|
}
|
|
return;
|
|
}
|
|
if (recvResult[0] == 0x15)
|
|
{
|
|
Console.WriteLine("NAK!");
|
|
|
|
reset();
|
|
resendReqMsg();
|
|
return;
|
|
}
|
|
if (mRecvTimes == 1)
|
|
{
|
|
if (recvResult[0] == 0x02)
|
|
{
|
|
Console.WriteLine("STX found!");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("STX not found!");
|
|
sendNak();
|
|
reset();
|
|
return;
|
|
}
|
|
}
|
|
if (mTotalRecvCount > 4 && (recvResult[recvCount - 1] == 0x03 || (recvCount > 1 && recvResult[recvCount - 2] == 0x03)))
|
|
{
|
|
if (recvResult[recvCount - 1] == 0x03)
|
|
{
|
|
Console.WriteLine("ETX found but has no LRC!");
|
|
|
|
System.Array.Copy(recvResult, 0, mTotalRecvResult, mRecvIndex, recvCount);
|
|
mRecvIndex += recvCount;
|
|
|
|
byte[] lrcRecvResult = new byte[1];
|
|
int lrcRecvCount = readFromSerialPort(lrcRecvResult);
|
|
if (lrcRecvCount > 0)
|
|
{
|
|
mRecvTimes += 1;
|
|
|
|
Console.WriteLine("LRC found!");
|
|
Console.WriteLine("Total receive times is " + mRecvTimes);
|
|
|
|
System.Array.Copy(lrcRecvResult, 0, mTotalRecvResult, mRecvIndex, lrcRecvCount);
|
|
}
|
|
}
|
|
else if (recvCount > 1 && recvResult[recvCount - 2] == 0x03)
|
|
{
|
|
Console.WriteLine("ETX found!");
|
|
Console.WriteLine("Total receive times is " + mRecvTimes);
|
|
|
|
System.Array.Copy(recvResult, 0, mTotalRecvResult, mRecvIndex, recvCount);
|
|
}
|
|
|
|
byte[] len = new byte[] { mTotalRecvResult[1], mTotalRecvResult[2] };
|
|
int length = 0;
|
|
String lenStr = ObjectUtil.bcd2Str(len);
|
|
Console.WriteLine("Length string is [" + lenStr + "]");
|
|
try
|
|
{
|
|
length = int.Parse(lenStr);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine("Msg length format has error : " + e);
|
|
sendNak();
|
|
reset();
|
|
return;
|
|
}
|
|
if (length != mTotalRecvCount - 5)
|
|
{
|
|
Console.WriteLine("Length not match! length = " + length + ", mTotalRecvCount = " + mTotalRecvCount);
|
|
Console.WriteLine("msg: " + System.Text.Encoding.Default.GetString(mTotalRecvResult));
|
|
sendNak();
|
|
reset();
|
|
return;
|
|
}
|
|
byte[] msg = new byte[length];
|
|
for (int i = 3; i < length + 3; i++)
|
|
{
|
|
msg[i - 3] = mTotalRecvResult[i];
|
|
}
|
|
byte olrc = mTotalRecvResult[4 + length];
|
|
byte[] lrcMsg = new byte[1 + len.Length + msg.Length]; // including ETX, no STX
|
|
System.Array.Copy(mTotalRecvResult, 1, lrcMsg, 0, lrcMsg.Length);
|
|
byte nlrc = ObjectUtil.lrc(lrcMsg);
|
|
if (nlrc == olrc)
|
|
{
|
|
Console.WriteLine("Check LRC success! LRC is [" + ObjectUtil.desToHex(new byte[] { nlrc }) + "]");
|
|
sendAck();
|
|
string jsonMsg = System.Text.Encoding.UTF8.GetString(msg);
|
|
|
|
onMsgReceived(jsonMsg);
|
|
reset();
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Check LRC failed! LRC is [" + ObjectUtil.desToHex(new byte[] { nlrc }) + "]");
|
|
sendNak();
|
|
reset();
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("ETX not found!");
|
|
|
|
System.Array.Copy(recvResult, 0, mTotalRecvResult, mRecvIndex, recvCount);
|
|
mRecvIndex += recvCount;
|
|
}
|
|
|
|
System.Threading.Thread.Sleep(100);
|
|
}
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
private void reset()
|
|
{
|
|
mTotalRecvResult = new byte[4096];
|
|
mTotalRecvCount = 0;
|
|
mRecvIndex = 0;
|
|
mRecvTimes = 0;
|
|
}
|
|
|
|
private void sendToSerialPort(byte[] msg)
|
|
{
|
|
try
|
|
{
|
|
if (!serialPort1.IsOpen)
|
|
{
|
|
serialPort1.Open();
|
|
}
|
|
serialPort1.Write(msg, 0, msg.Length);
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
private void resendReqMsg()
|
|
{
|
|
if (!isSending || sendTime > 3)
|
|
{
|
|
this.isReadyBack = true;
|
|
return;
|
|
}
|
|
sendReqMsg();
|
|
}
|
|
|
|
private void sendAck()
|
|
{
|
|
Console.WriteLine("Execute sendAck!");
|
|
|
|
sendToSerialPort(new byte[] { 0x06 });
|
|
}
|
|
|
|
private void sendNak()
|
|
{
|
|
Console.WriteLine("Execute sendNak!");
|
|
|
|
sendToSerialPort(new byte[] { 0x15 });
|
|
}
|
|
|
|
private void sendReqMsg()
|
|
{
|
|
try
|
|
{
|
|
sendTime++;
|
|
Console.WriteLine("serialPort1 Write start ");
|
|
isSending = true;
|
|
serialPort1.Write(currReqByteArr, 0, currReqByteArr.Length);
|
|
}
|
|
catch { };
|
|
Console.WriteLine("serialPort1 Write end ");
|
|
}
|
|
|
|
private Byte[] packReqMsg(string reqJson)
|
|
{
|
|
Console.WriteLine("packReqMsg -> " + reqJson);
|
|
byte[] bytesJsonReq = System.Text.Encoding.Default.GetBytes(reqJson);
|
|
|
|
//Step 1: Add STX 0x02 in 1st byte and msgLength with BCD format in 2-3 byte
|
|
int msgLength = bytesJsonReq.Length;
|
|
string msgLengthStrWithZeorLeft = msgLength.ToString().PadLeft(4, '0');
|
|
Byte[] msgLengthBCD = ObjectUtil.ConvertFrom(msgLengthStrWithZeorLeft);
|
|
byte[] bytesAddSTXAddMsgLenReq = new byte[bytesJsonReq.Length + 1 + 2];
|
|
bytesAddSTXAddMsgLenReq[0] = 0x02;
|
|
bytesAddSTXAddMsgLenReq[1] = msgLengthBCD[0];
|
|
bytesAddSTXAddMsgLenReq[2] = msgLengthBCD[1];
|
|
System.Array.Copy(bytesJsonReq, 0, bytesAddSTXAddMsgLenReq, 3, bytesJsonReq.Length);
|
|
|
|
//Step 2: calculate LRC
|
|
byte[] bytesAddETXAddMsgLenReq = new byte[bytesJsonReq.Length + 1 + 2];
|
|
|
|
bytesAddETXAddMsgLenReq[0] = msgLengthBCD[0];
|
|
bytesAddETXAddMsgLenReq[1] = msgLengthBCD[1];
|
|
System.Array.Copy(bytesJsonReq, 0, bytesAddETXAddMsgLenReq, 2, bytesJsonReq.Length);
|
|
bytesAddETXAddMsgLenReq[bytesAddETXAddMsgLenReq.Length - 1] = 0x03;
|
|
Byte result_lrc = ObjectUtil.lrc(bytesAddETXAddMsgLenReq);
|
|
|
|
//Step 3: pack
|
|
byte[] finalMsg = new byte[bytesJsonReq.Length + 5];
|
|
System.Array.Copy(bytesAddSTXAddMsgLenReq, 0, finalMsg, 0, bytesAddSTXAddMsgLenReq.Length);
|
|
finalMsg[finalMsg.Length - 2] = 0x03;
|
|
finalMsg[finalMsg.Length - 1] = result_lrc;
|
|
|
|
StringBuilder reqMsgSb = new StringBuilder();
|
|
for (int i = 0; i < finalMsg.Length; i++)
|
|
{
|
|
if (finalMsg[i].ToString().Length == 1)
|
|
{
|
|
reqMsgSb.Append("0");
|
|
}
|
|
reqMsgSb.Append(finalMsg[i].ToString());
|
|
}
|
|
Console.WriteLine("finalMsg = " + reqMsgSb.ToString());
|
|
return finalMsg;
|
|
}
|
|
|
|
private int readFromSerialPort(byte[] msg)
|
|
{
|
|
Console.WriteLine("Execute readFromSerialPort!");
|
|
int recvCount = serialPort1.BytesToRead;
|
|
|
|
if (recvCount > 0)
|
|
{
|
|
byte[] recvResult = new byte[recvCount];
|
|
serialPort1.Read(recvResult, 0, recvCount);
|
|
string recvResultStr = System.Text.Encoding.Default.GetString(recvResult);
|
|
|
|
System.Array.Copy(recvResult, 0, msg, 0, recvCount);
|
|
}
|
|
|
|
return recvCount;
|
|
}
|
|
|
|
private void onMsgReceived(string msgReceived)
|
|
{
|
|
Console.WriteLine("msgReceived = " + msgReceived);
|
|
this.currResultJsonStr = msgReceived;
|
|
this.isReadyBack = true;
|
|
}
|
|
|
|
}
|
|
}
|
|
|