天天看点

上位机与fx3u通讯串口_c#上位机与三菱PLC(FX3U)串口通讯

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

与三菱FX3U进行通讯

1. 第一种方法是自己写代码实现,主要代码如下:

//对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_OFF, 0, Y007_OFF.Length);//关闭串口

sp.Close();

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

举例说就是要对三菱PLC的Y007口进行操作,我们需要知道要对三菱PLC发送什么参数,这

里可以参考百度文库的一篇文章:

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

2.使用MX COMPONENT软件

2.1 MX Component 是一个工具,通过使用该工具,可以在无需具备通信协议及模块知

识的状况下实现从计算机至三菱PLC的通信。

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在例程中可以找到

上位机与fx3u通讯串口_c#上位机与三菱PLC(FX3U)串口通讯

//Logical Station Number的值和在MX Component中设置一样

int logicalStationNumber = 0;//添加axActUtlType对象

AxActUtlTypeLib.AxActUtlType axActUtlType = newAxActUtlTypeLib.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, refwirteData);//D0写入100

int wirteData1 = 100;

axActUtlType.WriteDeviceRandom("D0", 1, refwirteData1);//读D0数据

intreadData;

axActUtlType.ReadDeviceRandom("D0", 1, refreadData);//close

axActUtlType.Close();

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

原文:https://www.cnblogs.com/vijing/p/10460269.html