private string ReadCardTemp() { string strCardNum = ""; string strTxt = Path.Combine(Application.StartupPath, "CardMain", "TempCard.txt"); LBErrorLog.InsertFileLog("LB.ReadCard ReadCardTemp:" + strTxt); if (File.Exists(strTxt)) { string[] files = File.ReadAllLines(strTxt); if (files.Length > 0) { List <string> lstCard = new List <string>(); lstCard.AddRange(files); strCardNum = lstCard[0]; lstCard.RemoveAt(0); StringBuilder strBuild = new StringBuilder(); if (lstCard.Count > 0) { foreach (string str in lstCard) { strBuild.AppendLine(str); } } File.WriteAllText(strTxt, strBuild.ToString()); } } return(strCardNum); }
private void Timer_Tick(object sender, EventArgs e) { try { string strCard = LBCardHelper.ReadCardCode(); //string strCard = ReadCardTemp();//测试模拟读卡时用 ReadCount++; if (strCard != "") { this.richTextBox1.AppendText(strCard); this.richTextBox1.AppendText(Environment.NewLine); WriteCardCode(strCard); timer.Enabled = false; this.Close(); } if (ReadCount == 6)//超过6次未读卡成功就自动关闭 { timer.Enabled = false; this.Close(); } } catch (Exception ex) { LBErrorLog.InsertFileLog("LB.ReadCard Timer_Tick:" + ex.Message); this.Close(); } }
//private static void ReadCardAction() //{ // while (true) // { // try // { // string strCode = ReadCardCode(); // if (strCode != "") // { // CardCode = strCode; // //CardInfo cardInfo = new CardInfo(strCode, "", true); // //lock (LBCardHelper.LstCardCode) // // LstCardCode.Add(cardInfo); // } // Thread.Sleep(500); // } // catch(Exception ex) // { // LBErrorLog.InsertFileLog("ReadCardAction:" + ex.Message); // } // } //} public static string ReadCardDirect() { string strCode = ""; try { if (IsUseReadCard) { strCode = ReadCardCode(); } } catch (Exception ex) { LBErrorLog.InsertFileLog("ReadCardAction:" + ex.Message); } return(strCode); }
public Form1(string strKey, string strIP, string strPort, string strRate) { InitializeComponent(); int port; int.TryParse(strPort, out port); byte rate; byte.TryParse(strRate, out rate); this.strKey = strKey; LBCardHelper.IsUseNet = true; LBCardHelper.NetIPAddress = strIP; LBCardHelper.NetPort = port; LBCardHelper.Rate = rate; LBCardHelper.IsUseReadCard = true; LBErrorLog.InsertFileLog("Form1:" + strKey + "_" + strIP + "_" + strPort + "_" + strRate); }
public static void StartSerial() { //bool bolIsUse = false;//是否启用读卡器或者写卡器 try { if (mTimerVerify == null) { mTimerVerify = new System.Windows.Forms.Timer(); mTimerVerify.Interval = 2000; mTimerVerify.Tick += mTimerVerify_Tick; mTimerVerify.Enabled = true; } } catch (Exception ex) { LBErrorLog.InsertFileLog("StartSerial:" + ex.Message); } try { if (COMIsOpen) { ClosePort(); } if (IsUseReadCard && !COMIsOpen) { COMIsOpen = OpenPort(); } //if (IsUseReadCard) //{ // if (mThread == null) // { // mThread = new Thread(ReadCardAction); // mThread.Start(); // } //} } catch (Exception ex) { } }
public static string ReadCardCode() { if (!IsUseReadCard) { return(""); } string bolCardCode = ""; int i; int CardNum = 0; int Totallen = 0; int EPClen, m; byte[] EPC = new byte[5000]; int CardIndex; string temps; string s, sEPC; bool isonlistview; bool fIsInventoryScan = true; byte AdrTID = 0; byte LenTID = 0; byte TIDFlag = 0; LBErrorLog.InsertFileLog("开始读卡"); int fCmdRet = StaticClassReaderB.Inventory_G2(ref fComAdr, AdrTID, LenTID, TIDFlag, EPC, ref Totallen, ref CardNum, frmcomportindex); if ((fCmdRet == 1) | (fCmdRet == 2) | (fCmdRet == 3) | (fCmdRet == 4) | (fCmdRet == 0xFB))//代表已查找结束, { byte[] daw = new byte[Totallen]; Array.Copy(EPC, daw, Totallen); temps = ByteArrayToHexString(daw); //fInventory_EPC_List = temps; //存贮记录 m = 0; if (CardNum == 0) { LBErrorLog.InsertFileLog("ReadCardCode_GetReturnCodeDesc_Start"); string strMsg = GetReturnCodeDesc(fCmdRet); LBErrorLog.InsertFileLog("ReadCardCode_GetReturnCodeDesc_End:" + strMsg); //LBErrorLog.InsertFileLog("卡号为空:"+ strMsg); fIsInventoryScan = false; return(""); } for (CardIndex = 0; CardIndex < CardNum; CardIndex++) { EPClen = daw[m]; if (temps.Length >= m * 2 + 2 + EPClen * 2) { bolCardCode = temps.Substring(m * 2 + 2, EPClen * 2); } else { bolCardCode = ""; LBErrorLog.InsertFileLog("解析卡号Substring异常"); break; } } if (bolCardCode != "") { LBErrorLog.InsertFileLog("Lock(mLstCard):" + bolCardCode); lock (mLstCard) { if (mLstCard.Count < 100) { mLstCard.Add(bolCardCode); } else { mLstCard.Clear(); } } LBErrorLog.InsertFileLog("读卡S:" + bolCardCode); } } else { string strMsg = GetReturnCodeDesc(fCmdRet); LBErrorLog.InsertFileLog("读卡F:" + strMsg); //LBErrorLog.InsertErrorLog("读卡错误:"+strMsg, 0); switch (fCmdRet) { case 0x30: //通讯错误 case 0x36: //端口已关闭 case 0xEE: //返回指令错误 if (IsUseReadCard) { try { ClosePort(); COMIsOpen = OpenPort(); LBErrorLog.InsertFileLog("读卡F后重开:" + (COMIsOpen?"成功":"失败")); } catch (Exception ex) { LBErrorLog.InsertFileLog("读卡F后重开报错:" + ex.Message); } } break; default: //CardInfo cardInfo = new CardInfo("", strMsg, false); //lock (LBCardHelper.LstCardCode) // LstCardCode.Add(cardInfo); break; } } return(bolCardCode); }
public static bool OpenPort() { bool ComOpen = false; int port = 0; int openresult, i; openresult = 30; if (!_IsUseNet) { port = Convert.ToInt32(_Com.Substring(3, _Com.Length - 3)); } int fOpenComIndex = 0; for (i = 6; i >= 0; i--) { fBaud = Convert.ToByte(i); if (fBaud == 3) { continue; } if (!_IsUseNet) { openresult = StaticClassReaderB.OpenComPort(port, ref fComAdr, fBaud, ref frmcomportindex); } else { LBErrorLog.InsertFileLog("OpenPort:" + _NetIPAddress + "_" + _NetPort + "_" + Rate); openresult = StaticClassReaderB.OpenNetPort(_NetPort, _NetIPAddress, ref fComAdr, ref frmcomportindex); } fOpenComIndex = frmcomportindex; if (openresult == 0x35) { LBErrorLog.InsertFileLog("网口已打开"); ComOpen = true; } if (openresult == 0) { ComOpen = true; byte[] TrType = new byte[2]; byte[] VersionInfo = new byte[2]; byte ReaderType = 0; byte ScanTime = 0; byte dmaxfre = 0; byte dminfre = 0; byte powerdBm = 0; int fCmdRet = StaticClassReaderB.GetReaderInformation(ref fComAdr, VersionInfo, ref ReaderType, TrType, ref dmaxfre, ref dminfre, ref powerdBm, ref ScanTime, frmcomportindex); if ((fCmdRet == 0x35) || (fCmdRet == 0x30)) { ComOpen = false; StaticClassReaderB.CloseSpecComPort(frmcomportindex); LBErrorLog.InsertFileLog("CloseSpecComPort串口通讯错误"); } fCmdRet = StaticClassReaderB.SetPowerDbm(ref fComAdr, powerDbm, frmcomportindex);//设置功率 break; } else if (_IsUseNet) { break; } } if ((fOpenComIndex == -1) && (openresult == 0x30)) { LBErrorLog.InsertFileLog("串口通讯错误"); } return(ComOpen); }