| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using static CCDCount.DLL.Inovance.IncvanceEip;
- namespace CCDCount.DLL.Inovance
- {
- public class IncvanceSendMessClass
- {
- bool isStart = false;
- string strClaimedIP = "";
- int InstanceId = -1;
- public int OpenEip(string IP)
- {
- int result = -1;
- //limit input of controller
- bool blnTest = false;
- bool bValidIP = true;
- Regex regex = new Regex("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$");
- blnTest = regex.IsMatch(IP);
- //输入的IP是否符合IP格式
- if (blnTest == true)
- {
- string[] strTemp = IP.Split(new char[] { '.' });
- if (strTemp.Length < 4)
- {
- Console.WriteLine("IncvanceSendMessClass-OpenEip:不符合IP格式");
- bValidIP = false;
- result = 1;
- }
- for (int i = 0; (i < strTemp.Length) && bValidIP; i++)
- {
- if (Convert.ToInt32(strTemp[i]) > 255)
- { //大于255则提示,不符合IP格式
- Console.WriteLine("IncvanceSendMessClass-OpenEip:不符合IP格式");
- bValidIP = false;
- result = 1;
- }
- }
- }
- else
- {
- //输入非数字则提示,不符合IP格式
- Console.WriteLine("IncvanceSendMessClass-OpenEip:不符合IP格式");
- bValidIP = false;
- result = 1;
- }
- //开启EIP协议栈
- if (bValidIP)
- {
- if (!isStart)
- {
- if (IncvanceEip.EipStartExt(IP, 0))
- {
- isStart = true;
- strClaimedIP = IP;
- Console.WriteLine("IncvanceSendMessClass-OpenEip:EIP协议栈开启成功");
- result = 0;
- }
- else
- {
- isStart = false;
- Console.WriteLine("IncvanceSendMessClass-OpenEip:EIP协议栈开启失败");
- result = 2;
- }
- return result;
- }
- else
- {
- if (string.Compare(strClaimedIP, IP) != 0)
- {
- Console.WriteLine("IncvanceSendMessClass-OpenEip:更改上位机IP时需先关闭协议栈,再开启");
- result = 3;
- return result;
- }
- else
- {
- result = 4;
- Console.WriteLine("IncvanceSendMessClass-OpenEip:EIP协议栈已经开启");
- result = 0;
- return result;
- }
- }
- }
- return result;
- }
- public void CloseEip()
- {
- if (isStart)
- {
- IncvanceEip.EipStop();
- isStart = false;
- Console.WriteLine("IncvanceSendMessClass-CloseEip:EIP协议栈关闭");
- return;
- }
- else
- {
- Console.WriteLine("IncvanceSendMessClass-CloseEip:EIP协议栈未开启,请先开启");
- return;
- }
- }
- public int CreateClass3Connect(string PLCIP)
- {
- bool blnTest = false;
- bool bValidIP = true;
- Regex regex = new Regex("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$");
- blnTest = regex.IsMatch(PLCIP);
- if (blnTest == true)
- {
- string[] strTemp = PLCIP.Split(new char[] { '.' });
- for (int i = 0; i < strTemp.Length; i++)
- {
- if (Convert.ToInt32(strTemp[i]) > 255)
- {
- Console.WriteLine("IncvanceSendMessClass-CreateClass3Connect:不符合IP格式"); //大于255
- bValidIP = false;
- }
- }
- }
- else
- {
- Console.WriteLine("IncvanceSendMessClass-CreateClass3Connect:不符合IP格式"); //输入非数字
- bValidIP = false;
- }
- if (bValidIP)
- {
- int instanceId = 0;
- ERROR_NO errorNo;
- unsafe
- {
- errorNo = (ERROR_NO)EipOpenConnection(PLCIP, (IntPtr)(&instanceId));
- }
- string str = "";
- str += "\n请求创建连接 ip:";
- str += PLCIP;
- str += " ";
- if (errorNo != ERROR_NO.SUCCESS)
- {
- str += ("失败 ");
- switch (errorNo)
- {
- case ERROR_NO.ERR_EIP_STOPED:
- str += ("协议栈未开启");
- break;
- case ERROR_NO.ERRI_INVALID_CONNECTION_INSTANCE_SPECIFIED:
- str += ("连接的实例ID与已有的ID重复或超过最大值");
- break;
- case ERROR_NO.ERRI_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
- str += ("连接的网络路径格式错误,无法检测出来目标IP离线等错误");
- break;
- case ERROR_NO.ERRI_CONNECTION_COUNT_LIMIT_REACHED:
- str += ("达到最大连接数量");
- break;
- case ERROR_NO.ERRI_OUT_OF_MEMORY:
- str += ("内存溢出,缓冲区已满");
- break;
- case ERROR_NO.ERRR_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
- str += ("连接的网络地址无效");
- break;
- case ERROR_NO.ERRR_CONN_CONFIG_FAILED_NO_RESPONSE:
- str += ("连接无响应");
- break;
- case ERROR_NO.ERRR_CONN_CONFIG_FAILED_ERROR_RESPONSE:
- str += ("连接响应错误");
- break;
- default:
- str += ("其他错误");
- break;
- }
- }
- else
- {
- str += "成功";
- str += " 分配的实例ID 为 ";
- str += instanceId.ToString();
- InstanceId = instanceId;
- }
- str += "\n";
- Console.WriteLine("IncvanceSendMessClass-CreateClass3Connect:{0}", str);
- return 0;
- }
- return -1;
- }
- public void CloseClass3Connect()
- {
- if (InstanceId != -1)
- {
- ERROR_NO errorNo = (ERROR_NO)EipCloseConnection(InstanceId);
- string str = "";
- str += "\n请求关闭连接 实例ID:";
- str += InstanceId.ToString();
- if (errorNo != ERROR_NO.SUCCESS)
- {
- str += "失败";
- }
- else
- {
- str += "成功";
- }
- str += "\n";
- Console.WriteLine("IncvanceSendMessClass-CloseClass3Connect:{0}", str);
- }
- else
- {
- Console.WriteLine("IncvanceSendMessClass-CloseClass3Connect:参数错误");
- }
- }
- public void GetConnectState()
- {
- if (InstanceId != -1)
- {
- EtIPConnectionState state = (EtIPConnectionState)EipGetConnectionState(InstanceId);
- string str = "";
- str += "\n获取连接状态 实例ID:\n";
- str += InstanceId.ToString();
- switch (state)
- {
- case EtIPConnectionState.ConnectionNonExistent:
- str += "该实例未有连接";
- break;
- case EtIPConnectionState.ConnectionConfiguring:
- str += "连接正在打开过程中";
- break;
- case EtIPConnectionState.ConnectionEstablished:
- str += "连接已成功建立并在活动中";
- break;
- case EtIPConnectionState.ConnectionTimedOut:
- str += "连接超时";
- break;
- case EtIPConnectionState.ConnectionClosing:
- str += "连接正在关闭中";
- break;
- default:
- break;
- }
- str += "\n";
- Console.WriteLine("IncvanceSendMessClass-GetConnectState:{0}", str);
- }
- else
- {
- Console.WriteLine("IncvanceSendMessClass-GetConnectState:参数错误");
- }
- }
- public byte[] ReadRequest(string TagName,int ElementCount)
- {
- byte[] result = null;
- if (InstanceId != -1)
- {
- byte[] tagNameUTF8 = Encoding.UTF8.GetBytes(TagName);
- if (tagNameUTF8.Length > 255)
- {
- string str = "\n标签名长度超过255字节,请重新输入\n";
- //richTextBox1.AppendText(str);
- return result;
- }
- if (ElementCount > 0)
- {
- int destLength = 1400;
- byte[] dest = new byte[destLength];
- char[] cChar = Encoding.ASCII.GetChars(dest);
- tagTagReadDataBase[] pTaglist = new tagTagReadDataBase[1];
- pTaglist[0].pName = TagName;
- pTaglist[0].nElementCount = ElementCount;
- tagTagRetValue[] tagValue = new tagTagRetValue[1];
- ERROR_NO errorNo = ERROR_NO.OTHER_ERROR;
- //Stopwatch stopwatch = Stopwatch.StartNew();
- unsafe
- {
- errorNo = EipReadTagExt2(InstanceId, pTaglist, ref tagValue[0]);
- }
- //stopwatch.Stop();
- //Console.WriteLine("读取数据耗时:" + stopwatch.ElapsedMilliseconds);
- string strLog = "";
- strLog += "\n读取请求";
- if (errorNo != ERROR_NO.SUCCESS)
- {
- strLog += " 失败 标签名:" + TagName + " 实例ID:" + InstanceId.ToString() + " ";
- switch (errorNo)
- {
- case ERROR_NO.ERR_EIP_STOPED:
- strLog += ("协议栈未开启");
- break;
- case ERROR_NO.ERRI_INVALID_CONNECTION_INSTANCE_SPECIFIED:
- strLog += ("连接的实例ID与已有的ID重复或超过最大值");
- break;
- case ERROR_NO.ERRI_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
- strLog += ("连接的网络路径格式错误,无法检测出来目标IP离线等错误");
- break;
- case ERROR_NO.ERRI_CONNECTION_COUNT_LIMIT_REACHED:
- strLog += ("达到最大连接数量");
- break;
- case ERROR_NO.ERRI_OUT_OF_MEMORY:
- strLog += ("内存溢出,缓冲区已满");
- break;
- case ERROR_NO.ERRR_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
- strLog += ("连接的网络地址无效");
- break;
- case ERROR_NO.ERRR_CONN_CONFIG_FAILED_NO_RESPONSE:
- strLog += ("连接无响应");
- break;
- case ERROR_NO.ERRR_CONN_CONFIG_FAILED_ERROR_RESPONSE:
- strLog += ("连接响应错误");
- break;
- case ERROR_NO.ERRR_INVALID_DESTINATION:
- strLog += ("目标标签不存在");
- break;
- case ERROR_NO.ERRR_TAGNAME_TOO_LONG:
- strLog += ("标签名超过255字节");
- break;
- case ERROR_NO.ERRR_REQUEST_DATA_TOO_LARGE:
- strLog += ("请求数据超限");
- break;
- case ERROR_NO.ERRR_CONN_CONNECTION_TIMED_OUT:
- strLog += ("响应超时,请检查设备是否离线");
- break;
- case ERROR_NO.ERRR_TAGNAME_CONVERT_FAILED:
- strLog += ("标签名解析错误");
- break;
- case ERROR_NO.ERRR_SCAN_ERROR:
- strLog += ("扫描标签信息失败");
- break;
- default:
- strLog += ("其他错误");
- break;
- }
- strLog += "\n";
- //richTextBox1.AppendText(strLog);
- return result;
- }
- else
- {
- strLog += " 成功 实例ID:" + InstanceId.ToString() + " 数据如下:";
- strLog += "\n标签名:" + TagName;
- //richTextBox1.AppendText(strLog);
- string strType = "\n类型:";
- switch (tagValue[0].pType)
- {
- case TAG_TYPE.TAG_TYPE_BOOL:
- strType += "BOOL";
- break;
- case TAG_TYPE.TAG_TYPE_SINT:
- strType += "SINT";
- break;
- case TAG_TYPE.TAG_TYPE_INT:
- strType += "INT";
- break;
- case TAG_TYPE.TAG_TYPE_DINT:
- strType += "DINT";
- break;
- case TAG_TYPE.TAG_TYPE_LINT:
- strType += "LINT";
- break;
- case TAG_TYPE.TAG_TYPE_USINT:
- strType += "USINT";
- break;
- case TAG_TYPE.TAG_TYPE_UINT:
- strType += "UINT";
- break;
- case TAG_TYPE.TAG_TYPE_UDINT:
- strType += "UDINT";
- break;
- case TAG_TYPE.TAG_TYPE_ULINT:
- strType += "ULINT";
- break;
- case TAG_TYPE.TAG_TYPE_REAL:
- strType += "REAL";
- break;
- case TAG_TYPE.TAG_TYPE_LREAL:
- strType += "LREAL";
- break;
- case TAG_TYPE.TAG_TYPE_STRING:
- strType += "STRING";
- break;
- case TAG_TYPE.TAG_TYPE_BYTE:
- strType += "BYTE";
- break;
- case TAG_TYPE.TAG_TYPE_WORD:
- strType += "WORD";
- break;
- case TAG_TYPE.TAG_TYPE_DWORD:
- strType += "DWORD";
- break;
- case TAG_TYPE.TAG_TYPE_LWORD:
- strType += "LWORD";
- break;
- case TAG_TYPE.TAG_TYPE_STRUCT:
- strType += "STRUCT";
- break;
- case TAG_TYPE.TAG_TYPE_ARRAY:
- strType += "ARRAY";
- break;
- default:
- break;
- }
- //richTextBox1.AppendText(strType += "\n");
- string str2 = "元素个数:";
- str2 += ElementCount.ToString();
- str2 += "\n数据长度:";
- str2 += tagValue[0].nDataLength.ToString();
- //richTextBox1.AppendText(str2 += "\n");
- unsafe
- {
- byte* memBytePtr = (byte*)tagValue[0].pData.ToPointer();
- result = new byte[tagValue[0].nDataLength];
- for (int j = 0; j < tagValue[0].nDataLength; j++)
- {
- string str = "";
- str += j.ToString() + ": ";
- str += "0x" + String.Format("{0:X2}", memBytePtr[j]) + "\n";
- result[j] = memBytePtr[j];
- //richTextBox1.AppendText(str);
- }
- }
- DeleteTagListStru(ref tagValue[0], 1); //调用接口释放内存
- }
- }
- else
- {
- //richTextBox1.AppendText("参数错误\n");
- }
- }
- else
- {
- //richTextBox1.AppendText("参数错误\n");
- }
- return result;
- }
- public void WriteRequest(string TagName,int ElementCount,byte[] data)
- {
- if (InstanceId != -1)
- {
- byte[] tagNameUTF8 = Encoding.UTF8.GetBytes(TagName);
- if (tagNameUTF8.Length > 255)
- {
- string str = "\n标签名长度超过255字节,请重新输入\n";
- //richTextBox1.AppendText(str);
- return;
- }
- if (data.Length > 1400)
- {
- string str = "\n数据长度超过1400字节,请重新输入\n";
- //richTextBox1.AppendText(str);
- return;
- }
- if (ElementCount>0)
- {
- tagTagWriteDataBase[] pTaglist = new tagTagWriteDataBase[1];
- //标签1属性
- pTaglist[0].pName = TagName;
- pTaglist[0].nElementCount = ElementCount;
- pTaglist[0].pType = TAG_TYPE.TAG_TYPE_UNDEFINE;
- pTaglist[0].pData = Marshal.AllocHGlobal(1400);
- pTaglist[0].nDataLength = data.Length/2;
- byte temp = 0;
- for (int i = 0; i < data.Length; ++i)
- {
- temp = data[i];
- Marshal.WriteByte(pTaglist[0].pData + i * sizeof(byte), temp);
- }
- ERROR_NO errorNo = EipWriteTagExt2(InstanceId, pTaglist);
- string strLog = "";
- strLog += "\n写入请求 ";
- if (errorNo != ERROR_NO.SUCCESS)
- {
- strLog += " 失败 标签名:" + TagName + " 实例ID:" + InstanceId.ToString() + " ";
- switch (errorNo)
- {
- case ERROR_NO.ERR_EIP_STOPED:
- strLog += ("协议栈未开启");
- break;
- case ERROR_NO.ERRI_INVALID_CONNECTION_INSTANCE_SPECIFIED:
- strLog += ("连接的实例ID与已有的ID重复或超过最大值");
- break;
- case ERROR_NO.ERRI_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
- strLog += ("连接的网络路径格式错误,无法检测出来目标IP离线等错误");
- break;
- case ERROR_NO.ERRI_CONNECTION_COUNT_LIMIT_REACHED:
- strLog += ("达到最大连接数量");
- break;
- case ERROR_NO.ERRI_OUT_OF_MEMORY:
- strLog += ("内存溢出,缓冲区已满");
- break;
- case ERROR_NO.ERRR_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
- strLog += ("连接的网络地址无效");
- break;
- case ERROR_NO.ERRR_CONN_CONFIG_FAILED_NO_RESPONSE:
- strLog += ("连接无响应");
- break;
- case ERROR_NO.ERRR_CONN_CONFIG_FAILED_ERROR_RESPONSE:
- strLog += ("连接响应错误");
- break;
- case ERROR_NO.ERRR_INVALID_DESTINATION:
- strLog += ("目标标签不存在");
- break;
- case ERROR_NO.ERRR_TAGNAME_TOO_LONG:
- strLog += ("标签名超过255字节");
- break;
- case ERROR_NO.ERRR_REQUEST_DATA_TOO_LARGE:
- strLog += ("请求数据超限");
- break;
- case ERROR_NO.ERRR_CONN_CONNECTION_TIMED_OUT:
- strLog += ("响应超时,请检查设备是否离线");
- break;
- case ERROR_NO.ERRR_TAGNAME_CONVERT_FAILED:
- strLog += ("标签名解析错误");
- break;
- case ERROR_NO.ERRR_WRITE_DATASIZE_UNCONSISTENT:
- strLog += ("写入数据长度与标签实际长度不一致");
- break;
- case ERROR_NO.ERRR_SCAN_ERROR:
- strLog += ("扫描标签信息失败");
- break;
- default:
- strLog += ("其他错误");
- break;
- }
- strLog += "\n";
- //richTextBox1.AppendText(strLog);
- return;
- }
- else
- {
- strLog += "成功 实例ID:" + InstanceId.ToString() + " 写入数据如下:";
- strLog += "\n标签名:" + TagName;
- strLog += "\n元素个数:" + ElementCount.ToString();
- strLog += "\n写入数据长度:" + data.Length.ToString() + "\n";
- //richTextBox1.AppendText(strLog);
- for (int i = 0; i < data.Length; ++i)
- {
- string strA = "";
- strA += i.ToString() + ": ";
- strA += "0x" + String.Format("{0:X2}", data[i]) + "\n";
- //richTextBox1.AppendText(strA);
- }
- }
- }
- else
- {
- //richTextBox1.AppendText("参数错误\n");
- }
- }
- else
- {
- //richTextBox1.AppendText("参数错误\n");
- }
- }
- }
- }
|