using S7.Net; using System.Windows.Forms; namespace xicheji { public partial class MainForm : Form { private Plc? plc = null; public MainForm() { 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 //PLCAddress.Parse //1��DB100.DBB0 һ���ֽ���8��λ���ֱ�Ϊ0-- - 7������0.0----0.7��8λ --Byte,byte //2��DB100.DBW0һ�����������ֽڣ��ֱ�Ϊ DB100.DBB0�� DB100.DBB1 --Word,ushort //3��DB100.DBD0һ��˫���������֣��ֱ�Ϊ DB100.DBW0�� DB100.DBW2 --DWord,uint //4��DB100.DBX0.0 һ��λ��������С��λ --Bit,bool try { var db = 1; var start = plc.Read(DataType.DataBlock, db, 0, VarType.Byte, 1); Log("start:", start); var start2 = plc.Read("DB1.DBB0"); Log("start2:", start2); var length = plc.Read(DataType.DataBlock, db, 1, VarType.Byte, 1); Log("length:", length); var length2 = plc.Read("DB1.DBB1"); Log("length2:", length2); 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); var bbb = (bool)plc.Read("DB1.DBX10.1"); Log("bbb:", bbb); var ccc = plc.Read("DB1.DBD20"); Log("ccc:", ccc); //DWord.FromByteArray } 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(); } } }