public NetworkLayerProtocol() { sendFuc += _nF => { DataLinkLayer.ActionDllSendFuc(_nF); }; }
private static void SynchroSend(object sfc_object) { var sfc = sfc_object as ApplicationLayerProtocol; //字节小于7个 if (sfc.A_Data.Count <= 7) { SemaphorePool.Wait(); List <byte> sendData = new List <byte> { }; sendData.AddRange(NetworkLayerDataBuffer.SetSingleFrame(sfc.A_Data.Count)); sendData.AddRange(sfc.A_Data); DataLinkLayer.ActionDllSendFuc(ConvertToCANFrame(sfc.Address, sendData)); SemaphorePool.Release(); } else { BufferMultiFrameSend.ListNetLayerByteData = sfc.A_Data; BufferMultiFrameSend.nID = sfc.Address; #region 设置所有参数 重置参数 BufferMultiFrameSend.RemainFrameCount = 1; // 发送首帧 BufferMultiFrameSend.Offset = 0; BufferMultiFrameSend.ExpectedSN = 0; #endregion ThreadPool.QueueUserWorkItem(NetworkLayerSendMultiFrame); } }
public static NetworkLayerProtocol Parse(DataLinkLayer dataLinkData) { NetworkLayerProtocol networkData = new NetworkLayerProtocol(); networkData.N_AI = dataLinkData.Address; networkData.N_PCIType = (byte)(dataLinkData.ListDllData[0] & 0xF0); if (networkData.N_PCIType == 0x01) { networkData.SF_DL = (byte)(dataLinkData.ListDllData[0] & 0x0F); if ((networkData.SF_DL > 7) || (networkData.SF_DL == 0)) { throw new Exception("单帧长度错误"); } networkData.N_PCI = networkData.GetSFN_PCI(networkData.N_PCIType, networkData.SF_DL); dataLinkData.ListDllData.RemoveRange(0, 1); networkData.N_Data = dataLinkData.ListDllData; } else if (networkData.N_PCIType == 0x03) { networkData.FC_FS = (byte)(dataLinkData.ListDllData[0] & 0xF0); if (networkData.FC_FS == 3) { throw new Exception("Buffer 溢出"); } //networkData.N_PCI = networkData.GetFCN_PCI(); //dataLinkData.L_Data.RemoveRange(0, 3); //networkData.N_Data = dataLinkData.L_Data; } else { if (networkData.N_PCIType == 0x01) { networkData.FF_DL = networkData.GetFF_DL(dataLinkData.ListDllData[0], dataLinkData.ListDllData[1]); if ((networkData.FF_DL < 8) || (networkData.FF_DL > 0xFFF)) { throw new Exception("首帧长度错误"); } networkData.N_PCI = networkData.GetFFN_PCI(networkData.N_PCIType, networkData.FF_DL); dataLinkData.ListDllData.RemoveRange(0, 2); networkData.N_Data.AddRange(dataLinkData.ListDllData); } else { networkData.CF_SN = (byte)(dataLinkData.ListDllData[0] & 0xF0); if ((networkData.CF_SN > 15) || (networkData.CF_SN < 0)) { throw new Exception("连续帧序号错误"); } networkData.N_PCI = networkData.GetCFN_PCI(networkData.N_PCIType, dataLinkData.ListDllData[0]); dataLinkData.ListDllData.RemoveRange(0, 1); networkData.N_Data.AddRange(dataLinkData.ListDllData); } } return(networkData); }
public static void NetworkLayerSendMultiFrame(object obj) { string strSendBytes = DataFormatConvert.ListToStr(BufferMultiFrameSend.ListNetLayerByteData); int nSendTimes = 0; try { while (true) { if (BufferMultiFrameSend.Offset < BufferMultiFrameSend.ListNetLayerByteData.Count && BufferMultiFrameSend.RemainFrameCount > 0 && (MainWindow.m_OperateType == OperateType.WriteAllMasterPara || //ParaManageWnd.IsUpdateAllPara || MainWindow.m_OperateType == OperateType.WriteAllMasterAdjustPara || MainWindow.m_OperateType == OperateType.WriteMasterControlPara)) // (BufferMultiFrameSend.RemainFrameCount != 0) { if (BufferMultiFrameSend.Offset > 0) // 非首帧 { DataLinkLayer.ActionDllSendFuc(BufferMultiFrameSend.SendConsecutiveFrame()); } else { DataLinkLayer.ActionDllSendFuc(BufferMultiFrameSend.SendFirstFrame()); } BufferMultiFrameSend.RemainFrameCount--; } else { if (MainWindow.m_OperateType != OperateType.WriteAllMasterPara) //(!ParaManageWnd.IsUpdateAllPara) // 关闭参数管理窗口,停止设置参数 清空发送数据 { BufferMultiFrameSend.ClearMessage(); } break; } Thread.Sleep(200); } } catch (Exception ex) { } }
private void ReceiveFirstFrame(DataLayerEvent _event) { BufferMultiFrameReceive.nID = _event.ID; BufferMultiFrameReceive.DataLen = NetworkLayerDataBuffer.GetFF_DLSign(_event.listData); if (BufferMultiFrameReceive.DataLen < 8) { return; } int nDataIndex = 2; BufferMultiFrameReceive.ListNetLayerByteData.Clear(); BufferMultiFrameReceive.ListNetLayerByteData = new List <byte>(new byte[BufferMultiFrameReceive.DataLen]); for (int i = nDataIndex; i != 8; ++i) { BufferMultiFrameReceive.ListNetLayerByteData[(int)(i - nDataIndex)] = _event.listData[i]; } int nCount = 0; if ((BufferMultiFrameReceive.DataLen - 6) / 7 == 0) { nCount = (BufferMultiFrameReceive.DataLen - 6) / 7; } else { nCount = (BufferMultiFrameReceive.DataLen - 6) / 7 + 1; } BufferMultiFrameReceive.Offset = 6; BufferMultiFrameReceive.ProCtrlType = ProtocolCtrlType.FlowControl; BufferMultiFrameReceive.RemainFrameCount = nCount; BufferMultiFrameReceive.ExpectedSN = 1; DataLinkLayer.ActionDllSendFuc(BufferMultiFrameReceive.SendFlowControl()); }
public static void _Request(MessageBuffer m_messageBuffer) { byte[] btArrRequest = new byte[8]; //vbyPDU.Capacity=8; byte byPCIFirst; byPCIFirst = (byte)(((byte)m_messageBuffer.ProCtrlType) << 4); int nDataSize = m_messageBuffer.ListByteData.Count; int nPDUDataSize = 7; // PDU 包含的数据长度,在首帧中需要减一,流控制帧中不包含数据帧。 switch (m_messageBuffer.ProCtrlType) { case ProtocolCtrlType.SingleFrame: if (nDataSize > nPDUDataSize) { nDataSize = nPDUDataSize; } byPCIFirst = (byte)(byPCIFirst | nDataSize); btArrRequest[0] = byPCIFirst; for (int n = 1; n < btArrRequest.Length; n++) { btArrRequest[n] = m_messageBuffer.ListByteData[n - 1]; } //vbyPDU.AddRange(m_messageBuffer.vbyData); break; case ProtocolCtrlType.FirstFrame: if (nDataSize > 0xFFF) { nDataSize = 0xFFF; } --nPDUDataSize; byPCIFirst = (byte)(byPCIFirst | nDataSize >> 8); btArrRequest[0] = byPCIFirst; btArrRequest[0] = (byte)(nDataSize & 0xFF); byte[] data = new byte[nPDUDataSize]; m_messageBuffer.ListByteData.CopyTo(0, data, 0, data.Length); for (int n = 1; n < data.Length; n++) { btArrRequest[n] = data[n - 1]; } m_messageBuffer.StartLocation = (uint)nPDUDataSize; m_messageBuffer.RemainderFrameCount = 0; m_messageBuffer.ExpectedSequenceNumber = 1; break; case ProtocolCtrlType.ConsecutiveFrame: if (m_messageBuffer.ListByteData.Count() <= m_messageBuffer.StartLocation) { return; } byPCIFirst = (byte)(byPCIFirst | m_messageBuffer.ExpectedSequenceNumber); m_messageBuffer.ExpectedSequenceNumber = (byte)((m_messageBuffer.ExpectedSequenceNumber + 1) % 0x10); btArrRequest[0] = byPCIFirst; nPDUDataSize = (int)System.Math.Min(nPDUDataSize, m_messageBuffer.ListByteData.Count() - m_messageBuffer.StartLocation); for (int n = 1; n < m_messageBuffer.ListByteData.Count; n++) { btArrRequest[n] = m_messageBuffer.ListByteData[n - 1]; } m_messageBuffer.StartLocation += (uint)nPDUDataSize; --m_messageBuffer.RemainderFrameCount; break; case ProtocolCtrlType.FlowControl: //m_byBlockSize=0xFF; // 暂时仅发送 ContinueToSend。 byPCIFirst = (byte)(byPCIFirst | 0x0); btArrRequest[0] = byPCIFirst; btArrRequest[1] = 0xFF; btArrRequest[2] = 0x64; break; } DataLinkLayer.SendCanFrame(m_messageBuffer.nID, btArrRequest); }