private void AsyncFloatStart(LibnodavePLC lIBnodavePLC) { int ERROR = 0; string recive = null; logNet2 = new LogNetSingle("FloatDataLog.txt"); ThreadState1 = true; Task.Run(() => { float _value = 1.00f; while (true) { _value = _value + 0.01f; lIBnodavePLC.WriteFloat(_value); this.Invoke(new Action(() => { logNet2.RecordMessage(HslMessageDegree.DEBUG, "写入", _value.ToString()); recive = lIBnodavePLC.ReadFloat().ToString(); logNet2.RecordMessage(HslMessageDegree.DEBUG, "读取", recive); if (_value.ToString() != recive.ToString()) { ERROR++; } logNet2.RecordMessage(HslMessageDegree.DEBUG, "ERROR", ERROR.ToString()); } ) ); } }); }
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(); } }
/// <summary> /// 带返回结果的ReadBytes方法,获取读取字节请获取ReciveDate /// </summary> /// <param name="len">读取字节数组长度</param> /// <returns><eturns> public int ReadBytesResult(int len)//从PLC中读取自己数组 { List <byte> bytesContent = new List <byte>(); if ((dc != null)) { int Start = Device.Start; //if (IsConnect) //{ // this.Connect(); //} byte[] buffer; ushort alreadyFinished = 0; while (alreadyFinished < len) { logNet.RecordMessage(HslMessageDegree.INFO, "已读取读取长度", alreadyFinished.ToString() + DateTime.Now); ushort readLength = (ushort)Math.Min(len - alreadyFinished, 200); logNet.RecordMessage(HslMessageDegree.INFO, "读取长度", readLength.ToString() + DateTime.Now); int res = -1; buffer = new byte[readLength]; res = dc.readBytes(Device.Area, Device.DBNumber, Start, readLength, buffer); logNet.RecordMessage(HslMessageDegree.INFO, "读取数据返回结果:", daveStrerror(res) + DateTime.Now); if (res != 0) { ErrorCode = daveStrerror(res); if (res < 0) { _isconnect = false; //int i = 0; //while (i < 3) //{ // i++; // // _closed = true; _closeTime = DateTime.Now; // this.Connect(); // res = dc.readBytes(Device.Area, Device.DBNumber, Start, readLength, buffer); // if (res == 0) // break; //} } return(res); } bytesContent.AddRange(buffer); alreadyFinished += readLength; Start += readLength; } ReadBuff = bytesContent.ToArray(); _readtime = DateTime.Now; return(0); } return(0xffff); }
private ILogNet logNet; // 日志 private void button1_Click(object sender, EventArgs e) { // 写日志 HslMessageDegree degree = (HslMessageDegree)comboBox1.SelectedItem; // 两种方法,第一种 logNet.RecordMessage(degree, textBox1.Text, textBox2.Text); // 第二种 //if(degree == HslMessageDegree.DEBUG) //{ // logNet.WriteDebug( textBox1.Text, textBox2.Text ); //} //else if(degree == HslMessageDegree.INFO) //{ // logNet.WriteInfo( textBox1.Text, textBox2.Text ); //} //else if(degree == HslMessageDegree.WARN) //{ // logNet.WriteWarn( textBox1.Text, textBox2.Text ); //} //else if (degree == HslMessageDegree.ERROR) //{ // logNet.WriteError( textBox1.Text, textBox2.Text ); //} //else if (degree == HslMessageDegree.FATAL) //{ // logNet.WriteFatal( textBox1.Text, textBox2.Text ); //} }
private void LibInit() { try { ThreadState4 = true; logNet2 = new LogNetSingle("StrDataLog.txt"); _LibnodavePLC = new LibnodavePLC(); bool isopen = _LibnodavePLC.Init("DB1.0", Txt_IP.Text);; byte[] ReciveData = new byte[1000]; string sendmessage = ""; Task.Run(() => { while (true) { this.Invoke(new Action(() => { lock (this) { sendmessage = Txt_Send.Text = GetRandomString(400); } } ) ); _LibnodavePLC.WriteBytes(SoftBasic.HexStringToBytes(Txt_Send.Text)); logNet2.RecordMessage(HslMessageDegree.DEBUG, "写入", sendmessage); ReciveData = _LibnodavePLC.ReadBytes(200); this.Invoke(new Action(() => { lock (this) Txt_Recieve.Text = SoftBasic.ByteToHexString(ReciveData); logNet2.RecordMessage(HslMessageDegree.DEBUG, "读取", Txt_Recieve.Text); logNet2.RecordMessage(HslMessageDegree.DEBUG, null, "是否正常" + Check(Txt_Recieve.Text, Txt_Send.Text)); })); } } ); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
//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(); }
/// <summary> ///记录程序日志 /// </summary> /// <param name="degree">日志等级</param> /// <param name="key">关键字</param> /// <param name="txt">日志内容</param> public void WriteLog(HslMessageDegree degree, string key, string txt) { LogNetProgramer.RecordMessage(degree, key, txt); }
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); } }