"STM32. Net MF Development Board Learning-08" remote PLC Read and write control

Source: Internet
Author: User
Tags sleep thread

In industrial control projects, PLC is generally necessary, on-site control is generally at its core, and PC systems generally only play the role of remote monitoring, graphics and data storage. In addition to the PLC, PC and PLC communication between, general some intelligent display module (such as the different types of touch-screen HMI system) and PLC communication, we will put the host. NET MF System to create a simple HMI system, REMOTE Control PLC.

General foreign each PLC factory, its communication protocol is generally different, such as Siemens PLC PPI/MPI, Omron plc hostlink, AB plc DF1, Schneider plc Modbus and so on, because the Modbus protocol is simple and open, So some of the domestic PLC and intelligent modules are mostly support Modbus agreement, Siemens 200 series of PLC as long as the loading of an official Modbus library, but also can be very convenient to support the Modbus agreement.

In the previous article "PC through the Modbus Protocol Remote Control Development Board", we put the Modbus Rtu client on the PC side, this time we transfer this part of the program to the. NET Micro framework system, let. NET Micro The Framework Development Board directly accesses the PLC.

The difference is that the Red Bull development of COM3 directly support RS485 communication, so we and the PLC directly connected with two lines can be communicated, in addition to the RS485 is Half-duplex communication, so you need to manually send and receive a switch, in addition to the RS232 communication code is basically consistent.

The different code is as follows:

//发送数据
      if (RS485)
      {
        RTS.Write(true);
      }
      serial.Write(bytSendData, 0, intSendNum);
      if (RS485)
      {
        while (serial.BytesToWrite > 0) ;
        RTS.Write(false);
      }

The specific test code is as follows:

public static void Main ()
{
Client mbclient = new Client ();
Mbclient. RS485 = true;
Mbclient. Rts_pin = (cpu.pin) gpio_names. PF11;
Mbclient. Open ("COM3", 19200, System.IO.Ports.Parity.None);
Uint16[] Data=new uint16[3];
Graphics.clear (Color.Black);
Graphics.FillRectangle (0, 0, 239, color.white);
Graphics.DrawString (3, "Modbus Rtu Test", Color.Blue);
byte QW0 = 0;
while (true)
{
if (++qw0 > 128) QW0 = 0;
Mbclient. Write (1, 0, new uint16[] {QW0}, 1);
Thread.Sleep (300);

if (mbclient. Read (1,0,data,3) = = 0)
{
Graphics.suspendlayout ();
Graphics.FillRectangle (0, 239, MB, color.black);
Graphics.DrawString, "QW0:" + data[0]. ToString (), color.yellow);
Graphics.DrawString, "IW0:" + data[1]. ToString (), color.lightgreen);
Graphics.DrawString, "VW2:" +data[2]. ToString (), color.orange);
Graphics.resumelayout ();
}
Thread.Sleep (300);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.