您的位置:控制工程论坛网论坛 » PLC与PAC » 欧姆龙CP1H 通过hostlink fins串口 与上位机通信

JLVd1Qgz

JLVd1Qgz   |   当前状态:在线

总积分:721  2024年可用积分:357

注册时间: 2007-12-03

最后登录时间: 2024-09-11

空间 发短消息加为好友

欧姆龙CP1H 通过hostlink fins串口 与上位机通信

JLVd1Qgz  发表于 2024/6/13 19:50:32      573 查看 3 回复  [上一主题]  [下一主题]

手机阅读

欧姆龙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)

? ? ? ? {

? ? ? ? }

? ? }

}


1楼 0 0 回复
  • syf006

    syf006   |   当前状态:在线

    总积分:328  2024年可用积分:198

    注册时间: 2006-11-20

    最后登录时间: 2024-09-04

    空间 发短消息加为好友

    syf006   发表于 2024/6/13 19:58:37

    这不写的挺好的吗

    2楼 回复本楼

    引用 syf006 2024/6/13 19:58:37 发表于2楼的内容

  • xz6903

    xz6903   |   当前状态:在线

    总积分:301  2024年可用积分:157

    注册时间: 2007-09-29

    最后登录时间: 2024-08-30

    空间 发短消息加为好友

    xz6903   发表于 2024/6/13 20:03:41

    感谢大神分享
    3楼 回复本楼

    引用 xz6903 2024/6/13 20:03:41 发表于3楼的内容

  • sgaaa

    sgaaa   |   当前状态:在线

    总积分:629  2024年可用积分:224

    注册时间: 2007-11-04

    最后登录时间: 2024-09-03

    空间 发短消息加为好友

    sgaaa   发表于 2024/6/13 20:06:53

    感谢分享

    4楼 回复本楼

    引用 sgaaa 2024/6/13 20:06:53 发表于4楼的内容

总共 , 当前 /