123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using S7.Net;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace xicheji
- {
- public partial class Form1 : Form
- {
- private Plc plc;
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- plc = new Plc(CpuType.S7200, "127.0.0.1", 1502, 0, 1);
- plc.Open();
- richTextBox1.AppendText("连接成功!\n\n");
- richTextBox1.ScrollToCaret();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message + ex.StackTrace);
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- if (plc == null || !plc.IsConnected) return;
- //plc中类型与c#类型 bool => Bit
- //Byte => byte
- //word => ushort
- //DWord => uint
- //Int => short
- //DInt => int
- //Real => float
- //LReal => double
- //String => string
- //DateTimeLong=>datetime
- //s7wstring=>string
- try
- {
- var db = 1;
- var start = plc.Read(DataType.DataBlock, db, 0, VarType.Byte, 1);
- Log("start:", start);
- var length = plc.Read(DataType.DataBlock, db, 1, VarType.Byte, 1);
- Log("length:", length);
- var mac = plc.Read(DataType.DataBlock, db, 2, VarType.Byte, 6);
- Log("mac:", string.Join(":", (mac as byte[]).ToList().ConvertAll(a => "0x" + a.ToString("X2"))));
- var val = plc.Read(DataType.DataBlock, db, 10, VarType.Bit, 1, 1);
- Log("DB1.DBX10.1的值为:", val);
- val = plc.Read(DataType.DataBlock, db, 10, VarType.Bit, 1, 2);
- Log("DB1.DBX10.2的值为:", val);
- val = plc.Read(DataType.DataBlock, db, 20, VarType.DWord, 1);
- Log("读取DB1.DBX20 DWORD的洗车次数值为:", val);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message + ex.StackTrace);
- }
- }
- private void Log(params object[] msgs)
- {
- richTextBox1.AppendText(string.Join(" ", msgs));
- richTextBox1.AppendText("\n\n");
- richTextBox1.ScrollToCaret();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- plc?.Close();
- }
- }
- }
|