当前位置:网站首页>Visual system design example (Halcon WinForm) -10. PLC communication
Visual system design example (Halcon WinForm) -10. PLC communication
2022-07-27 14:49:00 【11eleven】
Industrial vision projects are inseparable from the inferior PLC Data communication , For example, when the product is in place, notify the visual department to take photos for detection , Vision starts to trigger the light source camera to take pictures Image processing , The final result output forms a closed loop , Like our commonly used Siemens PLC Mitsubishi PLC And so on have corresponding communication methods , Most of them define the address of the communication mark , Perform the address polling check and perform the corresponding operation . This article takes Siemens as an example , Siemens S7 agreement , Used Hsl The open source protocol library of encapsulates a pair of S7 The connection reading and writing method of the Protocol .


NUGET Install class library 7.0 Free version .
public bool Connect(out ResultMessage resut, string serverUrl = "192.168.10.10", int port = 4000, PLCModelEnum pLCModel = PLCModelEnum.S7_1200)
{
if (pLCModel == PLCModelEnum.S7_1200)
{
if (Plc == null)
{
Plc = new SiemensS7Net( SiemensPLCS.S1200,serverUrl)
{
ConnectTimeOut = 5000,
ReceiveTimeOut = 500
};
}
try
{
var open = Plc.ConnectServer();
if (open.IsSuccess)
{
IsConnect = true;
//PLC Open the success
resut = new ResultMessage()
{
IsSuccess = true,
ErrorMsg = open.Message
};
return true;
}
else
{
IsConnect = false;
Plc = null;
resut = new ResultMessage()
{
IsSuccess = false,
ErrorMsg = open.Message
};
return false;
}
}
catch (Exception ex)
{
IsConnect = false;
Plc = null;
resut = new ResultMessage()
{
IsSuccess = false,
ErrorMsg = $"Connect PLC Error:{ex.Message}"
};
return false;
}
}
else
{
IsConnect = false;
resut = new ResultMessage()
{
IsSuccess = false,
ErrorMsg = "Unknow PLC Model"
};
return false;
}
}
public T ReadNode<T>(string tag, out ResultMessage result, ushort length = 0)
{
T ret = default;
if (Plc == null)
{
result = new ResultMessage()
{
IsSuccess = false,
ErrorMsg = " Connection not initialized "
};
return ret;
}
result = default;
Type type = typeof(T);
switch (type.Name)
{
case nameof(Boolean):
{
var r = Plc.ReadBool(tag);
result = new ResultMessage()
{
IsSuccess = r.IsSuccess,
ErrorMsg = r.Message
};
if (r.IsSuccess)
ret = (T)Convert.ChangeType(r.Content, typeof(T));
}
break;
case nameof(Int16):
{
var r = Plc.ReadInt16(tag);
result = new ResultMessage()
{
IsSuccess = r.IsSuccess,
ErrorMsg = r.Message
};
if (r.IsSuccess)
ret = (T)Convert.ChangeType(r.Content, typeof(T));
}
break;
case nameof(UInt16):
{
var r = Plc.ReadUInt16(tag);
result = new ResultMessage()
{
IsSuccess = r.IsSuccess,
ErrorMsg = r.Message
};
if (r.IsSuccess)
ret = (T)Convert.ChangeType(r.Content, typeof(T));
}
break;
case nameof(Int32):
{
var r = Plc.ReadInt32(tag);
result = new ResultMessage()
{
IsSuccess = r.IsSuccess,
ErrorMsg = r.Message
};
if (r.IsSuccess)
ret = (T)Convert.ChangeType(r.Content, typeof(T));
}
break;
case nameof(UInt32):
{
var r = Plc.ReadUInt32(tag);
result = new ResultMessage()
{
IsSuccess = r.IsSuccess,
ErrorMsg = r.Message
};
if (r.IsSuccess)
ret = (T)Convert.ChangeType(r.Content, typeof(T));
}
break;
case nameof(Int64):
{
var r = Plc.ReadInt64(tag);
result = new ResultMessage()
{
IsSuccess = r.IsSuccess,
ErrorMsg = r.Message
};
if (r.IsSuccess)
ret = (T)Convert.ChangeType(r.Content, typeof(T));
}
break;
case nameof(UInt64):
{
var r = Plc.ReadUInt64(tag);
result = new ResultMessage()
{
IsSuccess = r.IsSuccess,
ErrorMsg = r.Message
};
if (r.IsSuccess)
ret = (T)Convert.ChangeType(r.Content, typeof(T));
}
break;
case nameof(Single):
{
var r = Plc.ReadFloat(tag);
result = new ResultMessage()
{
IsSuccess = r.IsSuccess,
ErrorMsg = r.Message
};
if (r.IsSuccess)
ret = (T)Convert.ChangeType(r.Content, typeof(T));
}
break;
case nameof(Double):
{
var r = Plc.ReadDouble(tag);
result = new ResultMessage()
{
IsSuccess = r.IsSuccess,
ErrorMsg = r.Message
};
if (r.IsSuccess)
ret = (T)Convert.ChangeType(r.Content, typeof(T));
}
break;
case nameof(String):
{
// If the length is 0, This is set as the default 1
if (length == 0)
{
length = 1;
}
var addressStr = tag;
var index = tag.IndexOf(".");
var lastIndex = tag.LastIndexOf(".");
if (lastIndex != index && lastIndex < tag.Length - 1)
{
try
{
addressStr = tag.Substring(0, lastIndex);
length = Convert.ToUInt16(tag.Substring(lastIndex + 1));
}
catch
{
}
}
// When reading, the characters read are shifted back 2 position
if (addressStr.IndexOf(".") > -1) {
var addressArr = addressStr.Split('.');
addressStr = $"{addressArr[0]}.{addressArr[1].ToInt() + 2}";
}
var r = Plc.ReadString(addressStr, length);
result = new ResultMessage() { IsSuccess = r.IsSuccess, ErrorMsg = r.Message };
if (r.IsSuccess)
ret = (T)Convert.ChangeType(r.Content, typeof(T));
}
break;
case nameof(Char):
{
var r = Plc.ReadString(tag, 1);
result = new ResultMessage()
{
IsSuccess = r.IsSuccess,
ErrorMsg = r.Message
};
if (r.IsSuccess)
ret = (T)Convert.ChangeType(r.Content.ToCharArray()[0], typeof(T));
}
break;
default: break;
}
return ret;
}
public bool WriteNode(string tag, object value, out ResultMessage result)
{
if (Plc == null)
{
result = new ResultMessage()
{
IsSuccess = false,
ErrorMsg = " Connection not initialized "
};
return false;
}
OperateResult ret = default;
switch (value.GetType().Name)
{
case nameof(Boolean):
{
ret = Plc.Write(tag, (bool)value);
}
break;
case nameof(Int16):
{
ret = Plc.Write(tag, (short)value);
}
break;
case nameof(UInt16):
{
ret = Plc.Write(tag, (ushort)value);
}
break;
case nameof(Int32):
{
ret = Plc.Write(tag, (int)value);
}
break;
case nameof(UInt32):
{
ret = Plc.Write(tag, (uint)value);
}
break;
case nameof(Int64):
{
ret = Plc.Write(tag, (long)value);
}
break;
case nameof(UInt64):
{
ret = Plc.Write(tag, (ulong)value);
}
break;
case nameof(Single):
{
ret = Plc.Write(tag, (float)value);
}
break;
case nameof(Double):
{
ret = Plc.Write(tag, (double)value);
}
break;
case nameof(String):
{
string strValue = (string)value;
var addressStr = tag;
var index = tag.IndexOf(".");
var lastIndex = tag.LastIndexOf(".");
if (lastIndex != index && lastIndex < tag.Length - 1)
{
try
{
addressStr = tag.Substring(0, lastIndex);
int len = tag.Substring(lastIndex+1).ToInt();
if (value.ToString().Length < len) {
strValue = strValue.PadRight(len,'\0');
}
}
catch
{
}
}
ret = Plc.Write(addressStr, strValue);
}
break;
default: break;
}
result = new ResultMessage()
{
IsSuccess = ret.IsSuccess,
ErrorMsg = ret.Message
};
return ret.IsSuccess;
}
边栏推荐
- Who can't capture packets these days? Wireshark packet capture and common protocol analysis are for you!
- idea打jar包与引入jar包
- [intensive reading of papers] grounded language image pre training (glip)
- Detoxify! After Harbin Institute of technology was disabled MATLAB, domestic industrial software fought back domineering
- What if win11 wallpaper turns black? The solution of win11 wallpaper blackening
- 巨形象的图解 SQL
- 2022 Niuke multi School II_ E I
- web上构建3d效果 基于three.js的实例
- c语言分层理解(c语言数组)
- Printf function buffer problem
猜你喜欢

Skywalking distributed system application performance monitoring tool - medium

Lecture 4: Longest ascending substring

SkyWalking分布式系统应用程序性能监控工具-中

Docker实践经验:Docker 上部署 mysql8 主从复制
![[intensive reading of papers] grounded language image pre training (glip)](/img/3a/4ad136065acb8627df9e064ed8ef32.png)
[intensive reading of papers] grounded language image pre training (glip)

MySQL advanced II. Logical architecture analysis

Construction and empirical research of post talent demand analysis framework based on recruitment advertisement

架构——MVC的升华

STM32 - capacitive touch button experiment

在Oracle VirtualBox中导入Kali Linux官方制作的虚拟机
随机推荐
aac 和 h264等的时间戳
[popular science] the difference and connection between accuracy and resolution
Automatically configure SSH password free login and cancel SSH password free configuration script
Differences among CPU, GPU and NPU
如何帮助企业优化Office管理
How to deploy open source Siyuan privately
大家最想要的,最全的C语言知识点总结,还不赶紧学习
lc marathon 7.26
力扣SQL语句习题,错题记录
MySQL save data prompt: out of range value for column error
STM32 - capacitive touch button experiment
【STM32】EXTI
@Bean 与 @Component 用在同一个类上,会发生什么?
巨形象的图解 SQL
Slam overview Reading Note 6: slam research based on image semantics: application-oriented solutions for autonomous navigation of mobile robots 2020
STM32——电容触摸按键实验
arduino+ZE08-CH2O甲醛模块,输出甲醛含量
SkyWalking分布式系统应用程序性能监控工具-中
Understand JS execution context in an article
How to return to the parent directory with commands