/// <summary> /// Hàm được gọi khi có sự kiện dữ liệu tồn tại trên cổng COM của thiết bị /// </summary> /// <param name="obj"></param> public override void OnInCommingData(object obj) { try { // Chuyển kiểu dữ liệu về dạng DataReceiveRs232 do lớp Base Devices chỉ gửi Object var rawData = (DataReceiveRs232)obj; if (rawData != null) { // Nếu chưa gán Port thì gán SetNetStream(rawData.Port); byte[] allbyte = rawData.Data; _tempbuffer = string.Concat(_tempbuffer, Encoding.ASCII.GetString(allbyte)); for (int i = 0; i < allbyte.Length; ++i) { byte onebyte = allbyte[i]; if (onebyte.Equals((byte)DeviceHelper.ENQ)) { Log.Debug(@"Centaur: <ENQ>"); DumpStateMachine(objWorkFlowRuntime, InstanceId); objASTM.CallGetENQ(objDataEventArgs); //SendByte((byte)DeviceHelper.ACK); //RaiseEventSendENQToWF(null, onebyte); BufferData = _tempbuffer = string.Empty; _timeoutManager.Start(); } else if (onebyte.Equals((byte)DeviceHelper.LF)) { Log.Debug("Centaur: {0}", _tempbuffer); _timeoutManager.Stop(); string temp = DeviceHelper.GetStringAfterCheckSum(_tempbuffer); _tempbuffer = string.Empty; if (temp != string.Empty) { //SendStringData(DeviceHelper.ACK.ToString()); objASTM.CallGetRightFrame(objDataEventArgs); BufferData = string.Concat(BufferData, temp); } else { //SendByte((byte)DeviceHelper.NAK); objASTM.CallGetWrongFrame(objDataEventArgs); } _timeoutManager.Start(); } else if (onebyte.Equals((byte)DeviceHelper.EOT)) { Log.Debug(@"Centaur: <EOT>"); objASTM.CallGetEOT(objDataEventArgs); _timeoutManager.Stop(); ProcessData(); //Send((int)EventID.GetEOT,_newQuery); } else if (onebyte.Equals((byte)DeviceHelper.ACK)) { //Send((int) EventID.GetACK); Log.Debug(@"Centaur: <ACK>"); DumpStateMachine(objWorkFlowRuntime, InstanceId); _timeoutManager.Stop(); objASTM.CallGetACK(objDataEventArgs); if (((PrvRequestArray != null) && (PrvRequestArray.Count != 0)) && (_lastState == 0)) { _lastState++; prvLastStringRequest = PrvRequestArray.Dequeue(); Log.Debug("Send String Data: {0}", prvLastStringRequest); SendStringData(prvLastStringRequest); _timeoutManager.Start(); } else { //SendByte((byte)DeviceHelper.EOT); _timeoutManager.Stop(); Log.Debug(@"Session closing ..."); objASTM.CallCloseSession(objDataEventArgs); if (PrvRequestArray != null && PrvRequestArray.Count != 0) { objASTM.CallGetQuery(objDataEventArgs); _lastState = 0; _timeoutManager.Start(); } } } else if (onebyte.Equals((byte)DeviceHelper.NAK)) { _timeoutManager.Stop(); Log.Debug(@"Centaur: <NAK>"); if (++_failSending < 5) { if (_lastState > 0) { objASTM.CallGetNAK(objDataEventArgs); SendStringData(prvLastStringRequest); } else { Log.Debug(@"Get NAK. Session closing ..."); objASTM.CallCloseSession(objDataEventArgs); } _timeoutManager.Start(); } else { Log.Error("Too much NAK! Session closed!"); objASTM.CallCloseSession(objDataEventArgs); } } } } } catch (Exception ex) { Log.FatalException("Fatal Error: ", ex); } }
/// <summary> /// Xử lý sau kkhi nhận được dữ liệu /// </summary> public override void ProcessRawData() { try { byte[] allbyte = ByteData; _tempbuffer = string.Concat(_tempbuffer, Encoding.ASCII.GetString(allbyte)); foreach (byte onebyte in allbyte) { if (onebyte.Equals((byte)DeviceHelper.ENQ)) { Log.Debug(@"Centaur: <ENQ>"); objASTM.CallGetENQ(objDataEventArgs); DumpStateMachine(objWorkFlowRuntime, InstanceId); //SendByte((byte)DeviceHelper.ACK); //RaiseEventSendENQToWF(null, onebyte); BufferData = _tempbuffer = string.Empty; } else if (onebyte.Equals((byte)DeviceHelper.LF)) { Log.Debug("Centaur: {0}", _tempbuffer); string temp = DeviceHelper.GetStringAfterCheckSum(_tempbuffer); _tempbuffer = string.Empty; if (temp != string.Empty) { //SendStringData(DeviceHelper.ACK.ToString()); objASTM.CallGetRightFrame(objDataEventArgs); BufferData = string.Concat(BufferData, temp); } else { //SendByte((byte)DeviceHelper.NAK); objASTM.CallGetWrongFrame(objDataEventArgs); } } else if (onebyte.Equals((byte)DeviceHelper.EOT)) { Log.Debug(@"Centaur: <EOT>"); objASTM.CallGetEOT(objDataEventArgs); ProcessData(); //Send((int)EventID.GetEOT,_newQuery); } else if (onebyte.Equals((byte)DeviceHelper.ACK)) { //Send((int) EventID.GetACK); Log.Debug(@"Centaur: <ACK>"); objASTM.CallGetACK(objDataEventArgs); if ((currentOrder != null) && (currentOrder.Count != 0) && (currentOrderIdx < currentOrder.Count)) { prvLastStringRequest = currentOrder[currentOrderIdx]; currentOrderIdx++; SendStringData(prvLastStringRequest); Log.Debug("Host: {0}", prvLastStringRequest); } else { //SendByte((byte)DeviceHelper.EOT); Log.Debug(@"Session closing ..."); objASTM.CallCloseSession(objDataEventArgs); if (PrvRequestArray != null && PrvRequestArray.Count != 0) { objASTM.CallGetQuery(objDataEventArgs); } } } else if (onebyte.Equals((byte)DeviceHelper.NAK)) { Log.Debug(@"Centaur: <NAK>"); objASTM.CallGetNAK(objDataEventArgs); SendStringData(prvLastStringRequest); } } } catch (Exception ex) { Log.FatalException("Fatal Error: ", ex); } finally { ClearData(); } }
void _objAstm_ACKTimeoutEvent(object sender, EventArgs e) { Log.Debug(@"Timeout expired ..."); Log.Debug(@"Session closing ..."); _objAstm.CallCloseSession(_objDataEventArgs); }
public override void ProcessRawData() { try { byte[] allbyte = ByteData; Log.Trace(allbyte); _tempbuffer = string.Concat(_tempbuffer, Encoding.ASCII.GetString(allbyte)); for (int i = 0; i < allbyte.Length; ++i) { byte onebyte = allbyte[i]; if (onebyte.Equals((byte)DeviceHelper.ENQ)) { objASTM.CallGetENQ(objDataEventArgs); //DumpStateMachine(objWorkFlowRuntime, InstanceId); //SendByte((byte)DeviceHelper.ACK); //RaiseEventSendENQToWF(null, onebyte); BufferData = _tempbuffer = string.Empty; } else if (onebyte.Equals((byte)DeviceHelper.LF)) { string temp = DeviceHelper.GetStringAfterCheckSum(_tempbuffer); Log.Trace(temp); _tempbuffer = string.Empty; if (temp != string.Empty) { //SendStringData(DeviceHelper.ACK.ToString()); objASTM.CallGetRightFrame(objDataEventArgs); BufferData = BufferData.StartsWith(@"R") ? string.Format("{0}{1}{2}", BufferData, DeviceHelper.CR, temp) : string.Concat(BufferData, temp); } else { //SendByte((byte)DeviceHelper.NAK); objASTM.CallGetWrongFrame(objDataEventArgs); } } else if (onebyte.Equals((byte)DeviceHelper.EOT)) { objASTM.CallGetEOT(objDataEventArgs); ProcessData(); //Send((int)EventID.GetEOT,_newQuery); } else if (onebyte.Equals((byte)DeviceHelper.ACK)) { //Send((int) EventID.GetACK); objASTM.CallGetACK(objDataEventArgs); if ((PrvRequestArray != null) && (PrvRequestArray.Count != 0)) { prvLastStringRequest = PrvRequestArray[0]; PrvRequestArray.RemoveAt(0); SendStringData(prvLastStringRequest); } else { //SendByte((byte)DeviceHelper.EOT); objASTM.CallCloseSession(objDataEventArgs); } } else if (onebyte.Equals((byte)DeviceHelper.NAK)) { //Send((int)EventID.GetNAK); objASTM.CallGetNAK(objDataEventArgs); SendStringData(prvLastStringRequest); } } } catch (Exception ex) { Log.Error(String.Format("Error While Process Raw Data: {0}", ex)); } finally { ClearData(); } }