欧姆龙CP1H 通过hostlink fins串口 与上位机通信,用了hslcommunication的dll。
功能如下:
PLC收到上位机Ready信号,设备可运行;
上位机每秒读取一次来自PLC的拍照触发信号;
上位机把拍照结果发给PLC;
上图是我自己瞎吉儿写的,已测试过可以读写。
时间紧任务重,有没有大佬出手的?重写一套,尊重知识,不白嫖。
不用教育我,明年上岸,此生再不碰这玩意~~~
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HslCommunication;
using HslCommunication.Profinet.Omron;
using System.Threading;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
OmronHostLink omronHostLink;
public Form1()
{
InitializeComponent();
// 实例化对象,指定PLC的ip地址和端口号
omronHostLink = new OmronHostLink();
}
private System.Threading.Thread thread;
private void button1_Click(object sender, EventArgs e)//打开串口
{
omronHostLink.SerialPortInni(sp =>
{
sp.PortName = "COM3";
sp.BaudRate = 9600;
sp.DataBits = 7;
sp.StopBits = System.IO.Ports.StopBits.One;
sp.Parity = System.IO.Ports.Parity.Even;
});
omronHostLink.Open();//连接
//判断是否连接成功
if (omronHostLink.IsOpen())
{
MessageBox.Show("连接成功");
// 启动线程
thread = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadRead));
thread.IsBackground = true;
thread.Start();
button1.Enabled = false; // 启动之后,禁用button
}
}
private async void ThreadRead()
{
while (true)
{
// 这里就是每秒一次的读取了,但是实际上是不精确的,只能说,大概是1秒
System.Threading.Thread.Sleep(1000);
Invoke(new Action(async () =>
{
//读取PLC给的触发信号,每秒读一次;也可判断PLC是否在线
OperateResult<bool> operateResult = await omronHostLink.ReadBoolAsync("w100.00");
if (operateResult.IsSuccess)
{
label1.Text = "连接成功";
textBox1.Text = $"Value[{DateTime.Now:HH:mm:ss}]:" + operateResult.Content; // 为了看清楚是否发生了读取,显示时候顺便显示下时间
}
else
{
label1.Text = "连接失败";
textBox1.Text = $"Read failed[{DateTime.Now:HH:mm:ss}]: " + operateResult.Message;
}
}));
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
楼主最近还看过
这里有VB.NET和C#的: