Exemplo n.º 1
0
        public void AsyncStart()
        {
            try
            {
                CancellationToken token = tokenSource.Token;
                string            Addr  = this._Addr;
                string            IP    = this._IP;
                if (!_PLCDriver.IsConnect)
                {
                    logNet.RecordMessage(HslMessageDegree.INFO, "创建连接中-----线程" + ThreadCount, DateTime.Now.ToString());
                    bool isopen = _PLCDriver.Init(Addr, IP);
                    isFirst = false;
                    if (!isopen)
                    {
                        MessageEventHandler(_dataTyte.ToString() + "建立连接失败" + DateTime.Now + "\r\n", null);
                        return;
                    }
                    LogMessage = LogMessage + _dataTyte.ToString() + "建立连接结果" + isopen.ToString() + "------" + DateTime.Now + "\r\n";
                    logNet.RecordMessage(HslMessageDegree.INFO, "创建连接完成线程" + ThreadCount, DateTime.Now.ToString());

                    //  _hda.Add(new HistoryData(during, _DataLength, check, DateTime.Now)); // UpdateUIDelegate(LogMessage);
                }

                bool        check  = false;
                string      send   = "";
                string      recive = "";
                int         length = 0;
                int         ret    = -1;
                IEnumerable sendBytes;
                task = new Task(() =>
                {
                    while (true)
                    {
                        if (token.IsCancellationRequested)
                        {
                            _hda.Clear();
                            _PLCDriver.Dispose();
                            return;
                            //throw new OperationCanceledException(token);
                        }

                        try
                        {
                            Thread.Sleep(10);
                            switch (this._dataTyte)
                            {
                            case comm.DataTyte.BOOL:

                                break;

                            case comm.DataTyte.WORD:
                                length    = this._DataLength / 2;
                                sendBytes = GetRandomUshort(length);
                                ret       = _PLCDriver.WriteUInt16s(sendBytes as UInt16[]);
                                if (ret != 0)
                                {
                                    SendError(_PLCDriver.ErrorCode);
                                }
                                ret = _PLCDriver.ReadUInt16s(length);
                                if (ret != 0)
                                {
                                    SendError(_PLCDriver.ErrorCode);
                                }
                                check = Check(sendBytes, _PLCDriver.ReadBuff);
                                break;

                            case comm.DataTyte.INT16:
                                length    = this._DataLength / 2;
                                sendBytes = GetRandomInt16(length);
                                ret       = _PLCDriver.WriteInt16s(sendBytes as Int16[]);
                                if (ret != 0)
                                {
                                    SendError(_PLCDriver.ErrorCode);
                                }
                                ret = _PLCDriver.ReadInt16s(length);
                                if (ret != 0)
                                {
                                    SendError(_PLCDriver.ErrorCode);
                                }
                                check = Check(sendBytes, _PLCDriver.ReadBuff);


                                break;

                            case comm.DataTyte.INT32:
                                length    = this._DataLength / 4;
                                sendBytes = GetRandomInt32(length);
                                ret       = _PLCDriver.WriteInt32s(sendBytes as Int32[]);
                                if (ret != 0)
                                {
                                    SendError(_PLCDriver.ErrorCode);
                                }
                                ret = _PLCDriver.ReadInt32s(length);
                                if (ret != 0)
                                {
                                    SendError(_PLCDriver.ErrorCode);
                                }
                                check = Check(sendBytes, _PLCDriver.ReadBuff);
                                break;

                            case comm.DataTyte.REAL:
                                length    = this._DataLength / 4;
                                sendBytes = GetRandomfloat(length);
                                ret       = _PLCDriver.WriteFloats(sendBytes as float[]);
                                if (ret != 0)
                                {
                                    SendError(_PLCDriver.ErrorCode);
                                }
                                ret = _PLCDriver.ReadFloats(length);
                                if (ret != 0)
                                {
                                    SendError(_PLCDriver.ErrorCode);
                                }
                                check = Check(sendBytes, _PLCDriver.ReadBuff);

                                break;

                            case comm.DataTyte.STRING:
                                length = this._DataLength * 2;
                                send   = GetRandomString(length);
                                ret    = _PLCDriver.WriteBytes(SoftBasic.HexStringToBytes(send));
                                if (ret != 0)
                                {
                                    SendError(_PLCDriver.ErrorCode);
                                }
                                _PLCDriver.ReadBytes(length / 2);
                                byte[] ReciveData = _PLCDriver.ReadBuff as byte[];
                                if (ReciveData == null)
                                {
                                    SendError(_PLCDriver.ErrorCode);
                                }
                                recive = SoftBasic.ByteToHexString(ReciveData);
                                check  = send.Equals(recive);

                                break;

                            default:
                                break;
                            }

                            double during = (_PLCDriver.Readtime - _PLCDriver.Sendtime).TotalMilliseconds;
                            //logNet2.RecordMessage(HslMessageDegree.DEBUG, null, "耗时" + (_LIBnodavePLC.Readtime - _LIBnodavePLC.Sendtime).TotalMilliseconds + "字节数" + send.Length.ToString() + "是否正常" + check);

                            _hda.Add(new HistoryData(during, _DataLength, check, DateTime.Now, _PLCDriver.Address));

                            if (ListData.Count > 10)
                            {
                                switch (DataTyte)
                                {
                                case comm.DataTyte.BOOL:

                                    break;

                                case comm.DataTyte.WORD:

                                    SaveData <HistoryData>("HistoryData_Word" + ThreadCount, ListData);
                                    break;

                                case comm.DataTyte.INT16:

                                    SaveData <HistoryData>("HistoryData_Int16" + ThreadCount, ListData);
                                    break;

                                case comm.DataTyte.INT32:
                                    SaveData <HistoryData>("HistoryData_Int32" + ThreadCount, ListData);
                                    break;

                                case comm.DataTyte.REAL:
                                    SaveData <HistoryData>("HistoryData_real" + ThreadCount, ListData);
                                    break;

                                case comm.DataTyte.STRING:
                                    SaveData <HistoryData>("HistoryData_str" + ThreadCount, ListData);
                                    break;

                                default:
                                    break;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageEventHandler(_PLCDriver.ErrorCode + ex.Message.ToString(), null);
                        }
                        { }
                    }
                }, token
                                );
                task.Start();
                LogMessage = LogMessage + _dataTyte.ToString() + "线程开始中-----------" + DateTime.Now + "\r\n";
                if (MessageEventHandler != null)
                {
                    MessageEventHandler(LogMessage, null);
                }
            }
            catch (Exception ex)
            {
                if (MessageEventHandler != null)
                {
                    MessageEventHandler(ex.Message.ToString(), null);
                }
                // MessageBox.Show(ex.Message);
            }
        }