Inovance_EIP.cs 121 KB

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