c#通讯西门子plc

一、环境

  1. c#:.net core、s7netplus
  2. 西门子plc:s7200smart

二、代码

 //通讯plc
        private void OpenPlc()
        {
            plcS7 = new Plc(CpuType.S7200Smart, "192.168.3.2", 0, 1);
            if (!plcS7.IsConnected)
            {
                try
                {
                    plcS7.Open();
                    _logger.LogCritical("PLC通讯成功!");

                    plcInventory = (ushort[])plcS7.Read(DataType.DataBlock, 1, 4482, VarType.Word, 279, 1);
                    plcSpecification = (float[])plcS7.Read(DataType.DataBlock, 1, 0, VarType.Real, 279, 1);

                    MergeInventory();
                }
                catch (Exception e)
                {
                    plcData.PlcState = false;
                    _logger.LogCritical("采集数据异常,点位错误。" + e.ToString());
                }
            }
            else
            {
                plcData.PlcState = false;
                _logger.LogCritical("PLC通讯失败!");
            }
        }
上一篇:java代码审计学习笔记


下一篇:PLC是如何执行中断?又是如何去应用的?