示例#1
0
        /// <summary>
        /// 板卡通用工作回调
        /// [注:读写通用变量值]
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _bkgrdwkForGeneralBaseData_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            uint[]   uintData;
            ushort[] ushortData;
            short[]  shortData;
            int[]    intData;
            float[]  floatData;
            byte[]   byteData;
            bool     queueState;

            //板卡工作状态
            while (_netWorkState)
            {
                try
                {
                    //板卡连接状态
                    if (_netConnected)
                    {
                        //数据单元队列非空(并发队列)
                        if (_netQueue.Count > 0)
                        {
                            ProDriver.Auxiliary.HZZHComBaseData bsData;
                            queueState = _netQueue.TryPeek(out bsData);
                            //获取队列元素成功
                            if (queueState)
                            {
                                //数据单元读取状态
                                if (bsData.InitiativeRead)
                                {
                                    try
                                    {
                                        switch (bsData.DataType)
                                        {
                                        case ProDriver.Auxiliary.HZZHComDataType.Short:
                                            _comNet.Read(1, bsData.Address, bsData.RegisterNumber, out shortData);
                                            bsData.ShortValue = (short[])shortData.Clone();
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Uint:
                                            _comNet.Read(1, bsData.Address, bsData.RegisterNumber, out uintData);
                                            bsData.UintValue  = (uint[])uintData.Clone();
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Ushort:
                                            _comNet.Read(1, bsData.Address, bsData.RegisterNumber, out ushortData);
                                            bsData.UshortValue = (ushort[])ushortData.Clone();
                                            bsData.ComSucceed  = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Float:
                                            _comNet.Read(1, bsData.Address, bsData.RegisterNumber, out floatData);
                                            bsData.FloatValue = (float[])floatData.Clone();
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Byte:
                                            _comNet.Read(1, bsData.Address, bsData.RegisterNumber, 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];
                                                }
                                            }

                                            bsData.ByteValue  = (byte[])btArr.Clone();
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Int:
                                        default:
                                            _comNet.Read(1, bsData.Address, bsData.RegisterNumber, out intData);
                                            bsData.IntValue   = (int[])intData.Clone();
                                            bsData.ComSucceed = true;
                                            break;
                                        }
                                    }
                                    catch (System.Exception ex)
                                    { throw new Exception("ReadError:" + ex.Message); }
                                }
                                else //数据单元写入状态
                                {
                                    try
                                    {
                                        switch (bsData.DataType)
                                        {
                                        case ProDriver.Auxiliary.HZZHComDataType.Int:
                                            _comNet.Write(1, bsData.Address, bsData.IntValue);
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Short:
                                            _comNet.Write(1, bsData.Address, bsData.ShortValue);
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Uint:
                                            _comNet.Write(1, bsData.Address, bsData.UintValue);
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Ushort:
                                            _comNet.Write(1, bsData.Address, bsData.UshortValue);
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Float:
                                            _comNet.Write(1, bsData.Address, bsData.FloatValue);
                                            bsData.ComSucceed = true;
                                            break;

                                        case ProDriver.Auxiliary.HZZHComDataType.Byte:
                                            _comNet.Write(1, bsData.Address, bsData.ByteValue);
                                            bsData.ComSucceed = true;
                                            break;

                                        default: break;
                                        }
                                    }
                                    catch (System.Exception ex)
                                    { throw new Exception("WriteError:" + ex.Message); }
                                }
                            }
                            else
                            {
                                throw new Exception("Obtain base data failed !");
                            }
                            queueState = _netQueue.TryDequeue(out bsData);
                        }
                    }
                }
                catch
                {
                    _netWorkState = false;
                    _netConnected = false;
                }

                System.Threading.Thread.Sleep(10);
            }
        }
        /// <summary>
        /// 写数据线程实体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WriteWork(Object sender, DoWorkEventArgs e)
        {
            uint[]   uintData;
            ushort[] ushortData;
            int[]    intData;
            float[]  floatData;
            short[]  shortData;
            byte[]   byteData;
            bool     queueState;

            while (Succeed)
            {
                try
                {
                    if (NetQueue.Count > 0)
                    {
                        BaseData _var;
                        queueState = NetQueue.TryPeek(out _var);
                        if (queueState)
                        {
                            if (!_var.ReadHand)
                            {
                                try
                                {
                                    switch (_var.DataTypes)
                                    {
                                    case DataType.Int:
                                        hzaux.Write(1, _var.Address, _var.IntValue);
                                        _var.Succeed = true;
                                        break;

                                    case DataType.Float:
                                        hzaux.Write(1, _var.Address, _var.FloatValue);
                                        _var.Succeed = true;
                                        break;

                                    case DataType.Uint:
                                        hzaux.Write(1, _var.Address, _var.UintValue);
                                        _var.Succeed = true;
                                        break;

                                    case DataType.Ushort:
                                        hzaux.Write(1, _var.Address, _var.UshortValue);
                                        _var.Succeed = true;
                                        break;

                                    case DataType.Short:
                                        hzaux.Write(1, _var.Address, _var.ShortValue);
                                        _var.Succeed = true;
                                        break;

                                    case DataType.Byte:
                                        hzaux.Write(1, _var.Address, _var.ByteValue);
                                        _var.Succeed = true;
                                        break;

                                    case DataType.Object:
                                        hzaux.Write(1, _var.Address, _var.ObjectValue);
                                        _var.Succeed = true;
                                        break;

                                    default:
                                        break;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    throw new Exception("写数据失败:" + ex.Message);
                                }
                            }
                            else
                            {
                                try
                                {
                                    switch (_var.DataTypes)
                                    {
                                    case DataType.Int:
                                        hzaux.Read(1, _var.Address, _var.RegisterNum, out intData);
                                        _var.IntValue = (int[])intData.Clone();
                                        _var.Succeed  = true;
                                        break;

                                    case DataType.Float:
                                        hzaux.Read(1, _var.Address, _var.RegisterNum, out floatData);
                                        _var.FloatValue = (float[])floatData.Clone();
                                        _var.Succeed    = true;
                                        break;

                                    case DataType.Uint:
                                        hzaux.Read(1, _var.Address, _var.RegisterNum, out uintData);
                                        _var.UintValue = (uint[])uintData.Clone();
                                        _var.Succeed   = true;
                                        break;

                                    case DataType.Ushort:
                                        hzaux.Read(1, _var.Address, _var.RegisterNum, out ushortData);
                                        _var.UshortValue = (ushort[])ushortData.Clone();
                                        _var.Succeed     = true;
                                        break;

                                    case DataType.Short:
                                        hzaux.Read(1, _var.Address, _var.RegisterNum, out shortData);
                                        _var.ShortValue = (short[])shortData.Clone();
                                        _var.Succeed    = true;
                                        break;

                                    case DataType.Byte:
                                        hzaux.Read(1, _var.Address, _var.RegisterNum, 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];
                                            }
                                        }
                                        _var.ByteValue = (byte[])bt.Clone();
                                        _var.Succeed   = true;
                                        break;

                                    default:
                                        break;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    throw new Exception("主动读取数据失败:" + ex.Message);
                                }
                            }
                            queueState = NetQueue.TryDequeue(out _var);
                        }
                        else
                        {
                            throw new Exception("取数据不成功");
                        }
                    }
                    else
                    {
                        readResetEvent.Set();
                        autoResetEvent.WaitOne();
                    }
                }
                catch (Exception ex)
                {
                    netSucceed = false;
                    readResetEvent.Set();
                    LogWriter.WriteLog("控制器掉线:" + ex.ToString());
                    if (!backworkerConnect.IsBusy)
                    {
                        backworkerConnect.RunWorkerAsync();
                    }
                }
            }
        }