欧姆龙CP1H 通过hostlink fins串口 与上位机通信 点击:555 | 回复:5



李凌

    
  • 精华:0帖
  • 求助:2帖
  • 帖子:2帖 | 7回
  • 年度积分:46
  • 历史总积分:157
  • 注册:2019年4月23日
发表于:2022-12-29 16:56:01
楼主
求助帖31分-未结帖

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

        {


        }

    }

}





楼主最近还看过



AI雪

  • 精华:0帖
  • 求助:0帖
  • 帖子:4帖 | 204回
  • 年度积分:26
  • 历史总积分:1951
  • 注册:2016年8月07日
发表于:2022-12-29 18:00:14
1楼

这不写的挺好的吗

wjj4773

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 19回
  • 年度积分:0
  • 历史总积分:104
  • 注册:2014年5月11日
发表于:2023-01-01 10:30:38
2楼

感谢大神分享

havegood

  • 精华:0帖
  • 求助:1帖
  • 帖子:80帖 | 273回
  • 年度积分:118
  • 历史总积分:1335
  • 注册:2016年7月23日
发表于:2023-01-04 09:45:56
3楼

南宫紫霖

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 99回
  • 年度积分:9
  • 历史总积分:120
  • 注册:2020年1月04日
发表于:2023-01-12 17:24:02
4楼

感谢大神分享

微信用户

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 7回
  • 年度积分:76
  • 历史总积分:76
  • 注册:2023年1月08日
发表于:2023-01-12 18:23:22
5楼
感谢分享!!

热门招聘
相关主题

官方公众号

智造工程师