一、环境
- c#:.net core、s7netplus
- 西门子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通讯失败!");
}
}