/// <summary> /// /// </summary> /// <param name="hostBacker"></param> /// <param name="strpath"></param> /// <param name="configpath"></param> /// <param name="memStation"></param> public StationUpdateTimer(HostBack.DataSave sqlSave,bool commType, string strpath, string configpath, MemStation[] memStation) { this.sqlSave = sqlSave; this._commType = commType; this.StationXmlFilePath = System.Windows.Forms.Application.StartupPath.ToString() + @"\" + strpath; this.StationConfigFilePath = System.Windows.Forms.Application.StartupPath.ToString() + @"\" + configpath; this.memStation = memStation; stationChangeTime.Interval = 5000; stationChangeTime.Tick += new EventHandler(stationChangeTime_Tick); }
public override void baseSocketClient_DataReceivedByAddress(byte[] bytes, string strAddress) { base.baseSocketClient_DataReceivedByAddress(bytes, strAddress); try { #region [ 缓冲区溢出 ] // 如果不存在数据 if (iCurLoc == 0) { iStartLoc = -1; iEndLoc = -1; iEndStartLoc = -1; cmdNewBytes = null; if (timer.Enabled == false) { timer.Enabled = true; } return; } #endregion #region [ 寻找开始标记 ] // 寻找开始标记 int i238; if (iStartLoc == -1) { i238 = 0; for (int i = 0; i < iCurLoc; i++) { if (byteBuffer[i] == 238) { i238++; } else { i238 = 0; } if (i238 >= 5) { iStartLoc = i - 5; iEndStartLoc = i + 1; break; } } } if (iEndStartLoc == -1) return; #endregion #region [获取分站信息] //分站地址号 int address = (int)byteBuffer[iStartLoc + 6]; MemStation mStation = new MemStation(); if (address != 0) { mStation = GetStation(address); } if (mStation.Address == 0) { iCurLoc = 0; for (int i = 0; i < byteBuffer.Length; i++) { byteBuffer[i] = (byte)0; } cmdNewBytes = null; if (timer.Enabled == false) { timer.Enabled = true; } return; } #endregion switch (mStation.StationModel) { case 1://KJ128A #region [ 寻找结束标记 ] // 寻找结束标记 int i254; if (iEndLoc == -1) { i254 = 0; int iTmp = 0; for (iTmp = iEndStartLoc; iTmp < iCurLoc; iTmp++) { if (byteBuffer[iTmp] == 254) { i254++; } else { i254 = 0; } if (i254 >= 3) { iEndLoc = iTmp; break; } } // 记录下一次开始寻找结束标志的位置 if (iEndLoc == -1) { iEndStartLoc = iTmp - 2; return; } } // Error: 写日志 if (iEndStartLoc == -1) return; #endregion #region [ 检索重复开始标志 ] // 检索开始标志后,结束标志前,是否还有第二个开始标志 i238 = 0; for (int i = iEndLoc - 2; i > iStartLoc + 4; i--) { if (byteBuffer[i] == 238) { i238++; } else { i238 = 0; } if (i238 >= 5) { // 提取重复标志位的数据 int iErrCmd = iEndLoc - iStartLoc + 1; byte[] cmdErr = new byte[iErrCmd]; for (int k = 0; k < iErrCmd; k++) { cmdErr[k] = byteBuffer[iStartLoc + k]; } // 显示重复标志位的数据 //if (RTxtMsge != null) //{ // RTxtMsge.WriteTxt(cmdErr, " [标志重复]", true, 0); //} iStartLoc = i - 1; break; } } #endregion #region [ 提取完整命令 ] // 根据命令长度,分配命令存放的空间 int iCmdNewLength = iEndLoc - iStartLoc - 9; if (iCmdNewLength < 1) { // Error: string str = string.Empty; return; } cmdNewBytes = new byte[iCmdNewLength]; // 提取完整命令 for (int i = 0; i < iCmdNewLength; i++) { cmdNewBytes[i] = byteBuffer[iStartLoc + i + 6]; } iCurLoc = 0; for (int i = 0; i < byteBuffer.Length; i++) { byteBuffer[i] = (byte)0; } //// 将后续命令前移 //if (iEndLoc + 1 < iCurLoc) //{ // iEndLoc++; // int iCmdTmpLength = iCurLoc - iEndLoc; // for (int i = 0; i <= iCmdTmpLength; i++) // { // byteBuffer[i] = byteBuffer[iEndLoc + i]; // } // iCurLoc = iCmdTmpLength; //} //else //{ // iCurLoc = 0; //} #endregion break; default: #region [ 提取完整命令 ] try { iCmdNewLength = 0; if (byteBuffer.Length > 9) { switch (byteBuffer[iStartLoc + 7]) { case 20://数据回送 iCmdNewLength = 6 + int.Parse(byteBuffer[iStartLoc + 8].ToString()) + int.Parse(byteBuffer[iStartLoc + 9].ToString()) * 256; if (iCmdNewLength <= 6) { cmdNewBytes = null; if (timer.Enabled == false) { timer.Enabled = true; } return; } if (byteBuffer.Length < iCmdNewLength + 6) { iCmdNewLength = 0; for (int i = 0; i < byteBuffer.Length; i++) { byteBuffer[i] = (byte)0; } } break; case 21://数据回送成功确认 case 23://对时成功确认 case 24://请求对时 iCmdNewLength = 4; break; default: for (int i = 0; i < byteBuffer.Length; i++) { byteBuffer[i] = (byte)0; } break; } } // 根据命令长度,分配命令存放的空间 if (iCmdNewLength < 1) { // Error: string str = string.Empty; cmdNewBytes = null; if (timer.Enabled == false) { timer.Enabled = true; } return; } cmdNewBytes = new byte[iCmdNewLength]; byte[] cmdBytesAll = new byte[iCmdNewLength + 5]; for (int i = 0; i < iCmdNewLength + 5; i++) { cmdBytesAll[i] = byteBuffer[iStartLoc + i + 1]; } // 提取完整命令 for (int i = 0; i < iCmdNewLength; i++) { cmdNewBytes[i] = byteBuffer[iStartLoc + i + 6]; } #region [分析效验位] _crcByte = CRCVerify.Crc(cmdBytesAll, iCmdNewLength + 3, 0); #endregion if (_crcByte == null) { cmdNewBytes = null; if (timer.Enabled == false) { timer.Enabled = true; } return; } if (cmdNewBytes[1] == 0) { cmdNewBytes = null; if (timer.Enabled == false) { timer.Enabled = true; } return; } //if (cmdNewBytes[cmdNewBytes.Length - 1] == 0 && cmdNewBytes[cmdNewBytes.Length - 2] == 0) //{ // cmdNewBytes = null; // if (timer.Enabled == false) // { // timer.Enabled = true; // } // return; //} // 验证命令号是否正确cmdNewBytes[1] != CmdNO if (cmdNewBytes[1] != mStation.SCmd && cmdNewBytes[1] != 24) { for (int i = 0; i < byteBuffer.Length; i++) { byteBuffer[i] = (byte)0; } cmdNewBytes = null; if (timer.Enabled == false) { timer.Enabled = true; } return; } // Crc 校验 if (!_crcByte[0].Equals(cmdNewBytes[cmdNewBytes.Length - 1]) || !_crcByte[1].Equals(cmdNewBytes[cmdNewBytes.Length - 2])) { cmdNewBytes = null; if (timer.Enabled == false) { timer.Enabled = true; } return; } iCurLoc = 0; for (int i = 0; i < byteBuffer.Length; i++) { byteBuffer[i] = (byte)0; } } catch { for (int i = 0; i < byteBuffer.Length; i++) { byteBuffer[i] = (byte)0; } cmdNewBytes = null; if (timer.Enabled == false) { timer.Enabled = true; } } #endregion break; } } catch { cmdNewBytes = null; if (timer.Enabled == false) { timer.Enabled = true; } } }
/// <summary> /// 分站信息改变 /// </summary> /// <param name="dgStation"></param> /// <param name="memStation"></param> /// <returns></returns> public bool StationChange(DataGridView[] dgStation, MemStation[] memStation) { if (m_TcpClientPort == null) return false; ArrayList al = new ArrayList(); // 根据串口巡检的基站组提取符合条件的基站 for (int j = 0; j < memStation.Length; j++) { if (1 == memStation[j].Group) { al.Add(memStation[j]); } } // 重新组织基站数据 MemStation[] tmpStation = new MemStation[al.Count]; MemStation[] oldStations = m_TcpClientPort.Stations; for (int k = 0; k < al.Count; k++) { tmpStation[k] = (MemStation)al[k]; for (int m = 0; m < oldStations.Length; m++) { if (oldStations[m].Address == tmpStation[k].Address && oldStations[m].ID == tmpStation[k].ID && oldStations[m].IpAddress.Equals(tmpStation[k].IpAddress) && oldStations[m].Port.Equals(tmpStation[k].Port) && oldStations[m].StationModel == tmpStation[k].StationModel) { tmpStation[k] = oldStations[m]; tmpStation[k].NoAnswer = 0; //tmpStation[k].State = oldStations[m].State; break; } } if (tmpStation[k].CmdVersion == null) { if (tmpStation[k].StationModel == 1) { // 初始化校对版本号命令 tmpStation[k].CmdVersion = KJ128A.Protocol.P20071210.Version(tmpStation[k].Address, 0, m_TcpClientPort.Mark); } } if (!IsHost) { tmpStation[k].State = -10000; } else { tmpStation[k].State = 0; } } // 更新串口中的基站数据 m_TcpClientPort.TempStations = tmpStation; m_TcpClientPort.IsStationChange = true; // 更新界面显示的基站数据 dgStation[0].Visible = false; dgStation[0].DataSource = m_TcpClientPort.TempStations; dgStation[0].Visible = true; al = null; return true; }
/// <summary> /// 初始化基站信息 /// </summary> /// <param name="dgStation"></param> /// <param name="memStation"></param> /// <returns></returns> public bool InitStation(DataGridView[] dgStation, MemStation[] memStation) { if (m_TcpClientPort == null) return false; ArrayList al = new ArrayList(); // 根据串口巡检的基站组提取符合条件的基站 for (int j = 0; j < memStation.Length; j++) { if (1 == memStation[j].Group) { al.Add(memStation[j]); } } // 重新组织基站数据 MemStation[] tmpStation = new MemStation[al.Count]; for (int k = 0; k < al.Count; k++) { tmpStation[k] = (MemStation)al[k]; // 初始化校对版本号命令 if (tmpStation[k].StationModel == 1) { tmpStation[k].CmdVersion = KJ128A.Protocol.P20071210.Version(tmpStation[k].Address, 0, m_TcpClientPort.Mark); } } // 更新串口中的基站数据 m_TcpClientPort.Stations = tmpStation; m_TcpClientPort.TempStations = tmpStation; m_TcpClientPort.IsStationChange = false; // 更新界面显示的基站数据 dgStation[0].Visible = false; dgStation[0].DataSource = m_TcpClientPort.Stations; dgStation[0].Visible = true; al = null; return true; }
/// <summary> /// 分站信息改变 /// </summary> static void stationUpdateTime_StationInfoChange() { DataTable dtStation = BuildStationTable(); try { //新的分站表数据 dtStation.ReadXml(strStationSavePath); int iStationCount = dtStation.Rows.Count; MemStation[] _memStationTemp = new MemStation[iStationCount]; //构建新的分站表数据 for (int i = 0; i < iStationCount; i++) { DataRow dr = dtStation.Rows[i]; _memStationTemp[i].ID = int.Parse(dr["ID"].ToString()); _memStationTemp[i].Address = int.Parse(dr["Address"].ToString()); _memStationTemp[i].Group = int.Parse(dr["Group"].ToString()); _memStationTemp[i].State = int.Parse(dr["State"].ToString()); _memStationTemp[i].Ver = int.Parse(dr["Ver"].ToString()); if (dr["IpAddress"] != null) { _memStationTemp[i].IpAddress = dr["IpAddress"].ToString(); } if (dr["IpPort"] != null) { _memStationTemp[i].Port = int.Parse(dr["IpPort"].ToString()); } _memStationTemp[i].StationModel = int.Parse(dr["StationModel"].ToString()); _memStationTemp[i].IsPointSelect = false; _memStationTemp[i].IsTwo = false; } //通知信息改变 if (!_commType) { _port.StationChange(dgStation, _memStationTemp); } else { _startTcp.StationChange(dgStation, _memStationTemp); } } catch { } finally { // 释放表格对象 dtStation.Dispose(); } }
/// <summary> /// 分站信息改变 /// </summary> /// <param name="dgStation"></param> /// <param name="memStation"></param> /// <returns></returns> public bool StationChange(DataGridView[] dgStation, MemStation[] memStation) { if (!(s_serialPort != null && s_serialPort.Length > 0)) return false; for (int i = 0; i < s_serialPort.Length; i++) { ArrayList al = new ArrayList(); // 根据串口巡检的基站组提取符合条件的基站 for (int j = 0; j < memStation.Length; j++) { if (s_serialPort[i].Group == memStation[j].Group) { al.Add(memStation[j]); } } // 重新组织基站数据 MemStation[] tmpStation = new MemStation[al.Count]; MemStation[] oldStations = s_serialPort[i].Stations; for (int k = 0; k < al.Count; k++) { tmpStation[k] = (MemStation)al[k]; if (oldStations != null) { for (int m = 0; m < oldStations.Length; m++) { if (oldStations[m].Address == tmpStation[k].Address && oldStations[m].ID == tmpStation[k].ID && oldStations[m].StationModel == tmpStation[k].StationModel) { tmpStation[k] = oldStations[m]; tmpStation[k].NoAnswer = 0; break; } } } if (tmpStation[k].CmdVersion == null) { if (tmpStation[k].StationModel == 1 || tmpStation[k].StationModel == 3) { // 初始化校对版本号命令 tmpStation[k].CmdVersion = KJ128A.Protocol.P20071210.Version(tmpStation[k].Address, 0, s_serialPort[i].Mark); } } if (!IsHost) { tmpStation[k].State = -10000; //if (tmpStation[k].State != -10000) //{ // tmpStation[k].State = 0; //} //else //{ //} } else { tmpStation[k].State = 0; } } // 更新串口中的基站数据 s_serialPort[i].TempStations = tmpStation; s_serialPort[i].IsStationChange = true; // 更新界面显示的基站数据 dgStation[i].Visible = false; dgStation[i].DataSource = s_serialPort[i].TempStations; dgStation[i].Visible = true; al = null; } return true; }
/// <summary> /// 初始化基站信息 /// </summary> /// <param name="dgStation"></param> /// <param name="memStation"></param> /// <returns></returns> public bool InitStation(DataGridView[] dgStation, MemStation[] memStation) { if (!(s_serialPort != null && s_serialPort.Length > 0)) return false; if (memStation == null || memStation.Length <= 0) return false; for (int i = 0; i < s_serialPort.Length; i++) { ArrayList al = new ArrayList(); // 根据串口巡检的基站组提取符合条件的基站 for (int j = 0; j < memStation.Length; j++) { if (s_serialPort[i].Group == memStation[j].Group) { al.Add(memStation[j]); } } // 重新组织基站数据 MemStation[] tmpStation = new MemStation[al.Count]; for (int k = 0; k < al.Count; k++) { tmpStation[k] = (MemStation)al[k]; if (tmpStation[k].StationModel == 1 || tmpStation[k].StationModel == 3) { // 初始化校对版本号命令 tmpStation[k].CmdVersion = KJ128A.Protocol.P20071210.Version(tmpStation[k].Address, 0, s_serialPort[i].Mark); } } // 更新串口中的基站数据 s_serialPort[i].Stations = tmpStation; s_serialPort[i].TempStations = tmpStation; s_serialPort[i].IsStationChange = false; // 更新界面显示的基站数据 dgStation[i].Visible = false; dgStation[i].DataSource = s_serialPort[i].TempStations; dgStation[i].Visible = true; al = null; } return true; }