/// <summary> /// 板卡专用工作回调 /// [注:读写专用变量值] /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _bkgrdwkForSpecialBaseData_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { uint[] uintData; ushort[] ushortData; short[] shortData; int[] intData; float[] floatData; byte[] byteData; ushort address = 0, num = 1; int lpcnt = 0; ProDriver.Auxiliary.HZZHComDataType dt; System.DateTime startTime = System.DateTime.Now; while (_netWorkState) { try { if (_netConnected) { //通用访问数据单元为空且专用数据单元非空 if ((_netQueue.Count == 0) && (_specialBaseDataList.Count > 0)) { address = _specialBaseDataList[lpcnt].Address; num = _specialBaseDataList[lpcnt].RegisterNumber; dt = _specialBaseDataList[lpcnt].DataType; switch (dt) { case ProDriver.Auxiliary.HZZHComDataType.Short: _comNet.Read(1, address, num, out shortData); System.Buffer.BlockCopy(shortData, 0, _specialBaseDataList[lpcnt].ShortValue, 0, shortData.Length * 2); break; case ProDriver.Auxiliary.HZZHComDataType.Uint: _comNet.Read(1, address, num, out uintData); System.Buffer.BlockCopy(uintData, 0, _specialBaseDataList[lpcnt].UintValue, 0, uintData.Length * 4); break; case ProDriver.Auxiliary.HZZHComDataType.Ushort: _comNet.Read(1, address, num, out ushortData); System.Buffer.BlockCopy(ushortData, 0, _specialBaseDataList[lpcnt].UshortValue, 0, ushortData.Length * 2); break; case ProDriver.Auxiliary.HZZHComDataType.Float: _comNet.Read(1, address, num, out floatData); System.Buffer.BlockCopy(floatData, 0, _specialBaseDataList[lpcnt].FloatValue, 0, floatData.Length * 4); break; case ProDriver.Auxiliary.HZZHComDataType.Byte: _comNet.Read(1, address, num, out byteData); byte[] btArr = new byte[byteData.Length]; for (int i = 0; i < byteData.Length; i++) { //高低位互换 if (i % 2 != 0) { btArr[i - 1] = byteData[i]; btArr[i] = byteData[i - 1]; } } System.Buffer.BlockCopy(btArr, 0, _specialBaseDataList[lpcnt].ByteValue, 0, byteData.Length); break; case ProDriver.Auxiliary.HZZHComDataType.Int: default: _comNet.Read(1, address, num, out intData); System.Buffer.BlockCopy(intData, 0, _specialBaseDataList[lpcnt].IntValue, 0, intData.Length * 4); break; } lpcnt++; if (lpcnt >= _specialBaseDataList.Count) { System.Threading.Thread.Sleep(2); System.TimeSpan elapse = System.DateTime.Now.Subtract(startTime); SpecailBaseDataElapse = elapse.TotalMilliseconds; lpcnt = 0; startTime = System.DateTime.Now; } } } } catch { _netWorkState = false; _netConnected = false; } System.Threading.Thread.Sleep(10); } }
/// <summary> /// 被动读取线程实体 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ReadWork(Object sender, DoWorkEventArgs e) { ushort addr, num; int readLoopCnt = 0; uint[] uintData; ushort[] ushortData; int[] intData; float[] floatData; short[] shortData; byte[] byteData; while (netSucceed) { try { if (ReadData.Count > 0) { addr = ReadData[readLoopCnt].Address; num = ReadData[readLoopCnt].RegisterNum; DataType dt = ReadData[readLoopCnt].DataTypes; switch (dt) { case DataType.Int: hzaux.Read(1, addr, num, out intData); Buffer.BlockCopy(intData, 0, ReadData[readLoopCnt].IntValue, 0, intData.Length * 4); break; case DataType.Float: hzaux.Read(1, addr, num, out floatData); Buffer.BlockCopy(floatData, 0, ReadData[readLoopCnt].FloatValue, 0, floatData.Length * 4); break; case DataType.Uint: hzaux.Read(1, addr, num, out uintData); Buffer.BlockCopy(uintData, 0, ReadData[readLoopCnt].UintValue, 0, uintData.Length * 4); break; case DataType.Ushort: hzaux.Read(1, addr, num, out ushortData); Buffer.BlockCopy(ushortData, 0, ReadData[readLoopCnt].UshortValue, 0, ushortData.Length * 2); break; case DataType.Short: hzaux.Read(1, addr, num, out shortData); Buffer.BlockCopy(shortData, 0, ReadData[readLoopCnt].ShortValue, 0, shortData.Length * 2); break; case DataType.Byte: hzaux.Read(1, addr, num, out byteData); byte[] bt = new byte[byteData.Length]; for (int i = 0; i < byteData.Length; i++) { if (i % 2 != 0) { bt[i - 1] = byteData[i]; bt[i] = byteData[i - 1]; } } Buffer.BlockCopy(bt, 0, ReadData[readLoopCnt].ByteValue, 0, bt.Length); break; default: break; } try { DiDoStatus.CurrInputStatus = InputStatus.IntValue; DiDoStatus.CurrOutputStatus = OutputStatus.IntValue; } catch { } readLoopCnt++; if (readLoopCnt >= ReadData.Count) { readLoopCnt = 0; } } } catch (Exception ex) { netSucceed = false; autoResetEvent.Set(); LogWriter.WriteLog("控制器掉线:被动读取 " + ex.ToString()); if (!backworkerConnect.IsBusy) { backworkerConnect.RunWorkerAsync(); } } readResetEvent.WaitOne(); } }