using System; using System.Drawing; using System.Collections; using System.Windows.Forms; using System.Data; //using System.Threading; //using System.Runtime.InteropServices; namespace SmartDeviceApplication1 { /// /// Form1 的摘要描述。 /// public class Form1 : System.Windows.Forms.Form { internal System.Windows.Forms.ListBox ListBox2; internal System.Windows.Forms.TextBox TextBox16; internal System.Windows.Forms.TextBox TextBox15; internal System.Windows.Forms.TextBox TextBox14; internal System.Windows.Forms.Label Label4; internal System.Windows.Forms.TextBox TextBox13; internal System.Windows.Forms.Label Label3; internal System.Windows.Forms.TextBox TextBox10; internal System.Windows.Forms.TextBox TextBox9; internal System.Windows.Forms.TextBox TextBox8; internal System.Windows.Forms.TextBox TextBox7; internal System.Windows.Forms.TextBox TextBox6; internal System.Windows.Forms.Label Label1; internal System.Windows.Forms.TextBox TextBox5; internal System.Windows.Forms.TextBox TextBox4; internal System.Windows.Forms.TextBox TextBox3; internal System.Windows.Forms.TextBox TextBox2; internal System.Windows.Forms.TextBox TextBox1; internal System.Windows.Forms.Button Button5; internal System.Windows.Forms.Button Button4; internal System.Windows.Forms.Button Button3; internal System.Windows.Forms.Button Button2; internal System.Windows.Forms.Button Button1; internal System.Windows.Forms.Label Label2; internal System.Windows.Forms.Label Label5; internal System.Windows.Forms.Label Label6; internal System.Windows.Forms.Label Label7; internal System.Windows.Forms.Label Label8; //[DllImport("Modbus.dll")] //public static extern short Get_AD_HValue(); int m_list1,m_list2; short m_WRegAd,m_WRegVal,m_WCoilAd,m_WCoilVal; int c,d,e1,f,Rtn1,Rtn2,Rtn3,Rtn4,RR; internal System.Windows.Forms.ListBox ListBox1; public Form1() { // // Windows Form 設計工具支援的必要項 // InitializeComponent(); // // TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼 // } /// /// 清除任何使用中的資源。 /// protected override void Dispose( bool disposing ) { base.Dispose( disposing ); } #region Windows Form 設計工具產生的程式碼 /// /// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改 /// 這個方法的內容。 /// private void InitializeComponent() { this.ListBox2 = new System.Windows.Forms.ListBox(); this.TextBox16 = new System.Windows.Forms.TextBox(); this.TextBox15 = new System.Windows.Forms.TextBox(); this.TextBox14 = new System.Windows.Forms.TextBox(); this.Label4 = new System.Windows.Forms.Label(); this.TextBox13 = new System.Windows.Forms.TextBox(); this.Label3 = new System.Windows.Forms.Label(); this.TextBox10 = new System.Windows.Forms.TextBox(); this.TextBox9 = new System.Windows.Forms.TextBox(); this.TextBox8 = new System.Windows.Forms.TextBox(); this.TextBox7 = new System.Windows.Forms.TextBox(); this.TextBox6 = new System.Windows.Forms.TextBox(); this.Label1 = new System.Windows.Forms.Label(); this.TextBox5 = new System.Windows.Forms.TextBox(); this.TextBox4 = new System.Windows.Forms.TextBox(); this.TextBox3 = new System.Windows.Forms.TextBox(); this.TextBox2 = new System.Windows.Forms.TextBox(); this.TextBox1 = new System.Windows.Forms.TextBox(); this.Button5 = new System.Windows.Forms.Button(); this.Button4 = new System.Windows.Forms.Button(); this.Button3 = new System.Windows.Forms.Button(); this.Button2 = new System.Windows.Forms.Button(); this.Button1 = new System.Windows.Forms.Button(); this.Label2 = new System.Windows.Forms.Label(); this.Label5 = new System.Windows.Forms.Label(); this.Label6 = new System.Windows.Forms.Label(); this.Label7 = new System.Windows.Forms.Label(); this.Label8 = new System.Windows.Forms.Label(); this.ListBox1 = new System.Windows.Forms.ListBox(); // // ListBox2 // this.ListBox2.Items.Add("19200"); this.ListBox2.Items.Add("9600"); this.ListBox2.Location = new System.Drawing.Point(201, 25); this.ListBox2.Size = new System.Drawing.Size(100, 26); // // TextBox16 // this.TextBox16.Location = new System.Drawing.Point(224, 248); this.TextBox16.Size = new System.Drawing.Size(56, 22); this.TextBox16.Text = "12345"; // // TextBox15 // this.TextBox15.Location = new System.Drawing.Point(128, 248); this.TextBox15.Size = new System.Drawing.Size(31, 22); this.TextBox15.Text = "17"; // // TextBox14 // this.TextBox14.Location = new System.Drawing.Point(224, 208); this.TextBox14.Size = new System.Drawing.Size(24, 22); this.TextBox14.Text = "1"; // // Label4 // this.Label4.Location = new System.Drawing.Point(184, 208); this.Label4.Size = new System.Drawing.Size(32, 20); this.Label4.Text = "Value"; // // TextBox13 // this.TextBox13.Location = new System.Drawing.Point(128, 208); this.TextBox13.Size = new System.Drawing.Size(31, 22); this.TextBox13.Text = "501"; // // Label3 // this.Label3.Location = new System.Drawing.Point(145, 25); this.Label3.Size = new System.Drawing.Size(48, 20); this.Label3.Text = "Baudrate"; // // TextBox10 // this.TextBox10.Location = new System.Drawing.Point(264, 160); this.TextBox10.Size = new System.Drawing.Size(48, 22); this.TextBox10.Text = "0"; // // TextBox9 // this.TextBox9.Location = new System.Drawing.Point(208, 160); this.TextBox9.Size = new System.Drawing.Size(48, 22); this.TextBox9.Text = "0"; // // TextBox8 // this.TextBox8.Location = new System.Drawing.Point(152, 160); this.TextBox8.Size = new System.Drawing.Size(48, 22); this.TextBox8.Text = "0"; // // TextBox7 // this.TextBox7.Location = new System.Drawing.Point(96, 160); this.TextBox7.Size = new System.Drawing.Size(48, 22); this.TextBox7.Text = "0"; // // TextBox6 // this.TextBox6.Location = new System.Drawing.Point(40, 160); this.TextBox6.Size = new System.Drawing.Size(48, 22); this.TextBox6.Text = "0"; // // Label1 // this.Label1.Location = new System.Drawing.Point(41, 25); this.Label1.Size = new System.Drawing.Size(56, 20); this.Label1.Text = "COM Port"; // // TextBox5 // this.TextBox5.Location = new System.Drawing.Point(168, 96); this.TextBox5.Size = new System.Drawing.Size(24, 22); this.TextBox5.Text = "0"; // // TextBox4 // this.TextBox4.Location = new System.Drawing.Point(136, 96); this.TextBox4.Size = new System.Drawing.Size(24, 22); this.TextBox4.Text = "0"; // // TextBox3 // this.TextBox3.Location = new System.Drawing.Point(104, 96); this.TextBox3.Size = new System.Drawing.Size(24, 22); this.TextBox3.Text = "0"; // // TextBox2 // this.TextBox2.Location = new System.Drawing.Point(72, 96); this.TextBox2.Size = new System.Drawing.Size(24, 22); this.TextBox2.Text = "0"; // // TextBox1 // this.TextBox1.Location = new System.Drawing.Point(40, 96); this.TextBox1.Size = new System.Drawing.Size(24, 22); this.TextBox1.Text = "0"; // // Button5 // this.Button5.Location = new System.Drawing.Point(320, 248); this.Button5.Text = "WReg"; this.Button5.Click += new System.EventHandler(this.Button5_Click); // // Button4 // this.Button4.Location = new System.Drawing.Point(320, 208); this.Button4.Text = "WCoil"; this.Button4.Click += new System.EventHandler(this.Button4_Click); // // Button3 // this.Button3.Location = new System.Drawing.Point(320, 160); this.Button3.Text = "RRegs"; this.Button3.Click += new System.EventHandler(this.Button3_Click); // // Button2 // this.Button2.Location = new System.Drawing.Point(320, 96); this.Button2.Text = "RCoils"; this.Button2.Click += new System.EventHandler(this.Button2_Click); // // Button1 // this.Button1.Location = new System.Drawing.Point(321, 25); this.Button1.Text = "Create"; this.Button1.Click += new System.EventHandler(this.Button1_Click); // // Label2 // this.Label2.Location = new System.Drawing.Point(40, 208); this.Label2.Size = new System.Drawing.Size(88, 20); this.Label2.Text = "Write Address"; // // Label5 // this.Label5.Location = new System.Drawing.Point(40, 248); this.Label5.Size = new System.Drawing.Size(88, 20); this.Label5.Text = "Write Address"; // // Label6 // this.Label6.Location = new System.Drawing.Point(184, 248); this.Label6.Size = new System.Drawing.Size(32, 20); this.Label6.Text = "Value"; // // Label7 // this.Label7.Location = new System.Drawing.Point(40, 136); this.Label7.Size = new System.Drawing.Size(96, 20); this.Label7.Text = "Address 16~20"; // // Label8 // this.Label8.Location = new System.Drawing.Point(40, 72); this.Label8.Size = new System.Drawing.Size(104, 20); this.Label8.Text = "Address 500~504"; // // ListBox1 // this.ListBox1.Items.Add("3"); this.ListBox1.Location = new System.Drawing.Point(104, 24); this.ListBox1.Size = new System.Drawing.Size(31, 26); // // Form1 // this.ClientSize = new System.Drawing.Size(434, 296); this.Controls.Add(this.ListBox2); this.Controls.Add(this.ListBox1); this.Controls.Add(this.TextBox16); this.Controls.Add(this.TextBox15); this.Controls.Add(this.TextBox14); this.Controls.Add(this.Label4); this.Controls.Add(this.TextBox13); this.Controls.Add(this.Label3); this.Controls.Add(this.TextBox10); this.Controls.Add(this.TextBox9); this.Controls.Add(this.TextBox8); this.Controls.Add(this.TextBox7); this.Controls.Add(this.TextBox6); this.Controls.Add(this.Label1); this.Controls.Add(this.TextBox5); this.Controls.Add(this.TextBox4); this.Controls.Add(this.TextBox3); this.Controls.Add(this.TextBox2); this.Controls.Add(this.TextBox1); this.Controls.Add(this.Button5); this.Controls.Add(this.Button4); this.Controls.Add(this.Button3); this.Controls.Add(this.Button2); this.Controls.Add(this.Button1); this.Controls.Add(this.Label2); this.Controls.Add(this.Label5); this.Controls.Add(this.Label6); this.Controls.Add(this.Label7); this.Controls.Add(this.Label8); this.Location = new System.Drawing.Point(100, 50); this.MaximizeBox = false; this.MinimizeBox = false; this.Text = "Modbus_VC#.Net_Demo"; this.Load += new System.EventHandler(this.Form1_Load); } #endregion /// /// 應用程式的主進入點。 /// static void Main() { Application.Run(new Form1()); } private void Button1_Click(object sender, System.EventArgs e) { m_list1 = 3; m_list2 = 19200; c = 0; d = 8; e1 = 1; f = 2000; // char[] aa; string bb="192.168.0.11"; // aa = bb.ToCharArray(); byte[] aa1=System.Text.Encoding.ASCII.GetBytes(bb); // Rtn1 = Wincon.ModbusRTU.MBRTUInit(m_list1, m_list2, c, d, e1, f); RR = Wincon.ModbusTCP.MBTCPInit(1, aa1, 502, 500); if(RR == 0) { MessageBox.Show("Initial Success"); } else { MessageBox.Show("Initial Fail"); } } private void Button2_Click(object sender, System.EventArgs e) { int r1, r2, r3, r4, r6; byte [] RC5 = new byte[5]; r1 = m_list1; r2 = 1; r3 = 500; r4 = 5; r6 = 1; //for(int i=0;i<5;i++) //{ // Rtn2 = Wincon.ModbusRTU.MBRTU_R_Coils(r1,r2,r3,r4,RC5,r6); Rtn3 = Wincon.ModbusTCP.MBTCP_R_Coils(1,r2,r3,r4,RC5,r6); //} if(Rtn2 == 0) { MessageBox.Show("RCoils Success"); TextBox1.Text = System.Convert.ToString(RC5[0]); TextBox2.Text = System.Convert.ToString(RC5[1]); TextBox3.Text = System.Convert.ToString(RC5[2]); TextBox4.Text = System.Convert.ToString(RC5[3]); TextBox5.Text = System.Convert.ToString(RC5[4]); } else { MessageBox.Show("Rcoils Fail"); } } private void Button3_Click(object sender, System.EventArgs e) { int rr1,rr2,rr3,rr4,rr6,RRtn5; short [] RRC5 = new short[5]; rr1 = m_list1; rr2 = 1; rr3 = 16; rr4 = 5; rr6 = 3; RRtn5 = Wincon.ModbusRTU.MBRTU_R_Registers(rr1, rr2, rr3, rr4, RRC5, rr6); if(RRtn5 == 0) { MessageBox.Show("RRegs Success"); TextBox6.Text = System.Convert.ToString(RRC5[0]); TextBox7.Text = System.Convert.ToString(RRC5[1]); TextBox8.Text = System.Convert.ToString(RRC5[2]); TextBox9.Text = System.Convert.ToString(RRC5[3]); TextBox10.Text = System.Convert.ToString(RRC5[4]); } else { MessageBox.Show("RRegs Fail"); } } private void Button4_Click(object sender, System.EventArgs e) { int w1,w2,w3; int w4; m_WCoilAd = 501; m_WCoilVal = 1; w1 = m_list1; w2 = 1; w3 = m_WCoilAd; w4 = m_WCoilVal; Rtn3 = Wincon.ModbusRTU.MBRTU_W_Coil(w1, w2, w3, w4); if(Rtn3 == 0) { MessageBox.Show("WCoil Success"); } else { MessageBox.Show("WCoil Fail"); } } private void Button5_Click(object sender, System.EventArgs e) { int w1,w2,w3; short w4; m_WRegAd = 17; m_WRegVal = 12345; w1 = m_list1; w2 = 1; w3 = m_WRegAd; w4 = m_WRegVal; // short [] w5= new short[4]; // w5[0] = 11; // w5[1] = 22; // w5[2] = 33; // w5[3] = 44; Rtn4 = Wincon.ModbusRTU.MBRTU_W_Register(w1, w2, w3, w4); // Rtn4 = Wincon.ModbusTCP.MBTCP_W_Register(1,w2, w3, w4); // Rtn4 = Wincon.ModbusTCP.MBTCP_W_Multi_Registers(1,1,500,4,w5); if (Rtn4 == 0) { MessageBox.Show("WReg Success"); // short [] RRC5 = new short[5]; // Wincon.ModbusTCP.MBTCP_R_Registers(1,1,499,5,RRC5,3); // TextBox6.Text = System.Convert.ToString(RRC5[0]); // TextBox7.Text = System.Convert.ToString(RRC5[1]); // TextBox8.Text = System.Convert.ToString(RRC5[2]); // TextBox9.Text = System.Convert.ToString(RRC5[3]); // TextBox10.Text = System.Convert.ToString(RRC5[4]); } else { MessageBox.Show("WReg Fail"); } } private void Form1_Load(object sender, System.EventArgs e) { } } }