Inovance_EIP.cs 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Dynamic;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Runtime.InteropServices;
  10. using System.Runtime.Serialization;
  11. using System.Security.Policy;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Windows.Forms;
  15. using System.Xml.Linq;
  16. using static System.Net.Mime.MediaTypeNames;
  17. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  18. namespace EIP_Protocol
  19. {
  20. #region MES与PLC通讯结构
  21. public enum eMachineState:short
  22. {
  23. Uninitialized=0, //未初始化状态
  24. Initializing, //初始化中...
  25. Initialized, //初始化完成
  26. Running, //运行中
  27. Paused, //暂停状态
  28. Fault, //故障状态
  29. Alarm //报警状态
  30. }
  31. public enum eMesCmd:byte
  32. {
  33. none=0,
  34. InStation = 1, //1:工站进站申请
  35. OutStation =2 //2:工站出站申请
  36. }
  37. public enum eAgvCmd
  38. {
  39. RequestPassingIn = 1, // = 1, AGV请求进料
  40. ConfirmPassInFinish = 2, //= 2, AGV请求进料完成确认
  41. RequestPassingOut = 3, //=3, AGV请求出料
  42. ConfrimPassingOutFinish = 4 //= 4, AGV请求出料完成确认
  43. }
  44. //图⽚命名需要遵循⼩⽶标准
  45. //[项⽬]_[⼯站]_[SN]_[物料-功能]_[测试时间]_[定位/检测/测量结果]_[是否原图-当前第⼏张-共⼏张]
  46. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  47. public struct CommandToPLC
  48. {
  49. [MarshalAs(UnmanagedType.U1)]
  50. public byte cmd; //1:AGV请求进料 2:AGV请求进料完成确认 3:AGV请求出料 4:AGV请求出料完成确认 PLC:成功回被PC清零
  51. [MarshalAs(UnmanagedType.I2)]
  52. public short cmdParam; //1:左边接口 2:右边接口
  53. [MarshalAs(UnmanagedType.I2)]
  54. public short cmdResult; //指令执行结果 1:OK 110:失败
  55. }
  56. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  57. public struct agvCommFrmPC
  58. {
  59. [MarshalAs(UnmanagedType.U1)]
  60. public byte cmd; //1:AGV请求进料 2:AGV请求进料完成确认 3:AGV请求出料 4:AGV请求出料完成确认 PLC:成功回被PC清零
  61. [MarshalAs(UnmanagedType.I2)]
  62. public short cmdParam; //1:左边接口 2:右边接口
  63. [MarshalAs(UnmanagedType.I2)]
  64. public short cmdResult; //指令执行结果 1:OK 110:失败
  65. }
  66. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  67. public struct CommandFromPLC
  68. {
  69. [MarshalAs(UnmanagedType.U1)]
  70. public byte cmd; //1:工站进站申请 2:工站出站申请
  71. [MarshalAs(UnmanagedType.I2)]
  72. public short cmdParam; //指令参数
  73. [MarshalAs(UnmanagedType.I2)]
  74. public short cmdResult; //指令执行结果 1:OK 110:失败
  75. }
  76. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  77. public struct mesCommToPC
  78. {
  79. [MarshalAs(UnmanagedType.U1)]
  80. public byte cmd; //1:工站进站申请 2:工站出站申请
  81. [MarshalAs(UnmanagedType.I2)]
  82. public short cmdParam; //指令参数
  83. [MarshalAs(UnmanagedType.I2)]
  84. public short cmdResult; //指令执行结果 1:OK 110:失败
  85. }
  86. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  87. public struct OP10_From_PLC
  88. {
  89. [MarshalAs(UnmanagedType.I4)]
  90. public int nThrowCount; //抛料次数
  91. [MarshalAs(UnmanagedType.R4)]
  92. public float fCleanAirPress; //清洁气压
  93. [MarshalAs(UnmanagedType.R4)]
  94. public float fCleanSpeed; //清洁速度mm/s
  95. [MarshalAs(UnmanagedType.R4)]
  96. public float fWindBladeHeight; //风刀高度mm
  97. [MarshalAs(UnmanagedType.R4)]
  98. public float fCleanTime; //清洁时间S
  99. [MarshalAs(UnmanagedType.I4)]
  100. public int nCleanCount; //清洁次数
  101. }
  102. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  103. public struct OP10_DataSet_t
  104. {
  105. [MarshalAs(UnmanagedType.I4)]
  106. public int nThrowCount; //抛料次数
  107. [MarshalAs(UnmanagedType.R4)]
  108. public float fCleanAirPress; //清洁气压
  109. [MarshalAs(UnmanagedType.R4)]
  110. public float fCleanSpeed; //清洁速度mm/s
  111. [MarshalAs(UnmanagedType.R4)]
  112. public float fWindBladeHeight; //风刀高度mm
  113. [MarshalAs(UnmanagedType.R4)]
  114. public float fCleanTime; //清洁时间S
  115. [MarshalAs(UnmanagedType.I4)]
  116. public int nCleanCount; //清洁次数
  117. [MarshalAs(UnmanagedType.I4)]
  118. public int nRemainCount; //外壳体余料数
  119. }
  120. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  121. public struct IoT_DataSet_t
  122. {
  123. [MarshalAs(UnmanagedType.I2)]
  124. public short machineState; //设备状态
  125. [MarshalAs(UnmanagedType.I2)]
  126. public short work_type; //作业类型:=1 PRESSURE_TEST(压测),=2 POINT_CHECK(点检),=3 OUT_STATION(正常跑料数据)
  127. [MarshalAs(UnmanagedType.I2)]
  128. public short testStatus; //测试状态:=1 PASS 0=FAIL
  129. [MarshalAs(UnmanagedType.I2)]
  130. public short BeatAction; //节拍动作 1:上料开始 2:上料结束 3:作业开始 4:作业结束 5:下料开始 6:下料结束
  131. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] //一个单独的位代表一个报警32*10=320
  132. public uint[] fault_codes;
  133. }
  134. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  135. public struct OP20_DataSet_t
  136. {
  137. [MarshalAs(UnmanagedType.I4)]
  138. public int nThrowCount; //抛料次数
  139. [MarshalAs(UnmanagedType.I4)]
  140. public int nRemainCount; //上盖余料数
  141. }
  142. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  143. public struct OP30_stnDataSet_t //站数据集
  144. {
  145. [MarshalAs(UnmanagedType.R4)]
  146. public float fGlueSupplySpeed; //供胶速度
  147. [MarshalAs(UnmanagedType.R4)]
  148. public float fAB_AirPress; //AB管气压
  149. [MarshalAs(UnmanagedType.R4)]
  150. public float fAB_AirPressDiff; //AB管气压差
  151. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
  152. public float[] fMesHeightInfos; //产品测高信息--点胶前的测高(mm)
  153. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
  154. public float[] fIntervalWeights; //可能没有:定期称重数据 A胶,B胶
  155. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
  156. public float[] fRemainGlues; //剩余胶量A:0 B:1
  157. }
  158. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  159. public struct OP30_DataSet_t
  160. {
  161. public BarcodeSet_t BarcodeSet; //条码集合
  162. public CommandFromPLC mesCommFrmPLC; //MES通讯
  163. public OP30_stnDataSet_t mesData;
  164. public IoT_DataSet_t iotData;
  165. }
  166. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  167. public struct OP40_DataSet_t
  168. {
  169. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
  170. public float[] fGluePosX; //胶线位置X偏差
  171. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
  172. public float[] fGluePosY; //胶线位置Y偏差
  173. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
  174. public float[] fGlue_Areas; //胶线面积
  175. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
  176. public float[] fGlue_Heights; //胶线高度
  177. [MarshalAs(UnmanagedType.I4)]
  178. public int nResult; //胶线检测结果 1:OK 非1:NG
  179. //public Image TestPic; //测试照片
  180. }
  181. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  182. public struct OP50_DataSet_t
  183. {
  184. [MarshalAs(UnmanagedType.I4)]
  185. public int nIsAddPCBAsmOK; //是否组装到位
  186. [MarshalAs(UnmanagedType.I4)]
  187. public int nHaveAddPCB; //是否有ADD板
  188. [MarshalAs(UnmanagedType.R4)]
  189. public float fForceAddPCB; //装ADD板的压力
  190. [MarshalAs(UnmanagedType.I4)]
  191. public int nRemainCount; //ADD板余料数
  192. }
  193. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  194. public struct OP60_DataSet_t
  195. {
  196. [MarshalAs(UnmanagedType.I4)]
  197. public int nIsTopCoverAsmOK; //是否组装到位
  198. [MarshalAs(UnmanagedType.I4)]
  199. public int nHaveTopCover; //是否有上盖板
  200. [MarshalAs(UnmanagedType.R4)]
  201. public float fForceTopCover; //装上盖板的压力
  202. }
  203. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  204. public struct OP70_DataSet_t
  205. {
  206. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
  207. public float[] fScrewTimes; //锁附时间 PLC 14颗螺丝 预留6
  208. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
  209. public short[] nScrewOrders; //锁附顺序 PLC 锁螺丝的标号
  210. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
  211. public short[] nScrewResults; //锁附结果 PLC
  212. [MarshalAs(UnmanagedType.I4)]
  213. public int nRemainCount; //螺丝余料数
  214. }
  215. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  216. public struct OP70_ScrewData_t
  217. {
  218. [MarshalAs(UnmanagedType.R4)]
  219. public float fTorque; //上位机传到PLC的锁螺丝扭力 每次读到阿特拉斯的数据都传一次
  220. [MarshalAs(UnmanagedType.R4)]
  221. public float fCircles; //上位机传到PLC的锁螺丝圈数 每次读到阿特拉斯的数据都传一次
  222. }
  223. public class OP70_PC_CollectDataSet_t
  224. {
  225. //需要自己收集
  226. public float[] fScrewTorques; //锁附扭力
  227. public float[] fScrewCircles; //锁附圈数
  228. public float[][] fTorqueCurve; //扭力曲线
  229. }
  230. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  231. public struct OP70_stnDataSet_t
  232. {
  233. public BarcodeSet_t BarcodeSet; //条码集合
  234. public CommandFromPLC mesCommFrmPLC; //MES通讯
  235. public OP70_DataSet_t mesData;
  236. public IoT_DataSet_t iotData;
  237. public OP70_ScrewData_t screwDataToPLC; //传到PLC的锁螺丝数据,用于HMI显示用
  238. }
  239. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  240. public struct OP70_stnScrewDataSet_t
  241. {
  242. public OP70_ScrewData_t Left;
  243. public OP70_ScrewData_t Right;
  244. }
  245. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  246. public struct BarcodeSet_t
  247. {
  248. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 51)] //载具条码
  249. public string strCarrierBarcode;
  250. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 101)] //产品条码
  251. public string strProductBarcode;
  252. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 101)] //工位零部件条码
  253. public string strPartBarcode;
  254. }
  255. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  256. public struct OP10_MesData_t
  257. {
  258. public BarcodeSet_t BarcodeSet; //条码集合
  259. public CommandToPLC agvCommToPLC; //AGV通讯
  260. public CommandFromPLC mesCommFrmPLC; //MES通讯
  261. public OP10_DataSet_t mesData;
  262. public IoT_DataSet_t iotData;
  263. }
  264. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  265. public struct OP20_MesData_t
  266. {
  267. public BarcodeSet_t BarcodeSet; //条码集合
  268. public CommandToPLC agvCommToPLC; //AGV通讯
  269. public CommandFromPLC mesCommFrmPLC; //MES通讯
  270. public OP20_DataSet_t mesData;
  271. public IoT_DataSet_t iotData;
  272. }
  273. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  274. public struct OP30_iotDataSet_t
  275. {
  276. public IoT_DataSet_t Left;
  277. public IoT_DataSet_t Right;
  278. }
  279. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  280. public struct OP30_MesData_t
  281. {
  282. OP30_DataSet_t Left;
  283. OP30_DataSet_t Right;
  284. }
  285. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  286. public struct OP40_MesData_t
  287. {
  288. public BarcodeSet_t BarcodeSet; //条码集合
  289. public CommandFromPLC mesCommFrmPLC; //MES通讯
  290. public OP40_DataSet_t mesData;
  291. public IoT_DataSet_t iotData;
  292. }
  293. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  294. public struct OP50_MesData_t
  295. {
  296. public BarcodeSet_t BarcodeSet; //条码集合
  297. public CommandToPLC agvCommToPLC; //AGV通讯
  298. public CommandFromPLC mesCommFrmPLC; //MES通讯
  299. public OP50_DataSet_t mesData;
  300. public IoT_DataSet_t iotData;
  301. }
  302. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  303. public struct OP60_MesData_t
  304. {
  305. public BarcodeSet_t BarcodeSet; //条码集合
  306. public CommandFromPLC mesCommFrmPLC; //MES通讯
  307. public OP60_DataSet_t mesData;
  308. public IoT_DataSet_t iotData;
  309. }
  310. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  311. public struct OP70_iotDataSet_t
  312. {
  313. public IoT_DataSet_t Left;
  314. public IoT_DataSet_t Right;
  315. }
  316. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  317. public struct OP70_MesData_t
  318. {
  319. public OP70_stnDataSet_t Left;
  320. public OP70_stnDataSet_t Right;
  321. }
  322. public struct OP80_DataSet_t
  323. {
  324. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
  325. public float[] fScrewHeights; //螺丝高度 PLC 14颗螺丝 预留6
  326. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
  327. public short[] nScrewResults; //螺丝检测结果 PLC 1:OK 0:NG
  328. }
  329. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  330. public struct OP80_MesData_t
  331. {
  332. public BarcodeSet_t BarcodeSet; //条码集合
  333. public CommandFromPLC mesCommFrmPLC; //MES通讯
  334. public OP80_DataSet_t mesData;
  335. public IoT_DataSet_t iotData;
  336. }
  337. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  338. public struct OP90_DataSet_t
  339. {
  340. [MarshalAs(UnmanagedType.I4)]
  341. public int nThrowCount; //抛料次数
  342. [MarshalAs(UnmanagedType.I4)]
  343. public int nRemainCount; //料箱余料数
  344. }
  345. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
  346. public struct OP90_MesData_t
  347. {
  348. public BarcodeSet_t BarcodeSet; //条码集合
  349. public CommandToPLC agvCommToPLC; //AGV通讯
  350. public CommandFromPLC mesCommFrmPLC; //MES通讯
  351. public OP90_DataSet_t mesData;
  352. public IoT_DataSet_t iotData;
  353. }
  354. #endregion MES与PLC通讯结构
  355. public struct OP10_MesData_r
  356. {
  357. public BarcodeSet_t BarcodeSet; //条码集合
  358. public agvCommFrmPC agvCommFrmPC; //AGV通讯
  359. public mesCommToPC mesCommToPC; //MES通讯
  360. }
  361. public struct OP20_MesData_r
  362. {
  363. public BarcodeSet_t BarcodeSet; //条码集合
  364. public agvCommFrmPC agvCommFrmPC; //AGV通讯
  365. public mesCommToPC mesCommToPC; //MES通讯
  366. }
  367. public struct OP30_MesData_r
  368. {
  369. public BarcodeSet_t BarcodeSet; //条码集合
  370. public agvCommFrmPC agvCommFrmPC; //AGV通讯
  371. public mesCommToPC mesCommToPC; //MES通讯
  372. }
  373. #region 写入PLC的通讯结构
  374. #endregion
  375. public class Inovance_EIP
  376. {
  377. private string strTagPrefix = "Application.GVL."; //标签前缀
  378. private object m_objLock = new object();
  379. bool isStart = false;
  380. string strClaimedComputerIP = ""; //PC的IP地址 - private实际IP
  381. private int m_nInstanceId = 0; //实例ID
  382. public bool m_bConnected = false;
  383. public string _pcIPStr = string.Empty; //PC的IP地址
  384. public string _plcIPStr = string.Empty; //PLC的IP地址
  385. public bool IsConnected
  386. {
  387. get { return m_bConnected; }
  388. }
  389. public void Config_TagPrefix(string TagPrefix)
  390. {
  391. strTagPrefix = TagPrefix;
  392. }
  393. #region 自定义的结构
  394. public enum ERROR_NO : int
  395. {
  396. ERR_EIP_STOPED = -2,//协议栈未开启
  397. OTHER_ERROR = -1,
  398. SUCCESS = 0,
  399. ERRI_INVALID_CONNECTION_INSTANCE_SPECIFIED = 1,//连接的实例ID与已有的ID重复或超过最大值
  400. ERRI_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH,//连接的网络路径格式错误,无法检测出来目标IP离线等错误
  401. ERRI_CONNECTION_COUNT_LIMIT_REACHED,//达到最大连接数量
  402. ERRI_OUT_OF_MEMORY,//内存溢出,缓冲区已满
  403. ERRR_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH, //连接的网络地址无效
  404. ERRR_CONN_CONFIG_FAILED_NO_RESPONSE, //连接无响应
  405. ERRR_CONN_CONFIG_FAILED_ERROR_RESPONSE,//连接响应错误
  406. ERRR_INVALID_DESTINATION, //目标标签不存在
  407. ERRR_TAGNAME_TOO_LONG, //标签名超过255字节
  408. ERRR_REQUEST_DATA_TOO_LARGE, //请求数据超限
  409. ERRR_CONN_CONNECTION_TIMED_OUT, //活动连接响应超时,请检查目标IP是否离线
  410. ERRR_TAGNAME_CONVERT_FAILED, //标签名解析错误
  411. ERRR_WRITE_DATASIZE_UNCONSISTENT, //数据长度与标签实际长度不一致
  412. ERRR_SCAN_ERROR, //扫描标签信息失败
  413. };
  414. public enum TAG_TYPE : int
  415. {
  416. TAG_TYPE_UNDEFINE = -1,
  417. TAG_TYPE_BOOL = 0xC1, //新增
  418. TAG_TYPE_SINT = 0xC2,
  419. TAG_TYPE_INT = 0xC3,
  420. TAG_TYPE_DINT = 0xC4,
  421. TAG_TYPE_LINT = 0xC5,
  422. TAG_TYPE_USINT = 0xC6,
  423. TAG_TYPE_UINT = 0xC7,
  424. TAG_TYPE_UDINT = 0xC8,
  425. TAG_TYPE_ULINT = 0xC9,
  426. TAG_TYPE_REAL = 0xCA,
  427. TAG_TYPE_LREAL = 0xCB,
  428. TAG_TYPE_STRING = 0xD0, //新增
  429. TAG_TYPE_BYTE = 0xD1,
  430. TAG_TYPE_WORD = 0xD2,
  431. TAG_TYPE_DWORD = 0xD3,
  432. TAG_TYPE_LWORD = 0xD4,
  433. TAG_TYPE_STRUCT = 0xA2, //新增
  434. TAG_TYPE_ARRAY = 0xA3
  435. };
  436. enum EtIPConnectionState : int
  437. {
  438. ConnectionNonExistent = 0x0, //该实例未有连接
  439. ConnectionConfiguring = 0x1, //连接正在打开过程中
  440. ConnectionEstablished = 0x3, //连接已成功建立并在活动中
  441. ConnectionTimedOut = 0x4, //连接超时
  442. ConnectionClosing = 0x6 //连接正在关闭中
  443. };
  444. public const uint INVALID_MEMBER = 0xffffffff; //Member is not valid flag should be used when no member should be specified in the UCMM
  445. public struct tagTagReadData
  446. {
  447. public string pName;
  448. public int nElementCount;
  449. public int nArrayPos;
  450. };
  451. public struct tagTagReadDataBase
  452. {
  453. public string pName;
  454. public int nElementCount;
  455. };
  456. public struct tagTagRetValue
  457. {
  458. public IntPtr pData;
  459. public TAG_TYPE pType;
  460. public int nDataLength;
  461. //tagTagRetValue()
  462. //{
  463. // pData = NULL;
  464. // pType = TAG_TYPE_UNDEFINE;
  465. // nDataLength = 0;
  466. //}
  467. //~tagTagRetValue()
  468. //{
  469. // if (pData)
  470. // {
  471. // delete pData;
  472. // pData = NULL;
  473. // }
  474. //}
  475. };
  476. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
  477. public struct tagTagWriteData
  478. {
  479. public string pName;
  480. public IntPtr pData;
  481. public TAG_TYPE pType;
  482. public int nArrayPos;
  483. public int nDataLength;
  484. public int nElementCount;
  485. };
  486. public struct tagTagWriteDataBase
  487. {
  488. public string pName;
  489. public IntPtr pData;
  490. public TAG_TYPE pType;
  491. public int nDataLength;
  492. public int nElementCount;
  493. };
  494. public class MySerializationBinder : SerializationBinder
  495. {
  496. public override Type BindToType(string assemblyName, string typeName)
  497. {
  498. // 如果类型名称是"MyNamespace.MyUnsupportedType",则将其转换为"MyNamespace.MySupportType"
  499. if (typeName == "MyNamespace.MyUnsupportedType")
  500. {
  501. return Type.GetType("MyNamespace.MySupportType");
  502. }
  503. // 否则返回null,表示无法进行转换
  504. return null;
  505. }
  506. }
  507. #endregion 自定义的结构
  508. #region 动态类型
  509. public class DynamicMethodProvider
  510. {
  511. public dynamic GetDynamicValue(string propertyName)
  512. {
  513. dynamic result;
  514. switch (propertyName)
  515. {
  516. case "Name":
  517. result = "John Doe";
  518. break;
  519. case "Age":
  520. result = 30;
  521. break;
  522. default:
  523. result = new ExpandoObject();
  524. ((IDictionary<string, object>)result).Add("UnknownProperty", "UnknownValue");
  525. break;
  526. }
  527. return result;
  528. }
  529. }
  530. public static T CreateElement<T>()
  531. {
  532. Type t = typeof(T);
  533. return (T)t.Assembly.CreateInstance(t.FullName);
  534. }
  535. public static dynamic CreateElement(string typename)
  536. {
  537. Type t = GetTypeByName(typename);
  538. return t.Assembly.CreateInstance(t.FullName);
  539. }
  540. public static Type GetTypeByName(string typename)
  541. {
  542. Type t = null;
  543. string source = typename;
  544. try
  545. {
  546. t = Type.GetType(source);
  547. if (t != null)
  548. {
  549. return t;
  550. }
  551. Assembly[] assembly = AppDomain.CurrentDomain.GetAssemblies();
  552. foreach (Assembly ass in assembly)
  553. {
  554. t = ass.GetType(source);
  555. if (t != null)
  556. {
  557. return t;
  558. }
  559. Type[] ts = ass.GetTypes();
  560. foreach (Type st in ts)
  561. {
  562. if (Regex.IsMatch(st.FullName, @"\." + source + @"(`?\d+)?$"))
  563. {
  564. return st;
  565. }
  566. }
  567. }
  568. }
  569. catch (Exception ex)
  570. {
  571. }
  572. return t;
  573. }
  574. public static Type GetTypeByName2(string typename)
  575. {
  576. Type t = null;
  577. string source = typename;
  578. if (source.IndexOf('<') > 0)
  579. {
  580. List<string> lv = new List<string>();
  581. while (Regex.IsMatch(source, @"<[^<>]+>"))
  582. {
  583. lv.Add(Regex.Match(source, @"(?<=<)[^<>]+(?=>)").Value);
  584. source = Regex.Replace(source, @"<[^<>]+>", "/" + (lv.Count - 1));
  585. }
  586. List<Type[]> args = new List<Type[]>();
  587. for (int i = 0; i < lv.Count; i++)
  588. {
  589. List<Type> arg = new List<Type>();
  590. string[] sp = lv[i].Split(',');
  591. for (int j = 0; j < sp.Length; j++)
  592. {
  593. string s = sp[j].Trim();
  594. if (!string.IsNullOrEmpty(s))
  595. {
  596. if (Regex.IsMatch(s, @"/\d+$"))
  597. {
  598. Match m = Regex.Match(s, @"^([^/\s]+)\s*/(\d+)$");
  599. if (!m.Success)
  600. {
  601. throw new Exception("");
  602. }
  603. Type p = GetTypeByName(m.Groups[1].Value);
  604. Type c = p.MakeGenericType(args[Convert.ToInt32(m.Groups[2].Value)]);
  605. arg.Add(c);
  606. }
  607. else
  608. {
  609. arg.Add(GetTypeByName(s));
  610. }
  611. }
  612. }
  613. args.Add(arg.ToArray());
  614. }
  615. Match f = Regex.Match(source, @"^([^/\s]+)\s*/(\d+)$");
  616. if (!f.Success)
  617. {
  618. throw new Exception("");
  619. }
  620. Type fp = GetTypeByName(f.Groups[1].Value);
  621. Type fc = fp.MakeGenericType(args[Convert.ToInt32(f.Groups[2].Value)]);
  622. return fc;
  623. }
  624. else
  625. {
  626. try
  627. {
  628. t = Type.GetType(source);
  629. if (t != null)
  630. {
  631. return t;
  632. }
  633. Assembly[] assembly = AppDomain.CurrentDomain.GetAssemblies();
  634. foreach (Assembly ass in assembly)
  635. {
  636. t = ass.GetType(source);
  637. if (t != null)
  638. {
  639. return t;
  640. }
  641. Type[] ts = ass.GetTypes();
  642. foreach (Type st in ts)
  643. {
  644. //if (Regex.IsMatch(st.FullName, @"\." + Regex.FormatRegEx(source) + @"(`?\d+)?$"))
  645. if (Regex.IsMatch(st.FullName, @"\." + source + @"(`?\d+)?$"))
  646. {
  647. return st;
  648. }
  649. }
  650. }
  651. }
  652. catch (Exception ex)
  653. {
  654. }
  655. }
  656. return t;
  657. }
  658. #endregion 动态类型
  659. #region DLL
  660. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  661. public static extern void EipStart();
  662. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  663. public static extern void EipStop();
  664. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  665. public static extern int EipOpenConnection(string ipAddress, IntPtr instanceID);
  666. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  667. public static extern int EipCloseConnection(int nID);
  668. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  669. public static extern int EipReadTag(int instanceID, string tagName, IntPtr type, byte[] dest, int dataLength, ushort elementCount = 1, uint nPos = INVALID_MEMBER);
  670. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  671. public static extern int EipWriteTag(int instanceID, string tagName, int type, byte[] source, int dataLength, ushort elementCount = 1, uint nPos = INVALID_MEMBER/*, uint strLen = 0*/);
  672. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  673. public static extern int EipGetConnectionState(int nID);
  674. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  675. public static extern bool EipStartExt(string ipAddress, uint nPort = 0);
  676. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  677. public static extern int EipReadTagList(int instanceID, int nNumOfTags, tagTagReadData[] pTagList, ref tagTagRetValue pdest);
  678. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  679. public static extern ERROR_NO EipWriteTagList(int instanceID, int nNumOfTags, tagTagWriteData[] pTagWritenData);
  680. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  681. public static extern bool DeleteTagListStru(ref tagTagRetValue pRetValue, int nNumOfTags);
  682. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  683. public static extern int EipReadTagExt(int instanceID, string tagName, IntPtr type, byte[] dest, int dataLength, ushort elementCount = 1);
  684. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  685. public static extern int EipWriteTagExt(int instanceID, string tagName, int type, byte[] source, int dataLength, ushort elementCount = 1);
  686. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  687. public static extern int EipReadTagListExt(int instanceID, int nNumOfTags, tagTagReadDataBase[] pTagList, ref tagTagRetValue pdest, bool bScan = false);
  688. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  689. public static extern ERROR_NO EipWriteTagListExt(int instanceID, int nNumOfTags, tagTagWriteDataBase[] pTagWritenData, bool bScan = false);
  690. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  691. public static extern ERROR_NO EipReadTagExt2(int iInstanceID, tagTagReadDataBase[] pTagList, ref tagTagRetValue pDest);
  692. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  693. public static extern ERROR_NO EipWriteTagExt2(int iInstanceID, tagTagWriteDataBase[] pTagWritenData);
  694. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  695. public static extern void ResetTagInfo();
  696. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  697. public static extern ERROR_NO EipReadTagRaw(int iInstanceID, tagTagReadDataBase[] pTagList, ref tagTagRetValue pDest);
  698. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  699. public static extern ERROR_NO EipWriteTagRaw(int iInstanceID, tagTagWriteDataBase[] pTagWritenData);
  700. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  701. public static extern int EipReadTagListRaw(int instanceID, int nNumOfTags, tagTagReadDataBase[] pTagList, ref tagTagRetValue pdest);
  702. [DllImport("EipTagSimple.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  703. public static extern ERROR_NO EipWriteTagListRaw(int instanceID, int nNumOfTags, tagTagWriteDataBase[] pTagWritenData);
  704. #endregion DLL
  705. #region private方法
  706. private string getTypeString(TAG_TYPE eType)
  707. {
  708. string strType = "";
  709. switch (eType)
  710. {
  711. case TAG_TYPE.TAG_TYPE_SINT:
  712. strType = "SINT";
  713. break;
  714. case TAG_TYPE.TAG_TYPE_INT:
  715. strType = "INT";
  716. break;
  717. case TAG_TYPE.TAG_TYPE_DINT:
  718. strType = "DINT";
  719. break;
  720. case TAG_TYPE.TAG_TYPE_LINT:
  721. strType = "LINT";
  722. break;
  723. case TAG_TYPE.TAG_TYPE_USINT:
  724. strType = "USINT";
  725. break;
  726. case TAG_TYPE.TAG_TYPE_UINT:
  727. strType = "UINT";
  728. break;
  729. case TAG_TYPE.TAG_TYPE_UDINT:
  730. strType = "UDINT";
  731. break;
  732. case TAG_TYPE.TAG_TYPE_ULINT:
  733. strType = "ULINT";
  734. break;
  735. case TAG_TYPE.TAG_TYPE_REAL:
  736. strType = "REAL";
  737. break;
  738. case TAG_TYPE.TAG_TYPE_LREAL:
  739. strType = "LREAL";
  740. break;
  741. case TAG_TYPE.TAG_TYPE_BYTE:
  742. strType = "BYTE";
  743. break;
  744. case TAG_TYPE.TAG_TYPE_WORD:
  745. strType = "WORD";
  746. break;
  747. case TAG_TYPE.TAG_TYPE_DWORD:
  748. strType = "DWORD";
  749. break;
  750. case TAG_TYPE.TAG_TYPE_LWORD:
  751. strType = "LWORD";
  752. break;
  753. case TAG_TYPE.TAG_TYPE_BOOL:
  754. strType = "BOOL";
  755. break;
  756. case TAG_TYPE.TAG_TYPE_STRING:
  757. strType = "STRING";
  758. break;
  759. case TAG_TYPE.TAG_TYPE_STRUCT:
  760. strType = "STRUCT";
  761. break;
  762. default:
  763. break;
  764. }
  765. return strType;
  766. }
  767. private byte[] StringToBytes(string s)
  768. {
  769. string[] str = s.Split(' ');
  770. int n = str.Length;
  771. byte[] cmdBytes = null;
  772. int p = 0;
  773. for (int k = 0; k < n; k++)
  774. {
  775. int sLen = str[k].Length;
  776. int bytesLen = sLen / 2;
  777. int position = 0;
  778. byte[] bytes = new byte[bytesLen];
  779. for (int i = 0; i < bytesLen; i++)
  780. {
  781. string abyte = str[k].Substring(position, 2);
  782. bytes[i] = Convert.ToByte(abyte, 16);
  783. position += 2;
  784. }
  785. if (position >= 2)
  786. {
  787. byte[] cmdBytes2 = new byte[p + bytesLen];
  788. if (cmdBytes != null)
  789. {
  790. Array.Copy(cmdBytes, 0, cmdBytes2, 0, p);
  791. }
  792. Array.Copy(bytes, 0, cmdBytes2, p, bytesLen);
  793. cmdBytes = cmdBytes2;
  794. p += bytesLen;
  795. }
  796. }
  797. return cmdBytes;
  798. }
  799. private (int,string) OpenEip(string strLocalComputerIp)
  800. {
  801. //limit input of controller
  802. bool blnTest = false;
  803. bool bValidIP = true;
  804. Regex regex = new Regex("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$");
  805. blnTest = regex.IsMatch(strLocalComputerIp);
  806. if (blnTest == true)
  807. {
  808. string[] strTemp = strLocalComputerIp.Split(new char[] { '.' });
  809. if (strTemp.Length < 4)
  810. {
  811. bValidIP = false;
  812. return (-1, "不符合IP格式");
  813. }
  814. for (int i = 0; (i < strTemp.Length) && bValidIP; i++)
  815. {
  816. if (Convert.ToInt32(strTemp[i]) > 255)
  817. {
  818. //大于255则提示,不符合IP格式
  819. bValidIP = false;
  820. return (-2, "IP大于255,不符合IP格式");
  821. }
  822. }
  823. }
  824. else
  825. {
  826. //输入非数字则提示,不符合IP格式
  827. bValidIP = false;
  828. return (-3, "输入非数字,不符合IP格式");
  829. }
  830. if (bValidIP)
  831. {
  832. if (!isStart)
  833. {
  834. if (EipStartExt(strLocalComputerIp, 0))
  835. {
  836. isStart = true;
  837. strClaimedComputerIP = strLocalComputerIp;
  838. return (0, "EIP协议栈开启成功");
  839. }
  840. else
  841. {
  842. isStart = false;
  843. return (1, "EIP协议栈开启失败");
  844. }
  845. }
  846. else
  847. {
  848. if (string.Compare(strClaimedComputerIP, strLocalComputerIp) != 0)
  849. {
  850. return (2,"更改上位机IP时需先关闭协议栈,再开启");
  851. }
  852. else
  853. {
  854. return (3, "EIP协议栈已经开启");
  855. }
  856. }
  857. }
  858. return (-4, "无效IP");
  859. }
  860. private (bool,string) CloseEip()
  861. {
  862. if (isStart)
  863. {
  864. EipStop();
  865. isStart = false;
  866. return (true, "EIP协议栈关闭");
  867. }
  868. else
  869. {
  870. return (false, "EIP协议栈未开启,请先开启");
  871. }
  872. }
  873. #endregion private方法
  874. #region 连接/断开
  875. #region 连接方式 一
  876. public Inovance_EIP() { }
  877. /// <summary>
  878. /// 连接PLC
  879. /// </summary>
  880. /// <param name="strComputerIp">PC的IP地址</param>
  881. /// <param name="strPlcIp">PLC的IP地址</param>
  882. /// <returns>成功:True 失败:False </returns>
  883. public (int,string) Connect(string strComputerIp,string strPlcIp)
  884. {
  885. _pcIPStr = strComputerIp;
  886. _plcIPStr = strPlcIp;
  887. lock (m_objLock)
  888. {
  889. string strRet = "";
  890. int nRet = 0;
  891. if (!isStart) (nRet, strRet) = OpenEip(strComputerIp);
  892. if (nRet != 0) return (nRet, strRet);
  893. bool blnTest = false;
  894. bool bValidIP = true;
  895. Regex regex = new Regex("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$");
  896. blnTest = regex.IsMatch(strPlcIp);
  897. if (blnTest == true)
  898. {
  899. string[] strTemp = strPlcIp.Split(new char[] { '.' });
  900. for (int i = 0; i < strTemp.Length; i++)
  901. {
  902. if (Convert.ToInt32(strTemp[i]) > 255)
  903. {
  904. bValidIP = false;
  905. return (-10, "PLC IP 大于255,不符合IP格式");
  906. }
  907. }
  908. }
  909. else
  910. {
  911. bValidIP = false;
  912. return (-11, "PLC IP输入非数字,不符合IP格式");
  913. }
  914. if (bValidIP)
  915. {
  916. if (strPlcIp.CompareTo(strClaimedComputerIP) == 0)
  917. {
  918. string strLog = "";
  919. strLog += "上位机IP与PLC IP相同,请重新输入";
  920. return (-20, strLog);
  921. }
  922. if (strPlcIp.CompareTo(strComputerIp) == 0)
  923. {
  924. string strLog = "";
  925. strLog += "上位机IP与PLC IP相同,请重新输入";
  926. return (-21, strLog);
  927. }
  928. int instanceId = 0;
  929. ERROR_NO errorNo;
  930. unsafe
  931. {
  932. errorNo = (ERROR_NO)EipOpenConnection(strPlcIp, (IntPtr)(&instanceId));
  933. }
  934. m_nInstanceId = instanceId;
  935. string str = "";
  936. str += "\n请求创建PLC连接 ip:";
  937. str += strPlcIp;
  938. str += " ";
  939. if (errorNo != ERROR_NO.SUCCESS)
  940. {
  941. m_bConnected = false;
  942. str += ("失败 ");
  943. switch (errorNo)
  944. {
  945. case ERROR_NO.ERR_EIP_STOPED:
  946. str += ("协议栈未开启");
  947. break;
  948. case ERROR_NO.ERRI_INVALID_CONNECTION_INSTANCE_SPECIFIED:
  949. str += ("连接的实例ID与已有的ID重复或超过最大值");
  950. break;
  951. case ERROR_NO.ERRI_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
  952. str += ("连接的网络路径格式错误,无法检测出来目标IP离线等错误");
  953. break;
  954. case ERROR_NO.ERRI_CONNECTION_COUNT_LIMIT_REACHED:
  955. str += ("达到最大连接数量");
  956. break;
  957. case ERROR_NO.ERRI_OUT_OF_MEMORY:
  958. str += ("内存溢出,缓冲区已满");
  959. break;
  960. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
  961. str += ("连接的网络地址无效");
  962. break;
  963. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_NO_RESPONSE:
  964. str += ("连接无响应");
  965. break;
  966. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_ERROR_RESPONSE:
  967. str += ("连接响应错误");
  968. break;
  969. default:
  970. str += ("其他错误");
  971. break;
  972. }
  973. }
  974. else
  975. {
  976. str += "成功";
  977. str += " 分配的实例ID 为 ";
  978. str += instanceId.ToString();
  979. m_bConnected = true;
  980. return (0, str);
  981. }
  982. str += "\n";
  983. return (100 + (int)errorNo, str);
  984. }
  985. }
  986. return (1000, "Error");
  987. }
  988. #endregion 连接方式 一
  989. #region 连接方式 二
  990. /// <summary>
  991. ///
  992. /// </summary>
  993. /// <param name="strComputerIp">PC的IP地址</param>
  994. /// <param name="strPlcIp">PLC的IP地址</param>
  995. public Inovance_EIP(string strComputerIp, string strPlcIp)
  996. {
  997. _pcIPStr = strComputerIp;
  998. _plcIPStr = strPlcIp;
  999. }
  1000. /// <summary>
  1001. /// 连接PLC
  1002. /// </summary>
  1003. /// <returns>成功:True 失败:False </returns>
  1004. public (int, string) Connect()
  1005. {
  1006. lock (m_objLock)
  1007. {
  1008. string strRet = "";
  1009. int nRet = 0;
  1010. if (!isStart) (nRet, strRet) = OpenEip(_pcIPStr);
  1011. if (nRet != 0) return (nRet, strRet);
  1012. bool blnTest = false;
  1013. bool bValidIP = true;
  1014. Regex regex = new Regex("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$");
  1015. blnTest = regex.IsMatch(_plcIPStr);
  1016. if (blnTest == true)
  1017. {
  1018. string[] strTemp = _plcIPStr.Split(new char[] { '.' });
  1019. for (int i = 0; i < strTemp.Length; i++)
  1020. {
  1021. if (Convert.ToInt32(strTemp[i]) > 255)
  1022. {
  1023. bValidIP = false;
  1024. return (-10, "PLC IP 大于255,不符合IP格式");
  1025. }
  1026. }
  1027. }
  1028. else
  1029. {
  1030. bValidIP = false;
  1031. return (-11, "PLC IP输入非数字,不符合IP格式");
  1032. }
  1033. if (bValidIP)
  1034. {
  1035. if (_plcIPStr.CompareTo(strClaimedComputerIP) == 0)
  1036. {
  1037. string strLog = "";
  1038. strLog += "上位机IP与PLC IP相同,请重新输入";
  1039. return (-20, strLog);
  1040. }
  1041. if (_plcIPStr.CompareTo(_pcIPStr) == 0)
  1042. {
  1043. string strLog = "";
  1044. strLog += "上位机IP与PLC IP相同,请重新输入";
  1045. return (-21, strLog);
  1046. }
  1047. int instanceId = 0;
  1048. ERROR_NO errorNo;
  1049. unsafe
  1050. {
  1051. errorNo = (ERROR_NO)EipOpenConnection(_plcIPStr, (IntPtr)(&instanceId));
  1052. }
  1053. m_nInstanceId = instanceId;
  1054. string str = "";
  1055. str += "\n请求创建PLC连接 ip:";
  1056. str += _plcIPStr;
  1057. str += " ";
  1058. if (errorNo != ERROR_NO.SUCCESS)
  1059. {
  1060. m_bConnected = false;
  1061. str += ("失败 ");
  1062. switch (errorNo)
  1063. {
  1064. case ERROR_NO.ERR_EIP_STOPED:
  1065. str += ("协议栈未开启");
  1066. break;
  1067. case ERROR_NO.ERRI_INVALID_CONNECTION_INSTANCE_SPECIFIED:
  1068. str += ("连接的实例ID与已有的ID重复或超过最大值");
  1069. break;
  1070. case ERROR_NO.ERRI_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
  1071. str += ("连接的网络路径格式错误,无法检测出来目标IP离线等错误");
  1072. break;
  1073. case ERROR_NO.ERRI_CONNECTION_COUNT_LIMIT_REACHED:
  1074. str += ("达到最大连接数量");
  1075. break;
  1076. case ERROR_NO.ERRI_OUT_OF_MEMORY:
  1077. str += ("内存溢出,缓冲区已满");
  1078. break;
  1079. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
  1080. str += ("连接的网络地址无效");
  1081. break;
  1082. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_NO_RESPONSE:
  1083. str += ("连接无响应");
  1084. break;
  1085. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_ERROR_RESPONSE:
  1086. str += ("连接响应错误");
  1087. break;
  1088. default:
  1089. str += ("其他错误");
  1090. break;
  1091. }
  1092. }
  1093. else
  1094. {
  1095. str += "成功";
  1096. str += " 分配的实例ID 为 ";
  1097. str += instanceId.ToString();
  1098. m_bConnected = true;
  1099. return (0, str);
  1100. }
  1101. str += "\n";
  1102. return (100 + (int)errorNo, str);
  1103. }
  1104. }
  1105. return (1000, "Error");
  1106. }
  1107. #endregion 连接方式 二
  1108. /// <summary>
  1109. /// 断开连接
  1110. /// </summary>
  1111. /// <returns>成功:True 失败:False</returns>
  1112. public bool Disconnect()
  1113. {
  1114. lock (m_objLock)
  1115. {
  1116. if (m_nInstanceId == 0 || !m_bConnected)
  1117. {
  1118. m_nInstanceId = 0;
  1119. m_bConnected = false;
  1120. return true;
  1121. }
  1122. ERROR_NO errorNo = (ERROR_NO)EipCloseConnection(m_nInstanceId);
  1123. //string str = "";
  1124. if (errorNo != ERROR_NO.SUCCESS)
  1125. {
  1126. //str += "失败";
  1127. m_bConnected = false;
  1128. m_nInstanceId = 0;
  1129. return false;
  1130. }
  1131. else
  1132. {
  1133. //str += "成功";
  1134. m_bConnected = false;
  1135. m_nInstanceId = 0;
  1136. CloseEip();
  1137. return true;
  1138. }
  1139. }
  1140. }
  1141. #endregion 连接/断开
  1142. public (int,string) Read_Tag(string strTagName, int nCount, out byte[] pBuf)
  1143. {
  1144. pBuf = null;
  1145. string strType = "";
  1146. lock (m_objLock)
  1147. {
  1148. if (!m_bConnected) return (-1,"未连接");
  1149. string tagName = strTagName;
  1150. byte[] tagNameUTF8 = Encoding.UTF8.GetBytes(tagName);
  1151. if (m_nInstanceId <= 0)
  1152. {
  1153. return (-2, "实例ID需大于0");
  1154. }
  1155. if (tagNameUTF8.Length > 255)
  1156. {
  1157. return (-3, "标签名长度超过255字节");
  1158. }
  1159. ushort elementCount = (ushort)nCount;
  1160. if (elementCount > 0)
  1161. {
  1162. int destLength = 1400;
  1163. byte[] dest = new byte[destLength];
  1164. char[] cChar = Encoding.ASCII.GetChars(dest);
  1165. //TAG_TYPE type = TAG_TYPE.TAG_TYPE_UNDEFINE;
  1166. //int returnLength = 0;
  1167. tagTagReadDataBase[] pTaglist = new tagTagReadDataBase[1];
  1168. pTaglist[0].pName = tagName;
  1169. pTaglist[0].nElementCount = elementCount;
  1170. tagTagRetValue[] tagValue = new tagTagRetValue[1];
  1171. ERROR_NO errorNo = ERROR_NO.OTHER_ERROR;
  1172. unsafe
  1173. {
  1174. errorNo = EipReadTagExt2(m_nInstanceId, pTaglist, ref tagValue[0]);
  1175. }
  1176. string strLog = "";
  1177. strLog += "读取请求";
  1178. if (/*returnLength < 0*/errorNo != ERROR_NO.SUCCESS)
  1179. {
  1180. strLog += " 失败 标签名:" + tagName + " 实例ID:" + m_nInstanceId.ToString() + " ";
  1181. switch (/*returnLength*/errorNo)
  1182. {
  1183. //case -1:
  1184. // strLog += ("其他错误");
  1185. // break;
  1186. //case -2:
  1187. // strLog += ("协议栈未开启");
  1188. // break;
  1189. //case -3:
  1190. // strLog += ("实例id小于或等于0");
  1191. // break;
  1192. //case -4:
  1193. // strLog += ("标签名长度大于255字节");
  1194. // break;
  1195. //case -5:
  1196. // strLog += ("目标标签不存在");
  1197. // break;
  1198. //case -6:
  1199. // strLog += ("响应超时,请检查设备是否离线");
  1200. // break;
  1201. //case -7:
  1202. // strLog += ("标签名解析错误");
  1203. // break;
  1204. //case -8:
  1205. // strLog += ("扫描标签信息失败");
  1206. // break;
  1207. //default:
  1208. // strLog += ("其他错误");
  1209. // break;
  1210. case ERROR_NO.ERR_EIP_STOPED:
  1211. strLog += ("协议栈未开启");
  1212. break;
  1213. case ERROR_NO.ERRI_INVALID_CONNECTION_INSTANCE_SPECIFIED:
  1214. strLog += ("连接的实例ID与已有的ID重复或超过最大值");
  1215. break;
  1216. case ERROR_NO.ERRI_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
  1217. strLog += ("连接的网络路径格式错误,无法检测出来目标IP离线等错误");
  1218. break;
  1219. case ERROR_NO.ERRI_CONNECTION_COUNT_LIMIT_REACHED:
  1220. strLog += ("达到最大连接数量");
  1221. break;
  1222. case ERROR_NO.ERRI_OUT_OF_MEMORY:
  1223. strLog += ("内存溢出,缓冲区已满");
  1224. break;
  1225. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
  1226. strLog += ("连接的网络地址无效");
  1227. break;
  1228. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_NO_RESPONSE:
  1229. strLog += ("连接无响应");
  1230. break;
  1231. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_ERROR_RESPONSE:
  1232. strLog += ("连接响应错误");
  1233. break;
  1234. case ERROR_NO.ERRR_INVALID_DESTINATION:
  1235. strLog += ("目标标签不存在");
  1236. break;
  1237. case ERROR_NO.ERRR_TAGNAME_TOO_LONG:
  1238. strLog += ("标签名超过255字节");
  1239. break;
  1240. case ERROR_NO.ERRR_REQUEST_DATA_TOO_LARGE:
  1241. strLog += ("请求数据超限");
  1242. break;
  1243. case ERROR_NO.ERRR_CONN_CONNECTION_TIMED_OUT:
  1244. strLog += ("响应超时,请检查设备是否离线");
  1245. break;
  1246. case ERROR_NO.ERRR_TAGNAME_CONVERT_FAILED:
  1247. strLog += ("标签名解析错误");
  1248. break;
  1249. case ERROR_NO.ERRR_SCAN_ERROR:
  1250. strLog += ("扫描标签信息失败");
  1251. break;
  1252. default:
  1253. strLog += ("其他错误");
  1254. break;
  1255. }
  1256. strLog += "\n";
  1257. return (10,strLog);
  1258. }
  1259. else
  1260. {
  1261. switch (/*type*/tagValue[0].pType)
  1262. {
  1263. case TAG_TYPE.TAG_TYPE_BOOL:
  1264. strType += "Boolean";
  1265. break;
  1266. case TAG_TYPE.TAG_TYPE_SINT:
  1267. strType += "SByte";
  1268. break;
  1269. case TAG_TYPE.TAG_TYPE_INT:
  1270. strType += "Int16";
  1271. break;
  1272. case TAG_TYPE.TAG_TYPE_DINT:
  1273. strType += "Int32";
  1274. break;
  1275. case TAG_TYPE.TAG_TYPE_LINT:
  1276. strType += "Int64";
  1277. break;
  1278. case TAG_TYPE.TAG_TYPE_USINT:
  1279. strType += "Byte";
  1280. break;
  1281. case TAG_TYPE.TAG_TYPE_UINT:
  1282. strType += "UInt16";
  1283. break;
  1284. case TAG_TYPE.TAG_TYPE_UDINT:
  1285. strType += "UInt32";
  1286. break;
  1287. case TAG_TYPE.TAG_TYPE_ULINT:
  1288. strType += "UInt64";
  1289. break;
  1290. case TAG_TYPE.TAG_TYPE_REAL:
  1291. strType += "Single";
  1292. break;
  1293. case TAG_TYPE.TAG_TYPE_LREAL:
  1294. strType += "Double";
  1295. break;
  1296. case TAG_TYPE.TAG_TYPE_STRING:
  1297. strType += "String";
  1298. break;
  1299. case TAG_TYPE.TAG_TYPE_BYTE:
  1300. strType += "Byte";
  1301. break;
  1302. case TAG_TYPE.TAG_TYPE_WORD:
  1303. strType += "UInt16";
  1304. break;
  1305. case TAG_TYPE.TAG_TYPE_DWORD:
  1306. strType += "UInt32";
  1307. break;
  1308. case TAG_TYPE.TAG_TYPE_LWORD:
  1309. strType += "UInt64";
  1310. break;
  1311. case TAG_TYPE.TAG_TYPE_STRUCT:
  1312. strType += "STRUCT";
  1313. break;
  1314. case TAG_TYPE.TAG_TYPE_ARRAY:
  1315. strType += "ARRAY";
  1316. break;
  1317. default:
  1318. break;
  1319. }
  1320. pBuf = new byte[tagValue[0].nDataLength];
  1321. unsafe
  1322. {
  1323. byte* memBytePtr = (byte*)tagValue[0].pData.ToPointer();
  1324. for (int j = 0; j < tagValue[0].nDataLength; j++)
  1325. {
  1326. pBuf[j] = memBytePtr[j];
  1327. //string str = "";
  1328. //str += j.ToString() + ": ";
  1329. //str += "0x" + String.Format("{0:X2}", memBytePtr[j]) + "\n";
  1330. }
  1331. }
  1332. DeleteTagListStru(ref tagValue[0], 1); //调用接口释放内存
  1333. }
  1334. }
  1335. else
  1336. {
  1337. return (-10, "元素个数参数错误,必须>0");
  1338. }
  1339. return (0, strType);
  1340. }
  1341. }
  1342. public (int, string) Write_Tag(string strTagName, int nCount, byte[] pBuf)
  1343. {
  1344. lock (m_objLock)
  1345. {
  1346. if (!m_bConnected) return (-1, "未连接");
  1347. string tagName = strTagName;
  1348. byte[] tagNameUTF8 = Encoding.UTF8.GetBytes(tagName);
  1349. if (m_nInstanceId <= 0)
  1350. {
  1351. return (-2, "实例ID需大于0");
  1352. }
  1353. if (tagNameUTF8.Length > 255)
  1354. {
  1355. return (-3, "标签名长度超过255字节");
  1356. }
  1357. ushort elementCount = (ushort)nCount;
  1358. if ((pBuf.Length < 1) || (elementCount < 1))
  1359. {
  1360. return (-4, "请求个数必须大于0,请重新输入");
  1361. }
  1362. if (pBuf.Length > 1400)
  1363. {
  1364. return (-5, "标签数据长度超过1400字节,请重新输入");
  1365. }
  1366. int dataLength = pBuf.Length;
  1367. tagTagWriteDataBase[] pTaglist = new tagTagWriteDataBase[1];
  1368. //标签1属性
  1369. pTaglist[0].pName = tagName;
  1370. pTaglist[0].nElementCount = elementCount;
  1371. pTaglist[0].pType = TAG_TYPE.TAG_TYPE_UNDEFINE;
  1372. pTaglist[0].pData = Marshal.AllocHGlobal(1400);
  1373. pTaglist[0].nDataLength = dataLength;
  1374. byte temp = 0;
  1375. for (int i = 0; i < dataLength; ++i)
  1376. {
  1377. temp = pBuf[i];
  1378. Marshal.WriteByte(pTaglist[0].pData + i * sizeof(byte), temp);
  1379. }
  1380. //ERROR_NO errorNo = (ERROR_NO)EipWriteTag(m_nInstanceId, tagName, (int)type, source, dataLength, elementCount, arrayPos/*, strLen*/);
  1381. //ERROR_NO errorNo = (ERROR_NO)EipWriteTagExt(m_nInstanceId, tagName, (int)type, source, dataLength, elementCount);
  1382. ERROR_NO errorNo = EipWriteTagExt2(m_nInstanceId, pTaglist);
  1383. //ERROR_NO errorNo = EipWriteTagRaw(instanceId, pTaglist);
  1384. string strLog = "";
  1385. strLog += "\n写入请求 ";
  1386. if (errorNo != ERROR_NO.SUCCESS)
  1387. {
  1388. strLog += " 失败 标签名:" + tagName + " 实例ID:" + m_nInstanceId.ToString() + " ";
  1389. switch (errorNo)
  1390. {
  1391. case ERROR_NO.ERR_EIP_STOPED:
  1392. strLog += ("协议栈未开启");
  1393. break;
  1394. case ERROR_NO.ERRI_INVALID_CONNECTION_INSTANCE_SPECIFIED:
  1395. strLog += ("连接的实例ID与已有的ID重复或超过最大值");
  1396. break;
  1397. case ERROR_NO.ERRI_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
  1398. strLog += ("连接的网络路径格式错误,无法检测出来目标IP离线等错误");
  1399. break;
  1400. case ERROR_NO.ERRI_CONNECTION_COUNT_LIMIT_REACHED:
  1401. strLog += ("达到最大连接数量");
  1402. break;
  1403. case ERROR_NO.ERRI_OUT_OF_MEMORY:
  1404. strLog += ("内存溢出,缓冲区已满");
  1405. break;
  1406. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
  1407. strLog += ("连接的网络地址无效");
  1408. break;
  1409. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_NO_RESPONSE:
  1410. strLog += ("连接无响应");
  1411. break;
  1412. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_ERROR_RESPONSE:
  1413. strLog += ("连接响应错误");
  1414. break;
  1415. case ERROR_NO.ERRR_INVALID_DESTINATION:
  1416. strLog += ("目标标签不存在");
  1417. break;
  1418. case ERROR_NO.ERRR_TAGNAME_TOO_LONG:
  1419. strLog += ("标签名超过255字节");
  1420. break;
  1421. case ERROR_NO.ERRR_REQUEST_DATA_TOO_LARGE:
  1422. strLog += ("请求数据超限");
  1423. break;
  1424. case ERROR_NO.ERRR_CONN_CONNECTION_TIMED_OUT:
  1425. strLog += ("响应超时,请检查设备是否离线");
  1426. break;
  1427. case ERROR_NO.ERRR_TAGNAME_CONVERT_FAILED:
  1428. strLog += ("标签名解析错误");
  1429. break;
  1430. case ERROR_NO.ERRR_WRITE_DATASIZE_UNCONSISTENT:
  1431. strLog += ("写入数据长度与标签实际长度不一致");
  1432. break;
  1433. case ERROR_NO.ERRR_SCAN_ERROR:
  1434. strLog += ("扫描标签信息失败");
  1435. break;
  1436. default:
  1437. strLog += ("其他错误");
  1438. break;
  1439. }
  1440. strLog += "\n";
  1441. return (10, strLog);
  1442. }
  1443. else
  1444. {
  1445. return (0, "OK");
  1446. }
  1447. }
  1448. }
  1449. /// <summary>
  1450. /// 标签结构
  1451. /// </summary>
  1452. public struct StructTag
  1453. {
  1454. public string strTagName;
  1455. public Type tTagType;
  1456. public int nCount;
  1457. }
  1458. /// <summary>
  1459. /// 一次读取多个标签
  1460. /// </summary>
  1461. /// <param name="stTagList">多个标签的列表</param>
  1462. /// <param name="pBuf">多个标签的返回值</param>
  1463. /// <returns>函数执行结构值,非零异常,零成功</returns>
  1464. public (int, string) Read_Tags(List<StructTag> stTagList, out byte[][] pBuf)
  1465. {
  1466. pBuf = null;
  1467. string strType = "";
  1468. lock (m_objLock)
  1469. {
  1470. if (!m_bConnected) return (-1, "未连接");
  1471. if (m_nInstanceId <= 0)
  1472. {
  1473. return (-2, "实例ID需大于0");
  1474. }
  1475. if (stTagList == null) return (-3, "标签列表空");
  1476. int nNumOfTags = stTagList.Count;
  1477. if (nNumOfTags < 1) return (-4, "标签列表长度空");
  1478. for (int i = 0; i < nNumOfTags; i++)
  1479. {
  1480. byte[] tagNameUTF8 = Encoding.UTF8.GetBytes(stTagList[i].strTagName);
  1481. if (tagNameUTF8.Length > 255)
  1482. {
  1483. return (-5, "标签名长度超过255字节");
  1484. }
  1485. if (stTagList[i].nCount < 1) return (-6, "标签读取长度小于1");
  1486. }
  1487. tagTagReadDataBase[] pTaglist = new tagTagReadDataBase[nNumOfTags];
  1488. for (int i = 0; i < nNumOfTags; i++)
  1489. {
  1490. pTaglist[i].pName = stTagList[i].strTagName;
  1491. pTaglist[i].nElementCount = stTagList[i].nCount;
  1492. }
  1493. tagTagRetValue[] pTagsValue = new tagTagRetValue[nNumOfTags];
  1494. pBuf = new byte[nNumOfTags][];
  1495. if (pBuf == null) return (-7, "分配内存失败,可能内存不足");
  1496. //ERROR_NO errorNo = (ERROR_NO)EipReadTagList(m_nInstanceId, nNumOfTags, pTaglist, ref pTagsValue[0]);
  1497. ERROR_NO errorNo = (ERROR_NO)EipReadTagListExt(m_nInstanceId, nNumOfTags, pTaglist, ref pTagsValue[0], true);
  1498. //ERROR_NO errorNo = (ERROR_NO)EipReadTagListRaw(m_nInstanceId, nNumOfTags, pTaglist, ref pTagsValue[0]);
  1499. string strLog = "";
  1500. strLog += "\n读取请求";
  1501. if (errorNo != ERROR_NO.SUCCESS)
  1502. {
  1503. strLog += " 失败 标签个数:" + nNumOfTags.ToString() + " 实例ID:" + m_nInstanceId.ToString() + " ";
  1504. switch (errorNo)
  1505. {
  1506. case ERROR_NO.ERR_EIP_STOPED:
  1507. strLog += ("协议栈未开启");
  1508. break;
  1509. case ERROR_NO.ERRI_INVALID_CONNECTION_INSTANCE_SPECIFIED:
  1510. strLog += ("连接的实例ID与已有的ID重复或超过最大值");
  1511. break;
  1512. case ERROR_NO.ERRI_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
  1513. strLog += ("连接的网络路径格式错误,无法检测出来目标IP离线等错误");
  1514. break;
  1515. case ERROR_NO.ERRI_CONNECTION_COUNT_LIMIT_REACHED:
  1516. strLog += ("达到最大连接数量");
  1517. break;
  1518. case ERROR_NO.ERRI_OUT_OF_MEMORY:
  1519. strLog += ("内存溢出,缓冲区已满");
  1520. break;
  1521. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
  1522. strLog += ("连接的网络地址无效");
  1523. break;
  1524. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_NO_RESPONSE:
  1525. strLog += ("连接无响应");
  1526. break;
  1527. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_ERROR_RESPONSE:
  1528. strLog += ("连接响应错误");
  1529. break;
  1530. case ERROR_NO.ERRR_INVALID_DESTINATION:
  1531. strLog += ("目标标签不存在");
  1532. break;
  1533. case ERROR_NO.ERRR_TAGNAME_TOO_LONG:
  1534. strLog += ("标签名超过255字节");
  1535. break;
  1536. case ERROR_NO.ERRR_REQUEST_DATA_TOO_LARGE:
  1537. strLog += ("请求数据超限");
  1538. break;
  1539. case ERROR_NO.ERRR_CONN_CONNECTION_TIMED_OUT:
  1540. strLog += ("响应超时,请检查设备是否离线");
  1541. break;
  1542. case ERROR_NO.ERRR_TAGNAME_CONVERT_FAILED:
  1543. strLog += ("标签名解析错误");
  1544. break;
  1545. case ERROR_NO.ERRR_SCAN_ERROR:
  1546. strLog += ("扫描标签信息失败");
  1547. break;
  1548. default:
  1549. strLog += ("其他错误");
  1550. break;
  1551. }
  1552. strLog += "\n";
  1553. return (-10, strLog);
  1554. }
  1555. else
  1556. {
  1557. for (int i = 0; i < nNumOfTags; i++)
  1558. {
  1559. strType += "\n标签名:" + pTaglist[i].pName + " 类型:";
  1560. switch (pTagsValue[i].pType)
  1561. {
  1562. case TAG_TYPE.TAG_TYPE_BOOL:
  1563. strType += "BOOL";
  1564. break;
  1565. case TAG_TYPE.TAG_TYPE_SINT:
  1566. strType += "SINT";
  1567. break;
  1568. case TAG_TYPE.TAG_TYPE_INT:
  1569. strType += "INT";
  1570. break;
  1571. case TAG_TYPE.TAG_TYPE_DINT:
  1572. strType += "DINT";
  1573. break;
  1574. case TAG_TYPE.TAG_TYPE_LINT:
  1575. strType += "LINT";
  1576. break;
  1577. case TAG_TYPE.TAG_TYPE_USINT:
  1578. strType += "USINT";
  1579. break;
  1580. case TAG_TYPE.TAG_TYPE_UINT:
  1581. strType += "UINT";
  1582. break;
  1583. case TAG_TYPE.TAG_TYPE_UDINT:
  1584. strType += "UDINT";
  1585. break;
  1586. case TAG_TYPE.TAG_TYPE_ULINT:
  1587. strType += "ULINT";
  1588. break;
  1589. case TAG_TYPE.TAG_TYPE_REAL:
  1590. strType += "REAL";
  1591. break;
  1592. case TAG_TYPE.TAG_TYPE_LREAL:
  1593. strType += "LREAL";
  1594. break;
  1595. case TAG_TYPE.TAG_TYPE_STRING:
  1596. strType += "STRING";
  1597. break;
  1598. case TAG_TYPE.TAG_TYPE_BYTE:
  1599. strType += "BYTE";
  1600. break;
  1601. case TAG_TYPE.TAG_TYPE_WORD:
  1602. strType += "WORD";
  1603. break;
  1604. case TAG_TYPE.TAG_TYPE_DWORD:
  1605. strType += "DWORD";
  1606. break;
  1607. case TAG_TYPE.TAG_TYPE_LWORD:
  1608. strType += "LWORD";
  1609. break;
  1610. case TAG_TYPE.TAG_TYPE_STRUCT:
  1611. strType += "STRUCT";
  1612. break;
  1613. case TAG_TYPE.TAG_TYPE_ARRAY:
  1614. strType += "ARRAY";
  1615. break;
  1616. default:
  1617. strType += pTagsValue[i].pType.ToString();
  1618. break;
  1619. }
  1620. strType += " 元素个数:" + pTaglist[i].nElementCount.ToString();
  1621. strType += " 数据长度:" + pTagsValue[i].nDataLength.ToString() + "\n";
  1622. unsafe
  1623. {
  1624. byte* memBytePtr = (byte*)pTagsValue[i].pData.ToPointer();
  1625. pBuf[i] = new byte[pTagsValue[i].nDataLength];
  1626. for (int j = 0; j < pTagsValue[i].nDataLength; j++)
  1627. {
  1628. pBuf[i][j] = memBytePtr[j];
  1629. //string str = "";
  1630. ///str += j.ToString() + ": ";
  1631. //str += "0x" + String.Format("{0:X2}", memBytePtr[j]) + "\n";
  1632. }
  1633. }
  1634. }
  1635. }
  1636. DeleteTagListStru(ref pTagsValue[0], nNumOfTags); //调用接口释放内存
  1637. return (0, strType);
  1638. }
  1639. }
  1640. /// <summary>
  1641. /// 一次写多个标签
  1642. /// </summary>
  1643. /// <param name="TagList">将写入的标签列表</param>
  1644. /// <param name="ObjList">与标签列表对应的值对象</param>
  1645. /// <returns></returns>
  1646. public (int, string) Write_Tags(List<StructTag> TagList, List<Object> ObjList)
  1647. {
  1648. int size = 0;
  1649. byte[] pBuf = null;
  1650. int nRet = 0;
  1651. string strRet = "";
  1652. if (!m_bConnected) return (-1, "未连接");
  1653. if (m_nInstanceId <= 0) return (-2, "实例ID需大于0");
  1654. if (ObjList.Count != TagList.Count) return (-3, "标签列表和数据列表不一致");
  1655. int nLen = TagList.Count;
  1656. int nNumOfTags = TagList.Count;
  1657. tagTagWriteDataBase[] pTaglist = new tagTagWriteDataBase[nNumOfTags];
  1658. try
  1659. {
  1660. for (int i = 0; i < nLen; i++)
  1661. {
  1662. if (TagList[i].strTagName.Length > 255) return (-4, "标签名称长度超过255字节");
  1663. if (TagList[i].tTagType == null) return (-5, "标签类型不能空");
  1664. //标签属性
  1665. pTaglist[i].pName = TagList[i].strTagName;
  1666. pTaglist[i].nElementCount = TagList[i].nCount;
  1667. pTaglist[i].pType = TAG_TYPE.TAG_TYPE_UNDEFINE;
  1668. pTaglist[i].pData = Marshal.AllocHGlobal(1400);
  1669. //标签写入的数据
  1670. size = 0;
  1671. string str = TagList[i].tTagType.Name;
  1672. string[] substrings = { "Boolean", "SByte", "Byte", "Int16", "UInt16", "Int32", "UInt32", "Int64", "UInt64", "Single", "Double", "String" };
  1673. bool containsAny = substrings.Any(substring => str.Contains(substring));
  1674. if (containsAny)
  1675. {
  1676. Type type = ObjList[i].GetType();
  1677. if (str != type.Name) return (120, "变量类型不一致");
  1678. }
  1679. if (str == "String")
  1680. {
  1681. string data = ObjList[i] as string;
  1682. pBuf = System.Text.Encoding.Default.GetBytes(data);
  1683. size = pBuf.Length;
  1684. if (size > 1400)
  1685. {
  1686. return (110, "数据长度超过1400字节,请重新输入");
  1687. }
  1688. }
  1689. else
  1690. {
  1691. if (TagList[i].tTagType.Name == "Boolean")
  1692. {
  1693. pBuf = new byte[1];
  1694. Type targetType = TagList[i].tTagType;
  1695. if ((bool)Convert.ChangeType(ObjList[i], targetType))
  1696. {
  1697. pBuf[0] = 1;
  1698. }
  1699. else
  1700. {
  1701. pBuf[0] = 0;
  1702. }
  1703. size = 1;
  1704. }
  1705. else
  1706. {
  1707. size = Marshal.SizeOf(TagList[i].tTagType);
  1708. pBuf = StructToBytes(ObjList[i], size);
  1709. }
  1710. }
  1711. //填充数据
  1712. for (int j = 0; j < size; j++)
  1713. {
  1714. Marshal.WriteByte(pTaglist[i].pData + j * sizeof(byte), pBuf[j]);
  1715. }
  1716. pTaglist[i].nDataLength = size;
  1717. }
  1718. ERROR_NO errorNo = ERROR_NO.SUCCESS;
  1719. lock (m_objLock)
  1720. {
  1721. //errorNo = EipWriteTagList(m_nInstanceId, nNumOfTags, pTaglist);
  1722. errorNo = EipWriteTagListExt(m_nInstanceId, nNumOfTags, pTaglist, true);
  1723. //errorNo = EipWriteTagListRaw(m_nInstanceId, nNumOfTags, pTaglist);
  1724. }
  1725. string strLog = "";
  1726. strLog += "\n写入请求";
  1727. if (errorNo != ERROR_NO.SUCCESS)
  1728. {
  1729. strLog += " 失败 标签个数:" + nNumOfTags.ToString() + " 实例ID:" + m_nInstanceId.ToString() + " ";
  1730. switch (errorNo)
  1731. {
  1732. case ERROR_NO.ERR_EIP_STOPED:
  1733. strLog += ("协议栈未开启");
  1734. break;
  1735. case ERROR_NO.ERRI_INVALID_CONNECTION_INSTANCE_SPECIFIED:
  1736. strLog += ("连接的实例ID与已有的ID重复或超过最大值");
  1737. break;
  1738. case ERROR_NO.ERRI_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
  1739. strLog += ("连接的网络路径格式错误,无法检测出来目标IP离线等错误");
  1740. break;
  1741. case ERROR_NO.ERRI_CONNECTION_COUNT_LIMIT_REACHED:
  1742. strLog += ("达到最大连接数量");
  1743. break;
  1744. case ERROR_NO.ERRI_OUT_OF_MEMORY:
  1745. strLog += ("内存溢出,缓冲区已满");
  1746. break;
  1747. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_INVALID_NETWORK_PATH:
  1748. strLog += ("连接的网络地址无效");
  1749. break;
  1750. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_NO_RESPONSE:
  1751. strLog += ("连接无响应");
  1752. break;
  1753. case ERROR_NO.ERRR_CONN_CONFIG_FAILED_ERROR_RESPONSE:
  1754. strLog += ("连接响应错误");
  1755. break;
  1756. case ERROR_NO.ERRR_INVALID_DESTINATION:
  1757. strLog += ("目标标签不存在");
  1758. break;
  1759. case ERROR_NO.ERRR_TAGNAME_TOO_LONG:
  1760. strLog += ("标签名超过255字节");
  1761. break;
  1762. case ERROR_NO.ERRR_REQUEST_DATA_TOO_LARGE:
  1763. strLog += ("请求数据超限");
  1764. break;
  1765. case ERROR_NO.ERRR_CONN_CONNECTION_TIMED_OUT:
  1766. strLog += ("响应超时,请检查设备是否离线");
  1767. break;
  1768. case ERROR_NO.ERRR_TAGNAME_CONVERT_FAILED:
  1769. strLog += ("标签名解析错误");
  1770. break;
  1771. case ERROR_NO.ERRR_WRITE_DATASIZE_UNCONSISTENT:
  1772. strLog += ("写入数据长度与标签实际长度不一致");
  1773. break;
  1774. case ERROR_NO.ERRR_SCAN_ERROR:
  1775. strLog += ("扫描标签信息失败");
  1776. break;
  1777. default:
  1778. strLog += ("其他错误");
  1779. break;
  1780. }
  1781. strLog += "\n";
  1782. strRet = strLog;
  1783. nRet = -10;
  1784. }
  1785. else
  1786. {
  1787. //成功
  1788. strRet = "OK";
  1789. nRet = 0;
  1790. }
  1791. }
  1792. catch (Exception ex)
  1793. {
  1794. nRet = 120;
  1795. strRet = ex.ToString();
  1796. }
  1797. finally
  1798. {
  1799. for (int i = 0; i < nLen; i++)
  1800. {
  1801. if (pTaglist[i].pData != ((IntPtr)0))
  1802. {
  1803. Marshal.FreeHGlobal(pTaglist[i].pData);
  1804. pTaglist[i].pData = (IntPtr)0;
  1805. }
  1806. }
  1807. }
  1808. return (nRet, strRet);
  1809. }
  1810. /// <summary>
  1811. /// 一次读取Bool标签或Bool数组标签
  1812. /// </summary>
  1813. /// <param name="strTagName">标签的名称</param>
  1814. /// <param name="nCount">读取的标签个数</param>
  1815. /// <param name="boolBuf">相应标签的返回值</param>
  1816. /// <returns>函数返回结果</returns>
  1817. public (int, string) Read_Bool_Tag(string strTagName, int nCount, out bool[] boolBuf)
  1818. {
  1819. byte[] pBuf = null;
  1820. boolBuf = null;
  1821. int nRet = 0;
  1822. string strRet = "";
  1823. (nRet, strRet) = Read_Tag(strTagPrefix + strTagName, nCount, out pBuf);
  1824. if (nRet != 0) return (nRet, strRet);
  1825. return Parse_Bool_Tag(pBuf, out boolBuf);
  1826. }
  1827. /// <summary>
  1828. /// 一次写入Bool标签或Bool数组标签
  1829. /// </summary>
  1830. /// <param name="strTagName">标签的名称</param>
  1831. /// <param name="nCount">标签个数</param>
  1832. /// <param name="boolBuf">相应标签的写入值</param>
  1833. /// <returns>函数返回结果</returns>
  1834. public (int, string) Write_Bool_Tag(string strTagName, int nCount, bool[] boolBuf)
  1835. {
  1836. byte[] pBuf = null;
  1837. int nRet = 0;
  1838. string strRet = "";
  1839. pBuf = BoolToBytes(boolBuf);
  1840. if (pBuf == null) return (1, "缓存区空");
  1841. (nRet, strRet) = Write_Tag(strTagPrefix + strTagName, nCount, pBuf);
  1842. return (nRet, strRet);
  1843. }
  1844. //将转换为Byte数组
  1845. public static byte[] BoolToBytes(bool[] boolBuf)
  1846. {
  1847. if (boolBuf==null) return null;
  1848. int nNum = 0;
  1849. nNum = boolBuf.Length;
  1850. if (nNum < 1) return null;
  1851. byte[] bytes = new byte[nNum];
  1852. for (int i = 0; i < nNum; i++)
  1853. {
  1854. if (boolBuf[i])
  1855. bytes[i] = 1;
  1856. else
  1857. bytes[i] = 0;
  1858. }
  1859. return bytes;
  1860. }
  1861. public (int, string) Parse_Bool_Tag(byte[] pBuf, out bool[] boolBuf)
  1862. {
  1863. boolBuf = null;
  1864. int nNum = 0;
  1865. nNum = pBuf.Length;
  1866. if (nNum < 1) return (1, "Parse Failed");
  1867. boolBuf = new bool[nNum];
  1868. for (int i = 0; i < nNum; i++)
  1869. {
  1870. byte[] databuf = new byte[1] {0};
  1871. databuf[0] = pBuf[i];
  1872. bool iTemp = BitConverter.ToBoolean(databuf, 0);
  1873. boolBuf[i] = iTemp;
  1874. }
  1875. return (0, "OK");
  1876. }
  1877. public (int, string) Read_Byte_Tag(string strTagName, int nCount, out byte[] pBuf)
  1878. {
  1879. return Read_Tag(strTagPrefix + strTagName, nCount, out pBuf);
  1880. }
  1881. public (int, string) Write_Byte_Tag(string strTagName, int nCount, byte[] pBuf)
  1882. {
  1883. return Write_Tag(strTagPrefix + strTagName, nCount, pBuf);
  1884. }
  1885. public (int, string) Read_SInt_Tag(string strTagName, int nCount, out sbyte[] sintBuf)
  1886. {
  1887. byte[] pBuf = null;
  1888. sintBuf = null;
  1889. int nRet = 0;
  1890. string strRet = "";
  1891. (nRet, strRet) = Read_Tag(strTagPrefix + strTagName, nCount, out pBuf);
  1892. if (nRet != 0) return (nRet, strRet);
  1893. return Parse_SInt_Tag(pBuf, out sintBuf);
  1894. }
  1895. public (int, string) Write_SInt_Tag(string strTagName, int nCount, sbyte[] DataBuf)
  1896. {
  1897. byte[] pBuf = null;
  1898. int nRet = 0;
  1899. string strRet = "";
  1900. pBuf = SByteToBytes(DataBuf);
  1901. if (pBuf == null) return (1, "pBuf is Null");
  1902. (nRet, strRet) = Write_Tag(strTagPrefix + strTagName, nCount, pBuf);
  1903. return (nRet, strRet);
  1904. }
  1905. //将转换为Byte数组
  1906. public static byte[] SByteToBytes(sbyte[] DataBuf)
  1907. {
  1908. if (DataBuf == null || DataBuf.Length <= 0) return null;
  1909. int nNum = 0;
  1910. nNum = DataBuf.Length;
  1911. if (nNum < 1) return null;
  1912. byte[] bytes = new byte[nNum];
  1913. for (int i = 0; i < nNum; i++)
  1914. {
  1915. byte[] databuf = BitConverter.GetBytes(DataBuf[i]);
  1916. bytes[i] = databuf[0];
  1917. }
  1918. return bytes;
  1919. }
  1920. public (int, string) Parse_SInt_Tag(byte[] pBuf, out sbyte[] sintBuf)
  1921. {
  1922. sintBuf = null;
  1923. int nNum = 0;
  1924. nNum = pBuf.Length;
  1925. if (nNum < 1) return (1, "Parse Failed");
  1926. sintBuf = new sbyte[nNum];
  1927. for (int i = 0; i < nNum; i++)
  1928. {
  1929. sintBuf[i] = (sbyte)pBuf[i];
  1930. }
  1931. return (0, "OK");
  1932. }
  1933. public (int, string) Read_String_Tag(string strTagName, int nCount, out string strBuf)
  1934. {
  1935. int nRet=0;
  1936. string strRet = "";
  1937. byte[] pBuf = null;
  1938. strBuf = "";
  1939. (nRet, strRet) = Read_Tag(strTagPrefix + strTagName, nCount, out pBuf);
  1940. if (nRet == 0)
  1941. strBuf = System.Text.Encoding.UTF8.GetString(pBuf);
  1942. return (nRet, strRet);
  1943. }
  1944. public (int, string) Write_String_Tag(string strTagName, int nCount, string strBuf)
  1945. {
  1946. int nRet = 0;
  1947. string strRet = "";
  1948. byte[] pBuf = null;
  1949. if (string.IsNullOrEmpty(strBuf)) return (1, "string is null");
  1950. pBuf = System.Text.Encoding.UTF8.GetBytes(strBuf);
  1951. if (pBuf == null || pBuf.Length == 0 ) return (2, "pBuf is null");
  1952. (nRet, strRet) = Write_Tag(strTagPrefix + strTagName, nCount, pBuf);
  1953. return (nRet, strRet);
  1954. }
  1955. public (int, string) Parse_String_Tag(byte[] pBuf, out string strBuf)
  1956. {
  1957. int nRet = 0;
  1958. string strRet = "";
  1959. strBuf = "";
  1960. strBuf = System.Text.Encoding.UTF8.GetString(pBuf);
  1961. return (nRet, strRet);
  1962. }
  1963. public (int, string) Read_Word_Tag(string strTagName, int nCount, out ushort[] wordBuf)
  1964. {
  1965. return Read_UInt_Tag(strTagName, nCount, out wordBuf);
  1966. }
  1967. public (int, string) Write_Word_Tag(string strTagName, int nCount, ushort[] wordBuf)
  1968. {
  1969. return Write_UInt_Tag(strTagName, nCount, wordBuf);
  1970. }
  1971. public (int, string) Read_DWord_Tag(string strTagName, int nCount, out uint[] dwordBuf)
  1972. {
  1973. return Read_UDInt_Tag(strTagName, nCount, out dwordBuf);
  1974. }
  1975. public (int, string) Write_DWord_Tag(string strTagName, int nCount, uint[] dwordBuf)
  1976. {
  1977. return Write_UDInt_Tag(strTagName, nCount, dwordBuf);
  1978. }
  1979. public (int, string) Read_LWord_Tag(string strTagName, int nCount, out ulong[] lwordBuf)
  1980. {
  1981. return Read_ULInt_Tag(strTagName, nCount, out lwordBuf);
  1982. }
  1983. public (int, string) Write_LWord_Tag(string strTagName, int nCount, ulong[] lwordBuf)
  1984. {
  1985. return Write_ULInt_Tag(strTagName, nCount, lwordBuf);
  1986. }
  1987. /// <summary>
  1988. /// read 16bit int
  1989. /// </summary>
  1990. /// <param name="strTagName"></param>
  1991. /// <param name="nCount"></param>
  1992. /// <param name="intBuf"></param>
  1993. /// <returns></returns>
  1994. public (int, string) Read_Int_Tag(string strTagName, int nCount, out short[] intBuf)
  1995. {
  1996. byte[] pBuf = null;
  1997. intBuf = null;
  1998. int nRet=0;
  1999. string strRet = "";
  2000. (nRet, strRet) = Read_Tag(strTagPrefix + strTagName, nCount, out pBuf);
  2001. if (nRet != 0) return (nRet, strRet);
  2002. return Parse_Int_Tag(pBuf, out intBuf);
  2003. }
  2004. public (int, string) Parse_Int_Tag(byte[] pBuf, out short[] intBuf)
  2005. {
  2006. intBuf = null;
  2007. int nNum = 0;
  2008. nNum = pBuf.Length / 2;
  2009. if (nNum < 1) return (1, "Parse Failed");
  2010. intBuf = new short[nNum];
  2011. for (int i = 0; i < nNum; i++)
  2012. {
  2013. byte[] databuf = new byte[2] { 0, 0 };
  2014. databuf[0] = pBuf[i * 2];
  2015. databuf[1] = pBuf[i * 2 + 1];
  2016. short iTemp = BitConverter.ToInt16(databuf, 0);
  2017. intBuf[i] = iTemp;
  2018. }
  2019. return (0, "OK");
  2020. }
  2021. public (int, string) Write_Int_Tag(string strTagName, int nCount, short[] DataBuf)
  2022. {
  2023. byte[] pBuf = null;
  2024. int nRet = 0;
  2025. string strRet = "";
  2026. pBuf = ShortToBytes(DataBuf);
  2027. if (pBuf == null ) return (1, "pBuf is Null");
  2028. (nRet, strRet) = Write_Tag(strTagPrefix + strTagName, nCount, pBuf);
  2029. return (nRet, strRet);
  2030. }
  2031. //将转换为Byte数组
  2032. public static byte[] ShortToBytes(short[] DataBuf)
  2033. {
  2034. if (DataBuf == null || DataBuf.Length<=0) return null;
  2035. int nNum = 0;
  2036. //int size = Marshal.SizeOf(typeof(short));
  2037. //int size = sizeof(short);
  2038. nNum = DataBuf.Length;
  2039. if (nNum < 1) return null;
  2040. byte[] bytes = new byte[nNum*2];
  2041. for (int i = 0; i < nNum; i++)
  2042. {
  2043. byte[] databuf = BitConverter.GetBytes(DataBuf[i]);
  2044. bytes[i * 2] = databuf[0];
  2045. bytes[i * 2 + 1] = databuf[1];
  2046. }
  2047. return bytes;
  2048. }
  2049. /// <summary>
  2050. /// 16bit uint
  2051. /// </summary>
  2052. /// <param name="strTagName"></param>
  2053. /// <param name="nCount"></param>
  2054. /// <param name="uintBuf"></param>
  2055. /// <returns></returns>
  2056. public (int, string) Read_UInt_Tag(string strTagName, int nCount, out UInt16[] uintBuf)
  2057. {
  2058. byte[] pBuf = null;
  2059. uintBuf = null;
  2060. int nRet = 0;
  2061. string strRet = "";
  2062. (nRet, strRet) = Read_Tag(strTagPrefix + strTagName, nCount, out pBuf);
  2063. if (nRet != 0) return (nRet, strRet);
  2064. return Parse_UInt_Tag(pBuf, out uintBuf);
  2065. }
  2066. public (int, string) Write_UInt_Tag(string strTagName, int nCount, ushort[] DataBuf)
  2067. {
  2068. byte[] pBuf = null;
  2069. int nRet = 0;
  2070. string strRet = "";
  2071. pBuf = UShortToBytes(DataBuf);
  2072. if (pBuf == null) return (1, "pBuf is Null");
  2073. (nRet, strRet) = Write_Tag(strTagPrefix + strTagName, nCount, pBuf);
  2074. return (nRet, strRet);
  2075. }
  2076. //将转换为Byte数组
  2077. public static byte[] UShortToBytes(ushort[] DataBuf)
  2078. {
  2079. if (DataBuf == null || DataBuf.Length <= 0) return null;
  2080. int nNum = 0;
  2081. //int size = Marshal.SizeOf(typeof(short));
  2082. //int size = sizeof(short);
  2083. nNum = DataBuf.Length;
  2084. if (nNum < 1) return null;
  2085. byte[] bytes = new byte[nNum*2];
  2086. for (int i = 0; i < nNum; i++)
  2087. {
  2088. byte[] databuf = BitConverter.GetBytes(DataBuf[i]);
  2089. bytes[i * 2] = databuf[0];
  2090. bytes[i * 2 + 1] = databuf[1];
  2091. }
  2092. return bytes;
  2093. }
  2094. public (int, string) Parse_UInt_Tag(byte[] pBuf, out UInt16[] uintBuf)
  2095. {
  2096. uintBuf = null;
  2097. int nNum = 0;
  2098. nNum = pBuf.Length / 2;
  2099. if (nNum < 1) return (1, "Parse Failed");
  2100. uintBuf = new UInt16[nNum];
  2101. for (int i = 0; i < nNum; i++)
  2102. {
  2103. byte[] databuf = new byte[2] { 0, 0 };
  2104. databuf[0] = pBuf[i * 2];
  2105. databuf[1] = pBuf[i * 2 + 1];
  2106. UInt16 iTemp = BitConverter.ToUInt16(databuf, 0);
  2107. uintBuf[i] = iTemp;
  2108. }
  2109. return (0, "OK");
  2110. }
  2111. /// <summary>
  2112. /// 32bits int
  2113. /// </summary>
  2114. /// <param name="strTagName"></param>
  2115. /// <param name="nCount"></param>
  2116. /// <param name="intBuf"></param>
  2117. /// <returns></returns>
  2118. public (int, string) Read_DInt_Tag(string strTagName, int nCount, out int[] intBuf)
  2119. {
  2120. byte[] pBuf = null;
  2121. intBuf = null;
  2122. int nRet = 0;
  2123. string strRet = "";
  2124. (nRet, strRet) = Read_Tag(strTagPrefix + strTagName, nCount, out pBuf);
  2125. if (nRet != 0) return (nRet, strRet);
  2126. return Parse_DInt_Tag(pBuf, out intBuf);
  2127. }
  2128. public (int, string) Write_DInt_Tag(string strTagName, int nCount, int[] DataBuf)
  2129. {
  2130. byte[] pBuf = null;
  2131. int nRet = 0;
  2132. string strRet = "";
  2133. pBuf = DIntToBytes(DataBuf);
  2134. if (pBuf == null) return (1, "pBuf is Null");
  2135. (nRet, strRet) = Write_Tag(strTagPrefix + strTagName, nCount, pBuf);
  2136. return (nRet, strRet);
  2137. }
  2138. //将转换为Byte数组
  2139. public static byte[] DIntToBytes(int[] DataBuf)
  2140. {
  2141. if (DataBuf == null || DataBuf.Length <= 0) return null;
  2142. int nNum = 0;
  2143. nNum = DataBuf.Length;
  2144. if (nNum < 1) return null;
  2145. byte[] bytes = new byte[nNum*4];
  2146. for (int i = 0; i < nNum; i++)
  2147. {
  2148. byte[] databuf = BitConverter.GetBytes(DataBuf[i]);
  2149. bytes[i * 4] = databuf[0];
  2150. bytes[i * 4 + 1] = databuf[1];
  2151. bytes[i * 4 + 2] = databuf[2];
  2152. bytes[i * 4 + 3] = databuf[3];
  2153. }
  2154. return bytes;
  2155. }
  2156. public (int, string) Parse_DInt_Tag(byte[] pBuf, out int[] intBuf)
  2157. {
  2158. intBuf = null;
  2159. int nNum = 0;
  2160. nNum = pBuf.Length / 4;
  2161. if (nNum < 1) return (1, "Parse Failed");
  2162. intBuf = new int[nNum];
  2163. for (int i = 0; i < nNum; i++)
  2164. {
  2165. byte[] databuf = new byte[4] { 0, 0, 0, 0 };
  2166. databuf[0] = pBuf[i * 4];
  2167. databuf[1] = pBuf[i * 4 + 1];
  2168. databuf[2] = pBuf[i * 4 + 2];
  2169. databuf[3] = pBuf[i * 4 + 3];
  2170. int iTemp = BitConverter.ToInt32(databuf, 0);
  2171. intBuf[i] = iTemp;
  2172. }
  2173. return (0, "OK");
  2174. }
  2175. /// <summary>
  2176. /// 无符号32位整数
  2177. /// </summary>
  2178. /// <param name="strTagName"></param>
  2179. /// <param name="nCount"></param>
  2180. /// <param name="intBuf"></param>
  2181. /// <returns></returns>
  2182. public (int, string) Read_UDInt_Tag(string strTagName, int nCount, out uint[] uintBuf)
  2183. {
  2184. byte[] pBuf = null;
  2185. uintBuf = null;
  2186. int nRet = 0;
  2187. string strRet = "";
  2188. (nRet, strRet) = Read_Tag(strTagPrefix + strTagName, nCount, out pBuf);
  2189. if (nRet != 0) return (nRet, strRet);
  2190. return Parse_UDInt_Tag(pBuf, out uintBuf);
  2191. }
  2192. public (int, string) Write_UDInt_Tag(string strTagName, int nCount, uint[] DataBuf)
  2193. {
  2194. byte[] pBuf = null;
  2195. int nRet = 0;
  2196. string strRet = "";
  2197. pBuf = UDIntToBytes(DataBuf);
  2198. if (pBuf == null) return (1, "pBuf is Null");
  2199. (nRet, strRet) = Write_Tag(strTagPrefix + strTagName, nCount, pBuf);
  2200. return (nRet, strRet);
  2201. }
  2202. //将转换为Byte数组
  2203. public static byte[] UDIntToBytes(uint[] DataBuf)
  2204. {
  2205. if (DataBuf == null || DataBuf.Length <= 0) return null;
  2206. int nNum = 0;
  2207. nNum = DataBuf.Length;
  2208. if (nNum < 1) return null;
  2209. byte[] bytes = new byte[nNum * 4];
  2210. for (int i = 0; i < nNum; i++)
  2211. {
  2212. byte[] databuf = BitConverter.GetBytes(DataBuf[i]);
  2213. bytes[i * 4] = databuf[0];
  2214. bytes[i * 4 + 1] = databuf[1];
  2215. bytes[i * 4 + 2] = databuf[2];
  2216. bytes[i * 4 + 3] = databuf[3];
  2217. }
  2218. return bytes;
  2219. }
  2220. public (int, string) Parse_UDInt_Tag(byte[] pBuf, out uint[] uintBuf)
  2221. {
  2222. uintBuf = null;
  2223. int nNum = 0;
  2224. nNum = pBuf.Length / 4;
  2225. if (nNum < 1) return (1, "Parse Failed");
  2226. uintBuf = new uint[nNum];
  2227. for (int i = 0; i < nNum; i++)
  2228. {
  2229. byte[] databuf = new byte[4] { 0, 0, 0, 0 };
  2230. databuf[0] = pBuf[i * 4];
  2231. databuf[1] = pBuf[i * 4 + 1];
  2232. databuf[2] = pBuf[i * 4 + 2];
  2233. databuf[3] = pBuf[i * 4 + 3];
  2234. uint iTemp = BitConverter.ToUInt32(databuf, 0);
  2235. uintBuf[i] = iTemp;
  2236. }
  2237. return (0, "OK");
  2238. }
  2239. public (int, string) Read_LInt_Tag(string strTagName, int nCount, out long[] longBuf)
  2240. {
  2241. byte[] pBuf = null;
  2242. longBuf = null;
  2243. int nRet = 0;
  2244. string strRet = "";
  2245. (nRet, strRet) = Read_Tag(strTagPrefix + strTagName, nCount, out pBuf);
  2246. if (nRet != 0) return (nRet, strRet);
  2247. return Parse_LInt_Tag(pBuf, out longBuf);
  2248. }
  2249. public (int, string) Write_LInt_Tag(string strTagName, int nCount, long[] DataBuf)
  2250. {
  2251. byte[] pBuf = null;
  2252. int nRet = 0;
  2253. string strRet = "";
  2254. pBuf = LIntToBytes(DataBuf);
  2255. if (pBuf == null) return (1, "pBuf is Null");
  2256. (nRet, strRet) = Write_Tag(strTagPrefix + strTagName, nCount, pBuf);
  2257. return (nRet, strRet);
  2258. }
  2259. //将转换为Byte数组
  2260. public static byte[] LIntToBytes(long[] DataBuf)
  2261. {
  2262. if (DataBuf == null || DataBuf.Length <= 0) return null;
  2263. int nNum = 0;
  2264. nNum = DataBuf.Length;
  2265. if (nNum < 1) return null;
  2266. byte[] bytes = new byte[nNum * 8];
  2267. for (int i = 0; i < nNum; i++)
  2268. {
  2269. byte[] databuf = BitConverter.GetBytes(DataBuf[i]);
  2270. bytes[i * 4] = databuf[0];
  2271. bytes[i * 4 + 1] = databuf[1];
  2272. bytes[i * 4 + 2] = databuf[2];
  2273. bytes[i * 4 + 3] = databuf[3];
  2274. bytes[i * 4 + 4] = databuf[4];
  2275. bytes[i * 4 + 5] = databuf[5];
  2276. bytes[i * 4 + 6] = databuf[6];
  2277. bytes[i * 4 + 7] = databuf[7];
  2278. }
  2279. return bytes;
  2280. }
  2281. public (int, string) Parse_LInt_Tag(byte[] pBuf, out long[] longBuf)
  2282. {
  2283. longBuf = null;
  2284. int nNum = 0;
  2285. nNum = pBuf.Length / 8;
  2286. if (nNum < 1) return (1, "Parse Failed");
  2287. longBuf = new long[nNum];
  2288. for (int i = 0; i < nNum; i++)
  2289. {
  2290. byte[] databuf = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
  2291. databuf[0] = pBuf[i * 4];
  2292. databuf[1] = pBuf[i * 4 + 1];
  2293. databuf[2] = pBuf[i * 4 + 2];
  2294. databuf[3] = pBuf[i * 4 + 3];
  2295. databuf[4] = pBuf[i * 4 + 4];
  2296. databuf[5] = pBuf[i * 4 + 5];
  2297. databuf[6] = pBuf[i * 4 + 6];
  2298. databuf[7] = pBuf[i * 4 + 7];
  2299. long iTemp = BitConverter.ToInt64(databuf, 0);
  2300. longBuf[i] = iTemp;
  2301. }
  2302. return (0, "OK");
  2303. }
  2304. /// <summary>
  2305. /// 无符号64整数
  2306. /// </summary>
  2307. /// <param name="strTagName"></param>
  2308. /// <param name="nCount"></param>
  2309. /// <param name="ulongBuf"></param>
  2310. /// <returns></returns>
  2311. public (int, string) Read_ULInt_Tag(string strTagName, int nCount, out ulong[] ulongBuf)
  2312. {
  2313. byte[] pBuf = null;
  2314. ulongBuf = null;
  2315. int nRet = 0;
  2316. string strRet = "";
  2317. (nRet, strRet) = Read_Tag(strTagPrefix + strTagName, nCount, out pBuf);
  2318. if (nRet != 0) return (nRet, strRet);
  2319. return Parse_ULInt_Tag(pBuf, out ulongBuf);
  2320. }
  2321. public (int, string) Write_ULInt_Tag(string strTagName, int nCount, ulong[] DataBuf)
  2322. {
  2323. byte[] pBuf = null;
  2324. int nRet = 0;
  2325. string strRet = "";
  2326. pBuf = ULIntToBytes(DataBuf);
  2327. if (pBuf == null) return (1, "pBuf is Null");
  2328. (nRet, strRet) = Write_Tag(strTagPrefix + strTagName, nCount, pBuf);
  2329. return (nRet, strRet);
  2330. }
  2331. //将转换为Byte数组
  2332. public static byte[] ULIntToBytes(ulong[] DataBuf)
  2333. {
  2334. if (DataBuf == null || DataBuf.Length <= 0) return null;
  2335. int nNum = 0;
  2336. nNum = DataBuf.Length;
  2337. if (nNum < 1) return null;
  2338. byte[] bytes = new byte[nNum * 8];
  2339. for (int i = 0; i < nNum; i++)
  2340. {
  2341. byte[] databuf = BitConverter.GetBytes(DataBuf[i]);
  2342. bytes[i * 4] = databuf[0];
  2343. bytes[i * 4 + 1] = databuf[1];
  2344. bytes[i * 4 + 2] = databuf[2];
  2345. bytes[i * 4 + 3] = databuf[3];
  2346. bytes[i * 4 + 4] = databuf[4];
  2347. bytes[i * 4 + 5] = databuf[5];
  2348. bytes[i * 4 + 6] = databuf[6];
  2349. bytes[i * 4 + 7] = databuf[7];
  2350. }
  2351. return bytes;
  2352. }
  2353. public (int, string) Parse_ULInt_Tag(byte[] pBuf, out ulong[] ulongBuf)
  2354. {
  2355. ulongBuf = null;
  2356. int nNum = 0;
  2357. nNum = pBuf.Length / 8;
  2358. if (nNum < 1) return (1, "Parse Failed");
  2359. ulongBuf = new ulong[nNum];
  2360. for (int i = 0; i < nNum; i++)
  2361. {
  2362. byte[] databuf = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
  2363. databuf[0] = pBuf[i * 4];
  2364. databuf[1] = pBuf[i * 4 + 1];
  2365. databuf[2] = pBuf[i * 4 + 2];
  2366. databuf[3] = pBuf[i * 4 + 3];
  2367. databuf[4] = pBuf[i * 4 + 4];
  2368. databuf[5] = pBuf[i * 4 + 5];
  2369. databuf[6] = pBuf[i * 4 + 6];
  2370. databuf[7] = pBuf[i * 4 + 7];
  2371. ulong iTemp = BitConverter.ToUInt64(databuf, 0);
  2372. ulongBuf[i] = iTemp;
  2373. }
  2374. return (0, "OK");
  2375. }
  2376. public (int, string) Read_Real_Tag(string strTagName, int nCount, out float[] floatBuf)
  2377. {
  2378. floatBuf = null;
  2379. byte[] pBuf = null;
  2380. int nRet = 0;
  2381. string strRet = "";
  2382. (nRet, strRet) = Read_Tag(strTagPrefix + strTagName, nCount, out pBuf);
  2383. if (nRet != 0) return (nRet, strRet);
  2384. return Parse_Real_Tag(pBuf, out floatBuf);
  2385. }
  2386. public (int, string) Write_Real_Tag(string strTagName, int nCount, float[] DataBuf)
  2387. {
  2388. byte[] pBuf = null;
  2389. int nRet = 0;
  2390. string strRet = "";
  2391. pBuf = RealToBytes(DataBuf);
  2392. if (pBuf == null) return (1, "pBuf is Null");
  2393. (nRet, strRet) = Write_Tag(strTagPrefix + strTagName, nCount, pBuf);
  2394. return (nRet, strRet);
  2395. }
  2396. //将转换为Byte数组
  2397. public static byte[] RealToBytes(float[] DataBuf)
  2398. {
  2399. if (DataBuf == null || DataBuf.Length <= 0) return null;
  2400. int nNum = 0;
  2401. nNum = DataBuf.Length;
  2402. if (nNum < 1) return null;
  2403. byte[] bytes = new byte[nNum * 4];
  2404. for (int i = 0; i < nNum; i++)
  2405. {
  2406. byte[] databuf = BitConverter.GetBytes(DataBuf[i]);
  2407. bytes[i * 4] = databuf[0];
  2408. bytes[i * 4 + 1] = databuf[1];
  2409. bytes[i * 4 + 2] = databuf[2];
  2410. bytes[i * 4 + 3] = databuf[3];
  2411. }
  2412. return bytes;
  2413. }
  2414. public (int, string) Parse_Real_Tag(byte[] pBuf, out float[] floatBuf)
  2415. {
  2416. floatBuf = null;
  2417. int nNum = 0;
  2418. nNum = pBuf.Length / 4;
  2419. floatBuf = new float[nNum];
  2420. if (nNum < 1) return (1, "Parse Failed");
  2421. for (int i = 0; i < nNum; i++)
  2422. {
  2423. byte[] databuf = new byte[4] { 0, 0, 0, 0 };
  2424. databuf[0] = pBuf[i * 4];
  2425. databuf[1] = pBuf[i * 4 + 1];
  2426. databuf[2] = pBuf[i * 4 + 2];
  2427. databuf[3] = pBuf[i * 4 + 3];
  2428. float fTemp = BitConverter.ToSingle(databuf, 0);
  2429. floatBuf[i] = fTemp;
  2430. }
  2431. return (0, "OK");
  2432. }
  2433. public (int, string) Read_LReal_Tag(string strTagName, int nCount, out double[] doubleBuf)
  2434. {
  2435. doubleBuf = null;
  2436. byte[] pBuf = null;
  2437. int nRet = 0;
  2438. string strRet = "";
  2439. (nRet, strRet) = Read_Tag(strTagPrefix + strTagName, nCount, out pBuf);
  2440. if (nRet != 0) return (nRet, strRet);
  2441. return Parse_LReal_Tag(pBuf, out doubleBuf);
  2442. }
  2443. public (int, string) Write_LReal_Tag(string strTagName, int nCount, double[] DataBuf)
  2444. {
  2445. byte[] pBuf = null;
  2446. int nRet = 0;
  2447. string strRet = "";
  2448. pBuf = LRealToBytes(DataBuf);
  2449. if (pBuf == null) return (1, "pBuf is Null");
  2450. (nRet, strRet) = Write_Tag(strTagPrefix + strTagName, nCount, pBuf);
  2451. return (nRet, strRet);
  2452. }
  2453. //将转换为Byte数组
  2454. public static byte[] LRealToBytes(double[] DataBuf)
  2455. {
  2456. if (DataBuf == null || DataBuf.Length <= 0) return null;
  2457. int nNum = 0;
  2458. nNum = DataBuf.Length;
  2459. if (nNum < 1) return null;
  2460. byte[] bytes = new byte[nNum * 8];
  2461. for (int i = 0; i < nNum; i++)
  2462. {
  2463. byte[] databuf = BitConverter.GetBytes(DataBuf[i]);
  2464. bytes[i * 4] = databuf[0];
  2465. bytes[i * 4 + 1] = databuf[1];
  2466. bytes[i * 4 + 2] = databuf[2];
  2467. bytes[i * 4 + 3] = databuf[3];
  2468. bytes[i * 4 + 4] = databuf[4];
  2469. bytes[i * 4 + 5] = databuf[5];
  2470. bytes[i * 4 + 6] = databuf[6];
  2471. bytes[i * 4 + 7] = databuf[7];
  2472. }
  2473. return bytes;
  2474. }
  2475. public (int, string) Parse_LReal_Tag(byte[] pBuf, out double[] doubleBuf)
  2476. {
  2477. doubleBuf = null;
  2478. int nNum = 0;
  2479. nNum = pBuf.Length / 8;
  2480. if (nNum < 1) return (1, "Parse Failed");
  2481. doubleBuf = new double[nNum];
  2482. for (int i = 0; i < nNum; i++)
  2483. {
  2484. byte[] databuf = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
  2485. databuf[0] = pBuf[i * 8];
  2486. databuf[1] = pBuf[i * 8 + 1];
  2487. databuf[2] = pBuf[i * 8 + 2];
  2488. databuf[3] = pBuf[i * 8 + 3];
  2489. databuf[4] = pBuf[i * 8 + 4];
  2490. databuf[5] = pBuf[i * 8 + 5];
  2491. databuf[6] = pBuf[i * 8 + 6];
  2492. databuf[7] = pBuf[i * 8 + 7];
  2493. double dTemp = BitConverter.ToDouble(databuf, 0);
  2494. doubleBuf[i] = dTemp;
  2495. }
  2496. return (0, "OK");
  2497. }
  2498. #region 对象与字节数组转化
  2499. //将结构体类型转换为Byte数组
  2500. public static byte[] StructToBytes(object structObj, int size)
  2501. {
  2502. byte[] bytes = new byte[size];
  2503. IntPtr structPtr = Marshal.AllocHGlobal(size);
  2504. try
  2505. {
  2506. //将结构体拷到分配好的内存空间
  2507. Marshal.StructureToPtr(structObj, structPtr, false);
  2508. //从内存空间拷贝到byte 数组
  2509. Marshal.Copy(structPtr, bytes, 0, size);
  2510. }
  2511. finally
  2512. {
  2513. //释放内存空间
  2514. Marshal.FreeHGlobal(structPtr);
  2515. }
  2516. return bytes;
  2517. }
  2518. //将Byte转换为结构体类型
  2519. public static object ByteToStruct(byte[] bytes, Type type)
  2520. {
  2521. int size = Marshal.SizeOf(type);
  2522. if (type.Name == "Boolean") size = 1;
  2523. if (size > bytes.Length)
  2524. {
  2525. return null;
  2526. }
  2527. //分配结构体内存空间
  2528. IntPtr structPtr = Marshal.AllocHGlobal(size);
  2529. object obj = null;
  2530. try
  2531. {
  2532. //将byte数组拷贝到分配好的内存空间
  2533. Marshal.Copy(bytes, 0, structPtr, size);
  2534. //将内存空间转换为目标结构体
  2535. obj = Marshal.PtrToStructure(structPtr, type);
  2536. }
  2537. finally
  2538. {
  2539. //释放内存空间
  2540. Marshal.FreeHGlobal(structPtr);
  2541. }
  2542. return obj;
  2543. }
  2544. //将Byte转换为结构体类型
  2545. public static Object ByteToStruct2(byte[] bytes, Type type)
  2546. {
  2547. int size = Marshal.SizeOf(type);
  2548. if (type.Name == "Boolean") size = 1;
  2549. if (size > bytes.Length)
  2550. {
  2551. return null;
  2552. }
  2553. //分配结构体内存空间
  2554. IntPtr structPtr = Marshal.AllocHGlobal(size);
  2555. object obj = null;
  2556. try
  2557. {
  2558. //将byte数组拷贝到分配好的内存空间
  2559. Marshal.Copy(bytes, 0, structPtr, size);
  2560. //将内存空间转换为目标结构体
  2561. obj = Marshal.PtrToStructure(structPtr, type);
  2562. }
  2563. finally
  2564. {
  2565. //释放内存空间
  2566. Marshal.FreeHGlobal(structPtr);
  2567. }
  2568. return obj;
  2569. }
  2570. public static T ConvertBytesToStruct<T>(byte[] bytes)
  2571. {
  2572. // 将字节数组转换为结构体
  2573. GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  2574. T myStruct = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
  2575. handle.Free();
  2576. return myStruct;
  2577. }
  2578. #endregion 对象与字节数组转化
  2579. /// <summary>
  2580. /// 一次读取单个标签
  2581. /// </summary>
  2582. /// <typeparam name="T">需要读取的标签结构</typeparam>
  2583. /// <param name="strTagName">需要读取的标签名</param>
  2584. /// <param name="nCount">读取标签的个数</param>
  2585. /// <param name="outObj">返回相应标签值对象</param>
  2586. /// <returns></returns>
  2587. public (int, string) Read_SingleTag<T>(string strTagName, int nCount, out T outObj)
  2588. {
  2589. outObj = default(T);
  2590. byte[] pBuf = null;
  2591. int nRet = 0;
  2592. string strRet = "";
  2593. try
  2594. {
  2595. (nRet, strRet) = Read_Tag(strTagPrefix + strTagName, nCount, out pBuf);
  2596. if (nRet != 0) return (nRet, strRet);
  2597. string str = typeof(T).Name;
  2598. string[] substrings = { "Boolean", "SByte", "Byte", "Int16", "UInt16", "Int32", "UInt32", "Int64", "UInt64", "Single", "Double", "String" };
  2599. bool containsAny = substrings.Any(substring => str.Contains(substring));
  2600. if (containsAny)
  2601. {
  2602. if (str != strRet) return (120, "变量类型不一致");
  2603. }
  2604. if (str == "String")
  2605. {
  2606. outObj = (T)(object)System.Text.Encoding.UTF8.GetString(pBuf);
  2607. return (0, "OK");
  2608. }
  2609. outObj = (T)ByteToStruct(pBuf, typeof(T));
  2610. }
  2611. catch(Exception ex)
  2612. {
  2613. return (110, ex.ToString());
  2614. }
  2615. return (0, "OK");
  2616. }
  2617. public (int, string) Write_SingleTag<T>(string strTagName, int nCount, T inObj)
  2618. {
  2619. byte[] pBuf = null;
  2620. int nRet = 0;
  2621. string strRet = "";
  2622. int size = 0;
  2623. try
  2624. {
  2625. string str = typeof(T).Name;
  2626. string[] substrings = { "Boolean", "SByte", "Byte", "Int16", "UInt16", "Int32", "UInt32", "Int64", "UInt64", "Single", "Double", "String" };
  2627. bool containsAny = substrings.Any(substring => str.Contains(substring));
  2628. if (containsAny)
  2629. {
  2630. Type type = inObj.GetType();
  2631. if (str != type.Name) return (120, "变量类型不一致");
  2632. }
  2633. if (str == "String")
  2634. {
  2635. //size = inObj.ToString().Length + 1;
  2636. string wtStr= inObj as string;
  2637. pBuf = System.Text.Encoding.Default.GetBytes(wtStr);
  2638. size = pBuf.Length;
  2639. }
  2640. else
  2641. {
  2642. if (typeof(T).Name == "Boolean")
  2643. {
  2644. //pBuf = new byte[1];
  2645. //Type targetType = typeof(T);
  2646. //if ((bool)Convert.ChangeType(inObj, targetType))
  2647. //{
  2648. // pBuf[0] = 1;
  2649. //}
  2650. //else
  2651. //{
  2652. // pBuf[0] = 0;
  2653. //}
  2654. size = 1;
  2655. }
  2656. else
  2657. {
  2658. size = Marshal.SizeOf(typeof(T));
  2659. }
  2660. pBuf = StructToBytes(inObj, size);
  2661. }
  2662. (nRet, strRet) = Write_Tag(strTagPrefix + strTagName, nCount, pBuf);
  2663. return (nRet, strRet);
  2664. }
  2665. catch (Exception ex)
  2666. {
  2667. return (110, ex.ToString());
  2668. }
  2669. }
  2670. /// <summary>
  2671. /// 一次读取多个标签
  2672. /// </summary>
  2673. /// <param name="stTagList">需要读取的标签列表信息</param>
  2674. /// <param name="outObj">返回的相应标签对象</param>
  2675. /// <returns>执行是否成功,非0不成功,以及显示相应的失败信息</returns>
  2676. public (int, string) Read_MultiTags(List<StructTag> stTagList, out List<Object> outObj)
  2677. {
  2678. outObj = new List<Object>();
  2679. byte[][] pBuf = null;
  2680. int nRet = 0;
  2681. string strRet = "";
  2682. int nLen = stTagList.Count;
  2683. List<StructTag> TagList=new List<StructTag>();
  2684. try
  2685. {
  2686. foreach(StructTag tag in stTagList)
  2687. {
  2688. TagList.Add(new StructTag {strTagName= strTagPrefix + tag.strTagName,tTagType=tag.tTagType,nCount=tag.nCount});
  2689. }
  2690. (nRet, strRet) = Read_Tags(TagList, out pBuf);
  2691. if (nRet != 0) return (nRet, strRet);
  2692. for (int i = 0; i < nLen; i++)
  2693. {
  2694. //需要调用的方法
  2695. //MethodInfo mi = typeof(Inovance_EIP).GetMethod("ByteToStruct");
  2696. //该方法的泛型类型Type->typeof(int)
  2697. //MethodInfo miConstructed = mi.MakeGenericMethod(strTagList[i].TagType);
  2698. //var arg = { pBuf[i]};
  2699. //执行调用
  2700. //miConstructed.Invoke(null, args);
  2701. //outObj.Add(Convert.ChangeType(ByteToStruct2(pBuf[i], targetType), targetType));
  2702. Type targetType = TagList[i].tTagType;
  2703. if (targetType != null)
  2704. {
  2705. object obj = ByteToStruct(pBuf[i], targetType);
  2706. //if (obj != null)
  2707. outObj.Add(obj);
  2708. }
  2709. }
  2710. }
  2711. catch (Exception ex)
  2712. {
  2713. return (110, ex.ToString());
  2714. }
  2715. return (0, "OK");
  2716. }
  2717. /// <summary>
  2718. /// 一次写多个标签
  2719. /// </summary>
  2720. /// <param name="stTagList">需要写入的标签列表信息</param>
  2721. /// <param name="ObjList">写入的相应标签对象</param>
  2722. /// <returns>执行是否成功,非0不成功,以及显示相应的失败信息</returns>
  2723. public (int, string) Write_MultiTags(List<StructTag> stTagList, List<Object> ObjList)
  2724. {
  2725. int nRet = 0;
  2726. string strRet = "";
  2727. int nLen = stTagList.Count;
  2728. if (ObjList.Count != nLen) return (10, "标签列表和对象列表长度不一致");
  2729. List<StructTag> TagList = new List<StructTag>();
  2730. try
  2731. {
  2732. foreach (StructTag tag in stTagList)
  2733. {
  2734. TagList.Add(new StructTag { strTagName = strTagPrefix + tag.strTagName, tTagType = tag.tTagType, nCount = tag.nCount });
  2735. }
  2736. (nRet, strRet) = Write_Tags(TagList, ObjList);
  2737. return (nRet, strRet);
  2738. }
  2739. catch (Exception ex)
  2740. {
  2741. return (110, ex.ToString());
  2742. }
  2743. }
  2744. #region 清除标签缓存
  2745. /// <summary>
  2746. /// 清除标签缓存
  2747. /// </summary>
  2748. public void Clear_TagCache()
  2749. {
  2750. ResetTagInfo();
  2751. }
  2752. #endregion 清除标签缓存
  2753. }
  2754. }