private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { int LastCount = serialPort1.BytesToRead; DateTime N = DateTime.Now; do { LastCount = serialPort1.BytesToRead; Thread.Sleep(100); }while (serialPort1.IsOpen && (serialPort1.BytesToRead != LastCount || serialPort1.BytesToRead < System.Runtime.InteropServices.Marshal.SizeOf(typeof(SerialResult))) && (N - DateTime.Now).TotalMilliseconds < 500); if (!serialPort1.IsOpen) { return; } if (serialPort1.BytesToRead < System.Runtime.InteropServices.Marshal.SizeOf(typeof(SerialResult))) { return; } LastCount = serialPort1.BytesToRead; byte[] Buf = new byte[LastCount]; serialPort1.Read(Buf, 0, LastCount); if (cbxRAW.Checked) { AddToLog(Encoding.Default.GetString(Buf)); } else { byte[] AdditionalData; SerialResult SR = FillResult(Buf, out AdditionalData); AddToLog(string.Format("Message: {0}, Cmd: {1}, Pipe: {2}, DataSize: {3}, AdditionalData: {4}", ((Results)SR.result).ToString("F"), ((Commands)SR.cmd).ToString("F"), SR.Pipe, SR.DataSize, Encoding.Default.GetString(AdditionalData))); } }
public bool SaveSerialBook(string strUserName, SerialBook oBook, DateTime dtStart, DateTime dtEnd, ref string strResponse) { bool result = false; if (oBook == null) { strResponse = "Dữ liệu không phù hợp"; } else { if (IsSerialBookResultExisted(oBook.Customer_Id, oBook.Book_Number, dtStart, dtEnd)) { strResponse = string.Format("Số sổ book_number {0} của khách hàng {1} đã tồn tại!", oBook.Customer_Id, oBook.Book_Number); log.Info(strResponse); } else { try { string strSerials = string.Empty; foreach (string str in oBook.Masoduthuongs) { strSerials += str + "|"; } int nSeq = 0; using (Promotion.DataModel.LuckySerial_BO objLuckySerial_BO = new LuckySerial_BO()) { List <Promotion.DataModel.LuckySerial> lstData = objLuckySerial_BO.GetByInputTime(dtStart, dtEnd).ToList(); if (lstData != null && lstData.Count > 0) { nSeq = lstData.Count; } } nSeq++; using (Promotion.DataModel.SerialResult_BO objSerialResult_BO = new SerialResult_BO()) { Promotion.DataModel.SerialResult objSerialResult = new SerialResult(); objSerialResult.book_number = oBook.Book_Number; objSerialResult.customer_id = oBook.Customer_Id; objSerialResult.customer_name = oBook.Customer_Name; objSerialResult.CMND = oBook.CMTND; objSerialResult.tel = oBook.Telephone; objSerialResult.amount = oBook.TotalAmount; objSerialResult.kyhan = oBook.Period; objSerialResult.currency = oBook.Currency; objSerialResult.Serials = strSerials; objSerialResult.Input_time = DateTime.Now; objSerialResult.SavedBy = strUserName; objSerialResult.EFFECT_DATE = oBook.ValueDate; objSerialResult.MATURITY_DATE = oBook.MaturityDate; objSerialResult.Sector = oBook.Sector; objSerialResult.ACCT_NUM = oBook.ACCOUNT_NUMBER; objSerialResult.CO_CODE = oBook.CO_CODE; objSerialResult.TERM = oBook.TERM; objSerialResult.Seq = nSeq; objSerialResult.DAO_CODE = oBook.DAO_CODE; objSerialResult.DEPT_CODE = oBook.DEPT_CODE; //objSerialResult.cMoney = oBook.TERM; objSerialResult_BO.CreateSerialResult(objSerialResult); } log.Info(strSerials); result = true; } catch (Exception ex) { log.Fatal(ex); } } } return(result); }