private void AsyncBitStart(LIBnodavePLC lIBnodavePLC) { if (!ThreadState3) { int ERROR = 0; ThreadState3 = true; bool flagl = true; string log = string.Format("BitDataLog.txt"); LogNetBit = new LogNetSingle(log); string recive = null; Task task = new Task(() => { while (true) { Thread.Sleep(10); flagl = !flagl; lIBnodavePLC.WriteBit(flagl); LogNetBit.RecordMessage(HslMessageDegree.DEBUG, "写入", flagl.ToString()); recive = lIBnodavePLC.ReadBit().ToString(); LogNetBit.RecordMessage(HslMessageDegree.DEBUG, "读入", recive); if (flagl.ToString() != recive.ToString()) { ERROR++; } LogNetBit.RecordMessage(HslMessageDegree.DEBUG, "ERROR", ERROR.ToString()); } } ); task.Start(); } }
//private void AsyncFloatStart( LIBnodavePLC lIBnodavePLC, float[] floats) //{ // int _Lenth = floats.Length; // int ERROR = 0; // string recive = null; // //string send = null; // bool check= false; // LogNetFloat = new LogNetSingle("FloatDataLog.txt"); // ThreadState1 = true; // double during = 0; // Task.Run(() => { // while (true) // { // if (floats != null) // { // Thread.Sleep(10); // floats = GetRandomfloat(750); // lIBnodavePLC.WriteFloats(floats); // lIBnodavePLC.ReadFloats(Convert.ToUInt16(floats.Length)); // check = (floats as IStructuralEquatable).Equals(lIBnodavePLC.Floats, StructuralComparisons.StructuralEqualityComparer); // during = (lIBnodavePLC.Readtime - lIBnodavePLC.Sendtime).TotalMilliseconds; // LogNetFloat.RecordMessage(HslMessageDegree.DEBUG, null, "耗时" + (lIBnodavePLC.Readtime - lIBnodavePLC.Sendtime).TotalMilliseconds + "字节数" + _Lenth.ToString() + "是否正常" + check); // _hdaFloat.Add(new HistoryData(during,_Lenth,check,DateTime.Now,)); // } // else // { // float _value = 1.00f; // _value = _value + 0.01f; // lIBnodavePLC.WriteFloat(_value); // this.Invoke(new Action(() => // { // LogNetFloat.RecordMessage(HslMessageDegree.DEBUG, "写入", _value.ToString()); // recive = lIBnodavePLC.ReadFloat().ToString(); // LogNetFloat.RecordMessage(HslMessageDegree.DEBUG, "读取", recive); // if (_value.ToString() != recive.ToString()) // { // ERROR++; // } // LogNetFloat.RecordMessage(HslMessageDegree.DEBUG, "ERROR", ERROR.ToString()); // } // ) // ); // } // } // }); //} //private void AsyncInt32Start(LIBnodavePLC lIBnodavePLC,int _Length) //{ // //if (ThreadState2) // //{ // // return; // //} // try // { // int ERROR = 0; // string log = string.Format("Int32DataLog.txt"); // LogNetInt32 = new LogNetSingle(log); // string recive = null; // int[] send=null; // ThreadState2 = true; // bool check = false; // Task task = new Task(() => // { // int _value = 1; // while (true) // { // if (_Length!= 0) // { // send = GetRandomInt32(_Length); // Thread.Sleep(10); // lIBnodavePLC.WriteInt32s(send); // lIBnodavePLC.ReadInt32s(_Length); // check = Check(lIBnodavePLC.Int32s,send); // //LogNetInt32.RecordMessage(HslMessageDegree.DEBUG, null, "耗时" + (lIBnodavePLC.Readtime - lIBnodavePLC.Sendtime).TotalMilliseconds + "字节数" + _Lenth.ToString() + "是否正常" + Check(recive, send)); // _hdaInt32.Add(new HistoryData((lIBnodavePLC.Readtime - lIBnodavePLC.Sendtime).TotalMilliseconds, _Length*4, check, DateTime.Now)); // } // else // { // _value = _value + 1; // lIBnodavePLC.WriteInt32(_value); // // Txt_Send.Text = _value.ToString(); // LogNetInt32.RecordMessage(HslMessageDegree.DEBUG, "写入", _value.ToString()); // recive = lIBnodavePLC.ReadInt32().ToString(); // LogNetInt32.RecordMessage(HslMessageDegree.DEBUG, "读取", recive); // if (_value.ToString() != recive.ToString()) // { // ERROR++; // } // LogNetInt32.RecordMessage(HslMessageDegree.DEBUG, "ERROR", ERROR.ToString()); // } // } // }); // task.Start(); // } // catch (Exception ex) // { // throw ex; // } //} private void AsyncInt32Start(LIBnodavePLC lIBnodavePLC) { //if (ThreadState2) //{ // return; //} try { int ERROR = 0; string log = string.Format("Int32DataLog.txt"); LogNetInt32 = new LogNetSingle(log); string recive = null; ThreadState2 = true; Task task = new Task(() => { int _value = 1; while (true) { Thread.Sleep(10); _value = _value + 1; lIBnodavePLC.WriteInt32(_value); // Txt_Send.Text = _value.ToString(); LogNetInt32.RecordMessage(HslMessageDegree.DEBUG, "写入", _value.ToString()); recive = lIBnodavePLC.ReadInt32().ToString(); LogNetInt32.RecordMessage(HslMessageDegree.DEBUG, "读取", recive); if (_value.ToString() != recive.ToString()) { ERROR++; } LogNetInt32.RecordMessage(HslMessageDegree.DEBUG, "ERROR", ERROR.ToString()); } }); task.Start(); } catch (Exception ex) { throw ex; } }
//private void ReadData<T>(T t) //{ // AsyncStart(new LIBnodavePLC(), new byte[1]); //} //private void AsyncInt16Start(LIBnodavePLC lIBnodavePLC, int Length) //{ // if (ThreadState) // { // return; // } // short[] sendbyte = null; // int ERROR = 0; // bool check = false; // // string send = null; // string recive = null; // string log = string.Format("Int16DataLog.txt"); // LogNetInt16 = new LogNetSingle(log); // ThreadState = true; // Task task = new Task(() => // { // Int16 _value = 1; // while (true) // { // if (Length != 0) // { // Thread.Sleep(10); // sendbyte = GetRandomInt16(Length); // lIBnodavePLC.WriteInt16s(GetRandomInt16(Length)); // lIBnodavePLC.ReadInt16s(sendbyte.Length); // // LogNetInt16.RecordMessage(HslMessageDegree.DEBUG, null, "耗时" + (lIBnodavePLC.Readtime - lIBnodavePLC.Sendtime).TotalMilliseconds + "字节数" + sendbyte.Length.ToString() + "是否正常" + Check(recive, send)); // check = Check(lIBnodavePLC.Int16s, sendbyte); // //LogNetInt32.RecordMessage(HslMessageDegree.DEBUG, null, "耗时" + (lIBnodavePLC.Readtime - lIBnodavePLC.Sendtime).TotalMilliseconds + "字节数" + _Lenth.ToString() + "是否正常" + Check(recive, send)); // _hdaInt16.Add(new HistoryData((lIBnodavePLC.Readtime - lIBnodavePLC.Sendtime).TotalMilliseconds, Length * 2, check, DateTime.Now)); // } // else // { // _value++; // lIBnodavePLC.WriteInt16(_value); // LogNetInt16.RecordMessage(HslMessageDegree.DEBUG, "写入", _value.ToString()); // recive = lIBnodavePLC.ReadInt16().ToString(); // LogNetInt16.RecordMessage(HslMessageDegree.DEBUG, "读取", recive); // if (_value.ToString() != recive.ToString()) // { // ERROR++; // } // LogNetInt16.RecordMessage(HslMessageDegree.DEBUG, "ERROR", ERROR.ToString()); // if (_value > 100) // { // _value = 0; // } // } // } // }); // task.Start(); //} private void AsyncInt16Start(LIBnodavePLC lIBnodavePLC) { if (ThreadState) { return; } int ERROR = 0; string recive = null; string log = string.Format("Int16DataLog.txt"); LogNetInt16 = new LogNetSingle(log); ThreadState = true; Task task = new Task(() => { Int16 _value = 1; while (true) { Thread.Sleep(10); _value++; lIBnodavePLC.WriteInt16(_value); LogNetInt16.RecordMessage(HslMessageDegree.DEBUG, "写入", _value.ToString()); recive = lIBnodavePLC.ReadInt16().ToString(); LogNetInt16.RecordMessage(HslMessageDegree.DEBUG, "读取", recive); if (_value.ToString() != recive.ToString()) { ERROR++; } LogNetInt16.RecordMessage(HslMessageDegree.DEBUG, "ERROR", ERROR.ToString()); if (_value > 100) { _value = 0; } } }); task.Start(); }
private void AsyncStart() { try { string Addr = this.Addr; string IP = this.IP; LIBnodavePLC _LIBnodavePLC = new LIBnodavePLC(); bool isopen = _LIBnodavePLC.Init(Addr, IP);; bool check = false; string send = ""; string recive = ""; int length = 0; IEnumerable sendBytes; Task.Run(() => { while (true) { Thread.Sleep(10); switch (this._dataTyte) { case comm.DataTyte.BOOL: break; case comm.DataTyte.WORD: length = this._DataLength / 2; sendBytes = GetRandomUshort(length); _LIBnodavePLC.WriteInt16s(sendBytes as Int16[]); _LIBnodavePLC.ReadInt16s(length); check = Check(sendBytes, _LIBnodavePLC.Int16s); break; case comm.DataTyte.INT16: length = this._DataLength / 2; sendBytes = GetRandomInt16(length); _LIBnodavePLC.WriteInt16s(sendBytes as Int16[]); _LIBnodavePLC.ReadInt16s(length); check = Check(sendBytes, _LIBnodavePLC.Int16s); break; case comm.DataTyte.DINT32: length = this._DataLength / 4; sendBytes = GetRandomInt32(length); _LIBnodavePLC.WriteInt32s(sendBytes as Int32[]); _LIBnodavePLC.ReadInt32s(length); check = Check(sendBytes, _LIBnodavePLC.Int32s); break; case comm.DataTyte.REAL: length = this._DataLength / 2; sendBytes = GetRandomfloat(length); _LIBnodavePLC.WriteFloats(sendBytes as float[]); _LIBnodavePLC.ReadFloats(length); check = Check(sendBytes, _LIBnodavePLC.Floats); break; case comm.DataTyte.STRING: length = this._DataLength * 2; send = GetRandomString(length); _LIBnodavePLC.WriteBytes(SoftBasic.HexStringToBytes(send)); byte[] ReciveData = _LIBnodavePLC.ReadBytes(length / 2); recive = SoftBasic.ByteToHexString(ReciveData); check = send.Equals(recive); break; default: break; } double during = (_LIBnodavePLC.Readtime - _LIBnodavePLC.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)); } } ); } catch (Exception ex) { // MessageBox.Show(ex.Message); } }
public Contectfactory(LIBnodavePLC lI) { this._libnodavePLC = lI; }