参考文档:
欧姆龙PLC使用-CSDN博客
CIP通讯介绍(欧姆龙PLC)-CSDN博客
使用NuGet添加引用:CIPCompolet
基础参考我的CIP协议介绍,默认TCP端口为:44818
类NXCompolet
类的功能可以在安装PLC开发软件后帮助文档中查看,对应路径:C:\ProgramData\Omron\CX-Compolet\StartMenu\CX-Compolet\SYSMAC Compolet help
功能及其示例代码点击后查看
# MachineAutomationControllerCompolet继承CIPPlcCompolet,CIPPlcCompolet的
# 变量
public const long TIMEOUT_INFINITE = 4294967295L;
string RoutePath
ConnectionType ConnectionType
bool Active
long ReceiveTimeLimit
string TypeName
int HeartBeatTimer
CIPDevice Device
bool UseRoutePath
string PeerAddress
int LocalPort
bool IsConnected
string[] VariableNames
# 函数
byte[] ExecuteCIPService(byte serviceCode, int classId, int instanceId, int attributeId, byte[] requestData, long requestDataSize)
byte[] ExecuteCIPService(byte serviceCode, int classId, long instanceId, int attributeId, byte[] requestData, long requestDataSize)
byte[] ExecuteCIPService(byte serviceCode, string variableName, byte[] requestData, long requestDataSize)
void Update()
void AboutBox()
VariableInfo GetVariableInfo(string variableName)# NXCompolet继承MachineAutomationControllerCompolet的
# 变量
string UnitName
short RunStatus
Encoding PlcEncoding
string[] VariableNames // 可以通过这个公共变量获取到有哪些可读的参数名
string[] SystemVariableNames
DateTime Clock
bool DontFragment
# 函数
MachineAutomationControllerCompolet(IContainer container)
object ReadVariable(string variableName)
Hashtable ReadVariableMultiple(string[] variableNames)
void WriteVariable(string variableName, object writeData)
byte[] ReadRawData(string variableName)
Hashtable ReadRawDataMultiple(string[] variableNames)
void WriteRawData(string variableName, byte[] writeData)
VariableInfo GetVariableInfo(string variableName) // 获取变量信息,是否为结构体、数组、对应变量类型
EventLogItemCollection GetEventLogs(EventCategory category)# NXCompolet自己定义的
RunModeTypes RunMode// 数据类型
public enum VariableType
{TIMER = 1,COUNTER = 2,CHANNEL = 3,UINT_BCD = 4,UDINT_BCD = 5,ULINT_BCD = 6,ENUM = 7,DATE_NSEC = 8,TIME_NSEC = 9,DATE_AND_TIME_NSEC = 10,TIME_OF_DAY_NSEC = 11,UNION = 12,BOOL = 193,SINT = 194,INT = 195,DINT = 196,LINT = 197,USINT = 198,UINT = 199,UDINT = 200,ULINT = 201,REAL = 202,LREAL = 203,STRING = 208,BYTE = 209,WORD = 210,DWORD = 211,LWORD = 212,ASTRUCT = 160,STRUCT = 162,ARRAY = 163
}
ExecuteCIPService函数功能描述
# CIPPlc Compolet Class Library
byte[] requestData = new byte[2];
requestData[0] = 1;
requestData[1] = 0;
data = CIPPlcCompolet1.ExecuteCIPService(0x4C, "Variable1", requestData, 2);
运行环境启动配置
- 安装欧姆龙PLC开发软件
- 安装工具:CX-Compolet_SGW_Ver1.72
- 在Network Browser》Control Panel》Network Configuration》EtherNet/IP Hardware》Communications Adapter》如果使用网管更改ip地址
- 启动网关。点击Start如果有对应Open状态就可以,负责勾选上下面的Extend the Ethernet ports后再Start
- PLC上配置IP地址(连接成功后更改IP地址)
- 更改后连接的IP地址设置(如果这里不更改一致,无法实现在线测试更改)
- 上方工具栏》控制器》通信设置》远程IP地址
- PLC添加可被外部读取的全局变量
上位机代码实现
依赖项,Nuget中搜索CIPCompolet添加:
using OMRON.Compolet.CIP;internal class MyCIP
{NXCompolet nXCompolet; // 欧姆龙的CIP通讯Mutex mutex = new Mutex(); // 进程间互斥锁public MyCIP() {Conncet("192.168.50.1"); // 192.168.50.1; 127.0.0.1}public void Conncet(string ip){Task.Run(() =>{nXCompolet = new NXCompolet(new Container());nXCompolet.PeerAddress = ip;while (true){mutex.WaitOne();Console.WriteLine("RunStatus: " + nXCompolet.RunStatus + "; PeerAddress:" + nXCompolet.PeerAddress);Console.WriteLine("port: " + nXCompolet.LocalPort);//Console.WriteLine("UnitName: " + nXCompolet.UnitName + ",DontFragment: " + nXCompolet.DontFragment);nXCompolet.Active = true;if (!nXCompolet.IsConnected){nXCompolet.Active = false;Console.WriteLine("PLC连接错误");Thread.Sleep(1000);continue;}Console.WriteLine("PLC连接成功");//nXCompolet.RunMode = NXCompolet.RunModeTypes.Program;object obj = nXCompolet.ReadVariable("gbHMI_CloseDoor");Console.WriteLine(obj.ToString());mutex.ReleaseMutex();Thread.Sleep(1000);}});}
}
运行结果
RunStatus: 1; PeerAddress:192.168.50.1 port: 2 PLC连接成功 True
抓取报文
安装wireshark抓取
使用欧姆龙附带的调试工具
更改IP后勾选上Active进入连接状态