本文使用:三菱FX2N系列与上位机通讯。
连接方式:选用三菱官方软件MX Component建立通讯连接,通讯线使用三菱原装下载通讯线。
一、辅助软件部分。
1、MX Component软件按步骤安装就可以了,一直下一步下一步。 这里提示一下,三菱GX Developer软件的安装,需注意安装向导提示页,有一页勾选了监视专用模式,需要取消勾选,才可对程序进行编写更改。
2、打开软件,点击Wizard,创建与PLC的连接。
3、下图界面选择站号:0,按向导提示点击下一步。
4、选择你的连接端口号,这里我的是COM4,按下图选择,选择完成点击下一步。
5、①选择PLC side I/F: cpu module;CPL类型选择:FX2N(C),波特率:9600,控制方式选择DTR and RTS Control (写和读);点击下一步。
6、这里直接下一步。
7、这里编辑连接名称,随意即可。
小结:软件建立通讯连接的I/F、端口号、波特率这些信息,与GX Developer通讯连接中均相互对应,只要GX Developer软件可以通讯上,MX Component软件也可以通讯成功。
以上通讯连接建立完成,在connection test 页面点击Test按钮,弹出提示框successfully类似字样表示连接成功。
二、上位机部分。
1、加载DLL文件,Interop.ActProgTypeLib.dll与Interop.ActUtlTypeLib.dll网上可以下载到,找不到的可以查看留言。
2、核心代码部分,直接复制粘贴就可以用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Marksys
{
class MELSECPlc
{
public static string PCReset = "M160";
public static string DiReset = "X1";
public static ActUtlTypeLib.ActUtlTypeClass lpcom_ReferencesUtlType = new ActUtlTypeLib.ActUtlTypeClass();
public static ActProgTypeLib.ActProgTypeClass lpcom_ReferencesProgType = new ActProgTypeLib.ActProgTypeClass();
public static bool FxPlcConnect()
{
try
{
int iReturnCode; //Return code
int iLogicalStationNumber; //LogicalStationNumber for ActUtlType
// 0 为站号
if (GetIntValue("0", out iLogicalStationNumber) != true)
{
//If failed, this process is end.
return false;
}
//Set the value of 'LogicalStationNumber' to the property.
lpcom_ReferencesUtlType.ActLogicalStationNumber = iLogicalStationNumber;
//Set the value of 'Password'.
lpcom_ReferencesUtlType.ActPassword = "";
//The Open method is executed.
iReturnCode = lpcom_ReferencesUtlType.Open();
if (iReturnCode == 0)
{
return true;
}
return false;
}
catch
{
return false;
}
}
public static int FxPlcReadBitbyByte(string byteItemAdrs)
{
int iReturnCode; //Return code
String szDeviceName = ""; //List data for 'DeviceName'
int iNumberOfData = 0; //Data for 'DeviceSize'
short[] arrDeviceValue; //Data for 'DeviceValue'
int iNumber; //Loop counter
System.String[] arrData; //Array for 'Data'
szDeviceName = byteItemAdrs;
if (!GetIntValue("1", out iNumberOfData))
{
return 10;
}
arrDeviceValue = new short[iNumberOfData];
try
{
iReturnCode = lpcom_ReferencesUtlType.ReadDeviceRandom2(szDeviceName,
iNumberOfData,
out arrDeviceValue[0]);
}
catch
{
return 10;
}
if (iReturnCode == 0)
{
//Assign the array for the read data.
arrData = new System.String[iNumberOfData];
//Copy the read data to the 'arrData'.
for (iNumber = 0; iNumber < iNumberOfData; iNumber++)
{
arrData[iNumber] = arrDeviceValue[iNumber].ToString();
}
//Set the read data to the 'Data', and display it.
//text1.Lines = arrData;
string str = arrData[0];
iNumber = int.Parse(arrData[0]);
return iNumber;
}
else
{
return 10;
}
}
public static string FxPlcWriteBitbyByte(int intStatus, string byteItemAdrs)
{
int iReturnCode; //Return code
String szDeviceName = ""; //List data for 'DeviceName'
int iNumberOfData = 0; //Data for 'DeviceSize'
short[] arrDeviceValue; //Data for 'DeviceValue'
int iNumber; //Loop counter
//Get the list of 'DeviceName'.
// Join each line(StringType array) of 'DeviceName' by the separator '\n',
// and create a joined string data.
szDeviceName = byteItemAdrs;
//Check the 'DeviceSize'.(If succeeded, the value is gotten.)
if (!GetIntValue("1", out iNumberOfData))
{
//If failed, this process is end.
return "10";
}
//Assign the array for 'DeviceValue'.
arrDeviceValue = new short[iNumberOfData];
//Check the 'DeviceValue'.(If succeeded, the value is gotten.)
arrDeviceValue = new short[iNumberOfData];
if (!GetShortArray(intStatus.ToString(), out arrDeviceValue))
{
//If failed, this process is end.
return "10";
}
//Set the 'DeviceValue'.
for (iNumber = 0; iNumber < iNumberOfData; iNumber++)
{
arrDeviceValue[iNumber] = arrDeviceValue[iNumber];
}
//
//Processing of WriteDeviceRandom2 method
//
try
{
//The WriteDeviceRandom2 method is executed.
iReturnCode = lpcom_ReferencesUtlType.WriteDeviceRandom2(szDeviceName,
iNumberOfData,
ref arrDeviceValue[0]);
}
//Exception processing
catch
{
return "10";
}
//The return code of the method is displayed by the hexadecimal.
string text = "";
text = String.Format("0x{0:x8}", iReturnCode);
return text;
}
public static bool GetShortArray(string lptxt_SourceOfShortArray, out short[] lplpshShortArrayValue)
{
int iSizeOfShortArray; //Size of ShortType array
int iNumber; //Loop counter
//Get the size of ShortType array.
iSizeOfShortArray = lptxt_SourceOfShortArray.Length;
lplpshShortArrayValue = new short[iSizeOfShortArray];
//Get each element of ShortType array.
for (iNumber = 0; iNumber < iSizeOfShortArray; iNumber++)
{
try
{
lplpshShortArrayValue[iNumber]
= Convert.ToInt16(lptxt_SourceOfShortArray);
}
//Exception processing
catch
{
return false;
}
}
//Normal End
return true;
}
public static bool GetIntValue(string lptxt_SourceOfIntValue, out int iGottenIntValue)
{
iGottenIntValue = 0;
try
{
iGottenIntValue = Convert.ToInt32(lptxt_SourceOfIntValue);
}
catch
{
return false;
}
return true;
}
}
}
三、调用部分
读取函数:MELSECPlc.FxPlcReadBitbyByte(MELSECPlc.DiReset);返回 “1” 表示有信号,返回 “0” 表示无信号。
写入函数:MELSECPlc.FxPlcWriteBitbyByte(1, MELSECPlc.PCMark); “1” 置位,“0”复位。