项目需求: 需要PC读取和控制PLC来控制电控系统的运动来配合视觉的检测
平台: 上位机软件:VS2010 C# PLC: FX2N-48MT-D
详情:
1. 先通过三菱GX-WORK2,连接编程线到PLC口,先做通讯测试,并且下载和上传程序,如果成功的话,
说明接线成功,通讯正常,接下来开始编写我们的程序。
2.起初直接下载三菱的MX compent4.08,安装好后,直接通过选择工具箱,选择com,选择FXCPU,但是
导入后,出现编程软件重启,一直不行。后开始尝试,直接提取dll,测试成功可行。大家可借鉴一下,比起
自己研究所有协议方便很多。
3. 直接新建程序,在引用中添加,AxInterop.ACTPCCOMLib.dll,程序如下:
namespace mxcompent2010
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ActFXCPU com = new ActFXCPU();
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ActFXCPU com = new ActFXCPU();
private void button1_Click(object sender, EventArgs e)
{
{
}
private void button2_Click(object sender, EventArgs e)
{
com.ActBaudRate = 9600;
com.ActControl = 8;
com.ActCpuType = 517;
com.ActPortNumber = 4;
com.ActTimeOut = 200;
int m = com.Open();
}
{
com.ActBaudRate = 9600;
com.ActControl = 8;
com.ActCpuType = 517;
com.ActPortNumber = 4;
com.ActTimeOut = 200;
int m = com.Open();
}
private void button3_Click(object sender, EventArgs e)
{int data;
int ret2 = com.SetDevice("Y10", 1); //设置Y10为高电平
com.GetDevice("Y10", out data);//得到Y10数据给data
}
{int data;
int ret2 = com.SetDevice("Y10", 1); //设置Y10为高电平
com.GetDevice("Y10", out data);//得到Y10数据给data
}
private void button4_Click(object sender, EventArgs e)
{
int data;
com.SetDevice("Y10",0); //设置Y10为低电平
com.GetDevice("Y10", out data);
}
{
int data;
com.SetDevice("Y10",0); //设置Y10为低电平
com.GetDevice("Y10", out data);
}
private void button5_Click(object sender, EventArgs e)
{
int data;
com.GetDevice("X0", out data); //得到X0参数给data
}
}
}
{
int data;
com.GetDevice("X0", out data); //得到X0参数给data
}
}
}
以上程序通讯测试ok。以下函数可以自己再琢磨,不需要分析PLC和PC的通讯协议了。
int ReadDeviceBlock(string szDevice, int lSize, out int lplData);
[DispId(22)]
int ReadDeviceBlock2(string szDevice, int lSize, out short lpsData);
[DispId(5)]
int ReadDeviceRandom(string szDeviceList, int lSize, out int lplData);
[DispId(24)]
int ReadDeviceRandom2(string szDeviceList, int lSize, out short lpsData);
[DispId(22)]
int ReadDeviceBlock2(string szDevice, int lSize, out short lpsData);
[DispId(5)]
int ReadDeviceRandom(string szDeviceList, int lSize, out int lplData);
[DispId(24)]
int ReadDeviceRandom2(string szDeviceList, int lSize, out short lpsData);
下载链接:http://download.csdn.net/download/snowbird13/10205059