当前位置:   article > 正文

S7net【C#】_c# s7net

c# s7net

C#项目,跟西门子PLC通讯

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using S7.Net;
  8. namespace MeProgram
  9. {
  10. public class S7NetPlc
  11. {
  12. Plc plc;
  13. /// <summary>
  14. /// 初始化S7NetPLC
  15. /// </summary>
  16. /// <param name="IP"></param>
  17. /// <param name="CPUType"></param>
  18. public S7NetPlc(string IP, string CPUType ="S71200")
  19. {
  20. CreatePlc(IP, CPUType);
  21. }
  22. /// <summary>
  23. /// 实例化PLC IP和CPU型号 CPU型号默认S71200
  24. /// </summary>
  25. /// <param name="ip"></param>
  26. /// <param name="cpu"></param>
  27. void CreatePlc(string ip, string cpu)
  28. {
  29. CpuType cputype = new CpuType();
  30. if (Enum.TryParse(cpu.ToUpper(), out cputype))
  31. {
  32. plc = new Plc(cputype, ip, 0, 1);
  33. }
  34. else
  35. {
  36. Console.WriteLine("CPU型号错误");
  37. }
  38. }
  39. /// <summary>
  40. /// 连接PLC 再次调用可重新连接
  41. /// </summary>
  42. public async void Connet()
  43. {
  44. try
  45. {
  46. await plc.OpenAsync();
  47. }
  48. catch (Exception ex)
  49. {
  50. Console.WriteLine(ex.Message);
  51. }
  52. }
  53. /// <summary>
  54. /// PLC连接状态
  55. /// </summary>
  56. /// <returns></returns>
  57. public bool ConnetStatus()
  58. {
  59. return plc.IsConnected;
  60. }
  61. #region 读取PLC数据
  62. /// <summary>
  63. /// 读取PLC 输入点 i
  64. /// </summary>
  65. /// <param name="start">开始的地址</param>
  66. /// <param name="字节数">读取字节数量</param>
  67. /// <returns></returns>
  68. public BitArray Get_I(int 地址I点, int 数量)
  69. {
  70. BitArray bitArray = new BitArray(数量);
  71. try
  72. {
  73. if (plc.IsConnected)
  74. {
  75. bitArray = (BitArray)plc.Read(DataType.Input, 0, 地址I点, VarType.Bit, 数量);
  76. }
  77. }
  78. catch (Exception ex)
  79. {
  80. Console.WriteLine(ex.Message);
  81. }
  82. return bitArray;
  83. }
  84. /// <summary>
  85. /// 读取PLC 输出点Q
  86. /// </summary>
  87. /// <param name="start">开始的地址</param>
  88. /// <param name="字节数">读取字节数量</param>
  89. /// <returns></returns>
  90. public BitArray Get_Q(int 地址Q点, int 数量)
  91. {
  92. BitArray bitArray = new BitArray(数量);
  93. try
  94. {
  95. if (plc.IsConnected)
  96. {
  97. bitArray = (BitArray)plc.Read(DataType.Output, 0, 地址Q点, VarType.Bit, 数量);
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. Console.WriteLine(ex.Message);
  103. }
  104. return bitArray;
  105. }
  106. /// <summary>
  107. /// 读取PLC默认寄存器地址
  108. /// </summary>
  109. /// <param name="地址M点"></param>
  110. /// <param name="字节数"></param>
  111. /// <returns></returns>
  112. public BitArray Get_M(int 地址M点, int 数量)
  113. {
  114. BitArray bitArray = new BitArray(数量);
  115. try
  116. {
  117. if (plc.IsConnected)
  118. {
  119. bitArray = (BitArray)plc.Read(DataType.Memory, 0, 地址M点, VarType.Bit, 数量);
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. Console.WriteLine(ex.Message);
  125. }
  126. return bitArray;
  127. }
  128. /// <summary>
  129. /// 批量读取连续的 bool 值
  130. /// </summary>
  131. /// <param name="DB"></param>
  132. /// <param name="起始地址"></param>
  133. /// <param name="数量"></param>
  134. /// <returns></returns>
  135. public BitArray Get_BitArray(int DB, int 偏移量, int 数量)
  136. {
  137. BitArray bitArray = new BitArray(数量);
  138. try
  139. {
  140. if (plc.IsConnected)
  141. {
  142. bitArray = (BitArray)plc.Read(DataType.DataBlock, DB, 偏移量, VarType.Bit, 数量);
  143. }
  144. }
  145. catch (Exception ex)
  146. {
  147. Console.WriteLine(ex.Message);
  148. }
  149. return bitArray;
  150. }
  151. /// <summary>
  152. /// 读取PLC DB块字节数据
  153. /// </summary>
  154. /// <param name="DB">DB块</param>
  155. /// <param name="start">开始的偏移量</param>
  156. /// <param name="字节数">读取的长度</param>
  157. /// <returns></returns>
  158. public byte[] Get_bytes(int DB, int 偏移量, int 数量)
  159. {
  160. byte[] db_byte = new byte[数量];
  161. try
  162. {
  163. if (plc.IsConnected)
  164. {
  165. db_byte = plc.ReadBytes(DataType.DataBlock, DB, 偏移量, 数量);
  166. }
  167. }
  168. catch (Exception ex)
  169. {
  170. Console.WriteLine("读取PLC_byte_Error:" + ex.Message);
  171. }
  172. return db_byte;
  173. }
  174. /// <summary>
  175. /// 读取PLC DB快16位有符号数据
  176. /// </summary>
  177. /// <param name="绝对地址"></param>
  178. /// <returns></returns>
  179. public short Get_int(int DB, int 偏移量)
  180. {
  181. short data = 0;
  182. try
  183. {
  184. if (plc.IsConnected)
  185. {
  186. data = (short)plc.Read(DataType.DataBlock, DB, 偏移量, VarType.Int, 1);
  187. }
  188. }
  189. catch (Exception ex)
  190. {
  191. Console.WriteLine("读取PLC_int_Error:" + ex.Message);
  192. }
  193. return data;
  194. }
  195. /// <summary>
  196. /// 读取PLC DB块32位有符号数据
  197. /// </summary>
  198. /// <param name="DB"></param>
  199. /// <param name="偏移量"></param>
  200. /// <returns></returns>
  201. public int Get_Dint(int DB, int 偏移量)
  202. {
  203. int data = 0;
  204. try
  205. {
  206. if (plc.IsConnected)
  207. {
  208. data = (int)plc.Read(DataType.DataBlock, DB, 偏移量, VarType.DInt, 1);
  209. }
  210. }
  211. catch (Exception ex)
  212. {
  213. Console.WriteLine("读取PLC_Dint出错:" + ex.Message);
  214. }
  215. return data;
  216. }
  217. /// <summary>
  218. /// 读取PLC DB快16位无符号数据
  219. /// </summary>
  220. /// <param name="绝对地址"></param>
  221. /// <returns></returns>
  222. public ushort Get_Word(int DB, int 偏移量)
  223. {
  224. ushort data = 0;
  225. try
  226. {
  227. if (plc.IsConnected)
  228. {
  229. data = (ushort)plc.Read(DataType.DataBlock, DB, 偏移量, VarType.Word, 1);
  230. }
  231. }
  232. catch (Exception ex)
  233. {
  234. Console.WriteLine("读取PLC_Word_Error:" + ex.Message);
  235. }
  236. return data;
  237. }
  238. /// <summary>
  239. /// 读取PLC DB快32位无符号数据
  240. /// </summary>
  241. /// <param name="DB"></param>
  242. /// <param name="偏移量"></param>
  243. /// <returns></returns>
  244. public uint Get_DWord(int DB, int 偏移量)
  245. {
  246. uint data = 0;
  247. try
  248. {
  249. if (plc.IsConnected)
  250. {
  251. data = (uint)plc.Read(DataType.DataBlock, DB, 偏移量, VarType.DWord, 1);
  252. }
  253. }
  254. catch (Exception ex)
  255. {
  256. Console.WriteLine("读取PLC_DWord出错:" + ex.Message);
  257. }
  258. return data;
  259. }
  260. /// <summary>
  261. /// 读取PLC Timer 类型数据
  262. /// </summary>
  263. /// <param name="绝对地址"></param>
  264. /// <returns></returns>
  265. public int Get_Timer(string 绝对地址)
  266. {
  267. object ss = 0;
  268. try
  269. {
  270. if (plc.IsConnected)
  271. {
  272. ss = plc.Read(绝对地址.ToUpper());
  273. }
  274. }
  275. catch (Exception ex)
  276. {
  277. Console.WriteLine("读取PLC_Timer_Error:" + ex.Message);
  278. }
  279. int fs = Convert.ToInt32(ss);
  280. return fs;
  281. }
  282. /// <summary>
  283. /// 读取PLC Real 类型数据
  284. /// </summary>
  285. /// <param name="绝对地址">PLC绝对地址</param>
  286. /// <returns></returns>
  287. public float Get_Real(int DB, int 偏移量)
  288. {
  289. float result = 0;
  290. try
  291. {
  292. if (plc.IsConnected)
  293. {
  294. result = (float)plc.Read(DataType.DataBlock, DB, 偏移量, VarType.Real, 1);
  295. }
  296. }
  297. catch (Exception ex)
  298. {
  299. Console.WriteLine("读取PLC_Real_Error:" + ex.Message);
  300. }
  301. return result;
  302. }
  303. /// <summary>
  304. /// 读取PLC string 类型数据
  305. /// </summary>
  306. /// <param name="DB">DB块</param>
  307. /// <param name="偏移地址">开始的偏移量</param>
  308. /// <returns></returns>
  309. public string Get_String(int DB, int 偏移量)
  310. {
  311. string 返回值 = string.Empty;
  312. try
  313. {
  314. if (plc.IsConnected)
  315. {
  316. //西门子字符串长度 第一个字节是字符串的总长度 第二个是字符串的当前长度 要从当前字节的+1字节读取当前长度
  317. var bytelen = (byte)plc.Read(DataType.DataBlock, DB, 偏移量 + 1, VarType.Byte, 1);
  318. 返回值 = (string)plc.Read(DataType.DataBlock, DB, 偏移量 + 2, VarType.String, bytelen);
  319. }
  320. }
  321. catch (Exception ex)
  322. {
  323. Console.WriteLine("读取PLC_String_Error:" + ex.Message);
  324. }
  325. return 返回值;
  326. }
  327. #endregion
  328. #region 写入PLC数据
  329. /// <summary>
  330. /// 写入PLC输出点Q 地址
  331. /// </summary>
  332. /// <param name="输出Q">Q5.0 的5</param>
  333. /// <param name="第几位">Q5.4 的4</param>
  334. /// <param name="值"></param>
  335. public void Set_Q(int 输出Q, int 第几位, bool)
  336. {
  337. try
  338. {
  339. if (plc.IsConnected)
  340. {
  341. plc.WriteBit(DataType.Output, 0, 输出Q, 第几位, 值);
  342. }
  343. }
  344. catch (Exception ex)
  345. {
  346. Console.WriteLine("写入Q点出错:" + ex.Message);
  347. }
  348. }
  349. /// <summary>
  350. /// 写入PLC默认寄存器地址
  351. /// </summary>
  352. /// <param name="地址M"></param>
  353. /// <param name="第几位"></param>
  354. /// <param name="值"></param>
  355. public void Set_M(int 地址M, int 第几位, bool)
  356. {
  357. try
  358. {
  359. if (plc.IsConnected)
  360. {
  361. plc.WriteBit(DataType.Memory, 0, 地址M, 第几位, 值);
  362. }
  363. }
  364. catch (Exception ex)
  365. {
  366. Console.WriteLine("写入M地址:" + ex.Message);
  367. }
  368. }
  369. /// <summary>
  370. /// 写入PLCDB块 bool 类型地址
  371. /// </summary>
  372. /// <param name="DB"></param>
  373. /// <param name="偏移量">分配的第几个字节</param>
  374. /// <param name="第几位">字节里的第几位</param>
  375. /// <param name="值"></param>
  376. public void Set_Bit(int DB, int 偏移量, int 第几位, bool)
  377. {
  378. try
  379. {
  380. if (plc.IsConnected)
  381. {
  382. plc.WriteBit(DataType.DataBlock, DB, 偏移量, 第几位, 值);
  383. }
  384. }
  385. catch (Exception ex)
  386. {
  387. Console.WriteLine("写入PLC_bool出错:" + ex.Message);
  388. }
  389. }
  390. /// <summary>
  391. /// 写入PLC DB块16位有符号数据
  392. /// </summary>
  393. /// <param name="绝对地址"></param>
  394. /// <param name="值"></param>
  395. public void Set_int(string 绝对地址, short)
  396. {
  397. try
  398. {
  399. if (plc.IsConnected)
  400. {
  401. plc.Write(绝对地址.ToUpper(), 值);
  402. }
  403. }
  404. catch (Exception ex)
  405. {
  406. Console.WriteLine("写入PLC_int出错:" + ex.Message);
  407. }
  408. }
  409. /// <summary>
  410. /// 写入PLC DB块32位有符号数据
  411. /// </summary>
  412. /// <param name="绝对地址"></param>
  413. /// <param name="值"></param>
  414. public void Set_Dint(string 绝对地址, int)
  415. {
  416. try
  417. {
  418. if (plc.IsConnected)
  419. {
  420. plc.Write(绝对地址.ToUpper(), 值);
  421. }
  422. }
  423. catch (Exception ex)
  424. {
  425. Console.WriteLine("写入PLC_Dint出错:" + ex.Message);
  426. }
  427. }
  428. /// <summary>
  429. /// 写入PLC DB块16位无符号数据
  430. /// </summary>
  431. /// <param name="绝对地址"></param>
  432. /// <param name="值"></param>
  433. public void Set_Word(string 绝对地址, ushort)
  434. {
  435. try
  436. {
  437. if (plc.IsConnected)
  438. {
  439. plc.Write(绝对地址.ToUpper(), 值);
  440. }
  441. }
  442. catch (Exception ex)
  443. {
  444. Console.WriteLine("写入PLC_Word出错:" + ex.Message);
  445. }
  446. }
  447. /// <summary>
  448. /// 写入PLC DB块32位无符号数据
  449. /// </summary>
  450. /// <param name="绝对地址"></param>
  451. /// <param name="值"></param>
  452. public void Set_DWord(string 绝对地址, uint)
  453. {
  454. try
  455. {
  456. if (plc.IsConnected)
  457. {
  458. plc.Write(绝对地址.ToUpper(), 值);
  459. }
  460. }
  461. catch (Exception ex)
  462. {
  463. Console.WriteLine("写入PLC_DWord出错:" + ex.Message);
  464. }
  465. }
  466. /// <summary>
  467. /// 写入PLC Real 类型地址
  468. /// </summary>
  469. /// <param name="绝对地址"></param>
  470. /// <param name="值"></param>
  471. public void Set_Real(string 绝对地址, float)
  472. {
  473. try
  474. {
  475. if (plc.IsConnected)
  476. {
  477. plc.Write(绝对地址.ToUpper(), 值);
  478. }
  479. }
  480. catch (Exception ex)
  481. {
  482. Console.WriteLine("写入PLC_Real出错:" + ex.Message);
  483. }
  484. }
  485. /// <summary>
  486. /// 写入PLC Timer 类型地址
  487. /// </summary>
  488. /// <param name="绝对地址"></param>
  489. /// <param name="值"></param>
  490. public void Set_Timer(string 绝对地址, int)
  491. {
  492. try
  493. {
  494. if (plc.IsConnected)
  495. {
  496. plc.Write(绝对地址.ToUpper(), 值);
  497. }
  498. }
  499. catch (Exception ex)
  500. {
  501. Console.WriteLine("写入PLC_Timer出错:" + ex.Message);
  502. }
  503. }
  504. /// <summary>
  505. /// 写入PLC string 类型地址
  506. /// </summary>
  507. /// <param name="DB">DB块</param>
  508. /// <param name="偏移地址">分配的偏移量</param>
  509. /// <param name="值"></param>
  510. public void Set_String(int DB, int 偏移量, string Values)
  511. {
  512. try
  513. {
  514. if (plc.IsConnected)
  515. {
  516. var lenMax = (byte)plc.Read(DataType.DataBlock, DB, 偏移量, VarType.Byte, 1);//拿到地址申请的字节长度
  517. plc.Write(DataType.DataBlock, DB, 偏移量, GetStringByteArray(lenMax, Values));
  518. }
  519. }
  520. catch (Exception ex)
  521. {
  522. Console.WriteLine("写入PLC_String出错:" + ex.Message);
  523. }
  524. }
  525. /// <summary>
  526. /// 把 string数据 转换成西门子String数据格式
  527. /// </summary>
  528. /// <param name="datastring"></param>
  529. /// <returns></returns>
  530. private byte[] GetStringByteArray(byte LenMax, string Values)
  531. {
  532. byte[] byteArray = Encoding.Default.GetBytes(Values);
  533. byte[] sheel = new byte[2];
  534. sheel[0] = Convert.ToByte(LenMax);//申请的最大内存
  535. sheel[1] = Convert.ToByte(Values.Length);//当前字符长度
  536. return sheel.Concat(byteArray).ToArray();
  537. }
  538. #endregion
  539. }
  540. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/290075
推荐阅读
相关标签
  

闽ICP备14008679号