1. First of all, I need to test the communication of PLC. I use XCOM serial port assistant to check the instruction manual of Delta PLC. I see that the default is the ASCII mode of MODBUS protocol. Specifically, I need to test the input and output according to the instruction manual. During this period, I encountered the following problems:
1) The communication format must be set correctly. The data bit, baud rate, parity, check code, etc. and the slave address may not be 01 by default, which can be viewed through the PLC
2) Do not put spaces in ASCII. Symbols are in English when needed. CRLF is a carriage return key. It is better to convert it to hexadecimal to observe whether it is the same as the hexadecimal in the manual
3) Before the test, the communication can be tested with the software provided by delta to ensure that the 485 connection of the equipment is correct.
4) When debugging with XCOM serial assistant, the phenomenon of garbled code appeared. Later, I tried to use Donald Duck to avoid the phenomenon of garbled code. (spent a day debugging, always thought it was a hardware problem)
2. Serial port class can be used in C ා, and the communication format is set the same as in the specification.
3. NMODBUS4 is used in modbus Protocol Part C, which can be downloaded directly from NuGet package under vs management
It includes RTU\ASCII\TCP mode. I asked my little partner in charge of PLC to help me change the communication format of PLC to RTU. Specifically, I modified M1320. The program was not clear
It includes the reading and writing methods of MODBUS, which are one-to-one corresponding to those in the manual. In terms of reading, it is recommended to open a thread to read data all the time. (static method is used here)
Set master
Read data
The thread needs to be established here, and the process needs to be operated in the thread, and the Invoke() method needs to be used, so as to avoid the error of the interface when the process is not drawn, and the interface IsHandleCreated needs to be judged in advance
void Method1() { if (!this.IsHandleCreated) { while (true) { Thread.Sleep(100); //Y = InitializeModel.master.ReadCoils(1, 1281, 1); Auto = InitializeModel.master.ReadCoils(1, 1280, 3); C = InitializeModel.master.ReadHoldingRegisters(1, 3584, 10); //C = InitializeModel.master.ReadInputRegisters(1, 3784, 1); Hand = InitializeModel.master.ReadCoils(1, 524 + 2048, 636 - 524 + 1); label6.Invoke(new Action(()=>{ label6.Text = C[0].ToString(); })); label5.Invoke(new Action(() => { label5.Text = C[6].ToString(); })); label7.Invoke(new Action(() => { label7.Text = C[2].ToString(); })); label8.Invoke(new Action(() => { label8.Text = C[4].ToString(); })); if (Auto[0]) { pictureBox2.Invoke(new Action(() => { pictureBox2.Visible = true; })); pictureBox4.Invoke(new Action(() => { pictureBox4.Visible = false; })); } else { pictureBox4.Invoke(new Action(() => { pictureBox4.Visible = true; })); pictureBox2.Invoke(new Action(() => { pictureBox2.Visible = false; })); } if (Auto[1]) { //pictureBox23.Invoke(new Action(() => { pictureBox23.Visible = true; })); pictureBox5.Invoke(new Action(() => { pictureBox5.Visible = true; })); pictureBox6.Invoke(new Action(() => { pictureBox6.Visible = false; })); } else { pictureBox5.Invoke(new Action(() => { pictureBox5.Visible = false; })); pictureBox6.Invoke(new Action(() => { pictureBox6.Visible = true; })); } if (Auto[2]) { pictureBox3.Invoke(new Action(() => { pictureBox3.Visible = true; })); pictureBox1.Invoke(new Action(() => { pictureBox1.Visible = false; })); } else { pictureBox3.Invoke(new Action(() => { pictureBox3.Visible = false; })); pictureBox1.Invoke(new Action(() => { pictureBox1.Visible = true; })); } //Feeding state } } }
Writing data
Here, the PLC needs to read the rising edge, and directly use the Click event method to set it to zero again and again, but in fact, more similar to HMI, it should use MouseDown and MouseUp events, and set Down to one and UP to zero
The specific interface is as follows: