MainForm.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using S7.Net;
  2. using S7.Net.Types;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace xicheji
  13. {
  14. public partial class MainForm : Form
  15. {
  16. private Plc plc;
  17. public MainForm()
  18. {
  19. InitializeComponent();
  20. }
  21. private void button1_Click(object sender, EventArgs e)
  22. {
  23. try
  24. {
  25. plc = new Plc(CpuType.S7200, "127.0.0.1", 1502, 0, 1);
  26. plc.Open();
  27. richTextBox1.AppendText("连接成功!\n\n");
  28. richTextBox1.ScrollToCaret();
  29. }
  30. catch (Exception ex)
  31. {
  32. MessageBox.Show(ex.Message + ex.StackTrace);
  33. }
  34. }
  35. private void button2_Click(object sender, EventArgs e)
  36. {
  37. if (plc == null || !plc.IsConnected) return;
  38. //plc中类型与c#类型 bool => Bit
  39. //Byte => byte
  40. //word => ushort
  41. //DWord => uint
  42. //Int => short
  43. //DInt => int
  44. //Real => float
  45. //LReal => double
  46. //String => string
  47. //DateTimeLong=>datetime
  48. //s7wstring=>string
  49. //PLCAddress.Parse
  50. //1、DB100.DBB0 一个字节有8个位,分别为0-- - 7!例:0.0----0.7共8位 --Byte,byte
  51. //2、DB100.DBW0一个字有两个字节,分别为 DB100.DBB0和 DB100.DBB1 --Word,ushort
  52. //3、DB100.DBD0一个双字有两个字,分别为 DB100.DBW0和 DB100.DBW2 --DWord,uint
  53. //4、DB100.DBX0.0 一个位,这是最小单位 --Bit,bool
  54. try
  55. {
  56. var db = 1;
  57. var start = plc.Read(DataType.DataBlock, db, 0, VarType.Byte, 1);
  58. Log("start:", start);
  59. var start2 = plc.Read("DB1.DBB0");
  60. Log("start2:", start2);
  61. var length = plc.Read(DataType.DataBlock, db, 1, VarType.Byte, 1);
  62. Log("length:", length);
  63. var length2 = plc.Read("DB1.DBB1");
  64. Log("length2:", length2);
  65. var mac = plc.Read(DataType.DataBlock, db, 2, VarType.Byte, 6);
  66. Log("mac:", string.Join(":", (mac as byte[]).ToList().ConvertAll(a => "0x" + a.ToString("X2"))));
  67. var mac2 = plc.Read("DB1.DBD2-6");
  68. Log("mac2:", mac2);
  69. var val = plc.Read(DataType.DataBlock, db, 10, VarType.Bit, 1, 1);
  70. Log("DB1.DBX10.1的值为:", val);
  71. val = plc.Read(DataType.DataBlock, db, 10, VarType.Bit, 1, 2);
  72. Log("DB1.DBX10.2的值为:", val);
  73. val = plc.Read(DataType.DataBlock, db, 20, VarType.DWord, 1);
  74. Log("读取DB1.DBX20 DWORD的洗车次数值为:", val);
  75. var bbb = (bool)plc.Read("DB1.DBX10.1");
  76. Log("bbb:", bbb);
  77. var ccc = plc.Read("DB1.DBD20");
  78. Log("ccc:", ccc);
  79. //DWord.FromByteArray
  80. }
  81. catch (Exception ex)
  82. {
  83. MessageBox.Show(ex.Message + ex.StackTrace);
  84. }
  85. }
  86. private void Log(params object[] msgs)
  87. {
  88. richTextBox1.AppendText(string.Join(" ", msgs));
  89. richTextBox1.AppendText("\n\n");
  90. richTextBox1.ScrollToCaret();
  91. }
  92. private void button3_Click(object sender, EventArgs e)
  93. {
  94. plc?.Close();
  95. }
  96. }
  97. }