c#上位机与三菱PLC(FX3U)串口通信

项目中会常常用到上位机与PLC之间的串口通讯,本文介绍一下C#如何编写上位机代码html

与三菱FX3U进行通信编程

1. 第一种方法是本身写代码实现,主要代码以下:dom

//对PLC的Y7进行置1
byte[] Y007_ON = { 0x02, 0x37, 0x30, 0x37, 0x30, 0x35, 0x03, 0x30, 0x36 }; //选择串口参数
SerialPort sp = new SerialPort("COM5", 9600, Parity.Even, 7); //打开串口
sp.Open(); //写入数据
sp.Write(Y007_ON, 0, Y007_ON.Length); //关闭串口
sp.Close();

  该方法的缺点在于咱们首先要熟悉三菱PLC的通信协议,而后根据通讯规程来编写通讯代码工具

  举例说就是要对三菱PLC的Y007口进行操做,咱们须要知道要对三菱PLC发送什么参数,这网站

  里能够参考百度文库的一篇文章:this

  https://wenku.baidu.com/view/157632dad05abe23482fb4daa58da0116c171fa8.htmlspa

2.使用MX COMPONENT软件code

  2.1 MX Component 是一个工具,经过使用该工具,能够在无需具有通讯协议及模块知orm

    识的情况下实现从计算机至三菱PLC的通讯。htm

    MX Component的安装使用教程网上有不少,顺便找一下就能够找到合适的,这样

    要说明的是MX Component工具,使用手册和编程手册均可以在三菱的网站上下载。

    工具下载:

    https://cn.mitsubishielectric.com/fa/zh/download/dwn_idx_softwareDetail.asp?sid=45

    手册下载:

    https://cn.mitsubishielectric.com/fa/zh/download/dwn_idx_manual.asp

    下载安装以后sample路径(win10,默认安装):C:\MELSEC\Act\Samples

  2.2 介绍安装配置好MX Component以后C#使用ActUtlType控件进行串口通讯

    首先要引用,这两个DLL在例程中能够找到

    

//Logical Station Number的值和在MX Component中设置同样
int logicalStationNumber = 0; //添加axActUtlType对象
AxActUtlTypeLib.AxActUtlType axActUtlType = new AxActUtlTypeLib.AxActUtlType(); //不加这三句会报 //引起类型为“System.Windows.Forms.AxHost+InvalidActiveXStateException”的异常
((System.ComponentModel.ISupportInitialize)(axActUtlType)).BeginInit(); this.Controls.Add(axActUtlType); ((System.ComponentModel.ISupportInitialize)(axActUtlType)).EndInit(); //open
axActUtlType.ActLogicalStationNumber = logicalStationNumber; axActUtlType.ActPassword = ""; axActUtlType.Open(); //Y7写入1
int wirteData = 1; axActUtlType.WriteDeviceRandom("Y7", 1, ref wirteData); //D0写入100
int wirteData1 = 100; axActUtlType.WriteDeviceRandom("D0", 1, ref wirteData1); //读D0数据
int readData; axActUtlType.ReadDeviceRandom("D0", 1, ref readData); //close
axActUtlType.Close();

    这里只是简单介绍,更深刻的内容仍是去看编程手册和例程。

相关文章
相关标签/搜索