基恩士PLC KV7000,8000还是比较好用的,那如何和上位机通讯,我把源码写出来了。采用上位链路通讯,基恩士官方给我们留了8501端口,这个端口有意思刚好是我生日。基恩士的资料我觉得做的特别好,能快速写源代码得益于官方资料特别详细,对了,他的通讯采用是ASCII码直接通讯。比如你读到的ASCII码是666,那他的实际值也就是666。好了上代码,如果热度比较高,大家有不清楚的地方,我出视频讲解。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net.Sockets;using System.Threading;using System.Net;namespace WDBaseCommuntionHelper{    public class KV7_8000Service    {        Socket KVTCPClicent;        readonly object LockKVTCP = new object();        public enum DataType        {            ///             /// 16位无符号            ///             U,            ///             /// 16位有符号            ///             S,            ///             /// 32位无符号            ///             D,            ///             /// 32位有符号            ///             L,            ///             /// 16位16进制数            ///             H,            ///             /// 默认            ///             N        }        #region 命令        ///         /// 更改模式        ///         const string Command1 = "M";        ///         /// 清除错误        ///         const string Command2 = "ER";        ///         /// 检查错误编号        ///         const string Command3 = "?E";        ///         /// 查询机型        ///         const string Command4 = "?K";        ///         /// 检查运行模式        ///         const string Command5 = "?M";        ///         /// 时间设定        ///         const string Command6 = "WRT";        ///         /// 强制置位        ///         const string Command7 = "ST";        ///         /// 强制复位        ///         const string Command8 = "RS";        ///         /// 连续强制置位        ///         const string Command9 = "STS";        ///         /// 连续强制复位        ///         const string Command10 = "RSS";        ///         /// 读取数据        ///         const string Command11 = "RD";        ///         /// 读取连续数据        ///         const string Command12 = "RDS";        ///         /// 读取连续数据        ///         const string Command13 = "RDE";        ///         /// 写入数据        ///         const string Command14 = "WR";        ///         /// 写入连续数据        ///         const string Command15 = "WRS";        ///         /// 写入连续数据        ///         const string Command16 = "WRE";        ///         /// 写入设定值        ///         const string Command17 = "WS";        ///         /// 写入连续设定值        ///         const string Command18 = "WSS";        ///         /// 监控器登录        ///         const string Command19 = "MBS";        ///         /// 监控器登录        ///         const string Command20 = "MWS";        ///         /// 读取监控器        ///         const string Command21 = "MBR";        ///         /// 读取监控器        ///         const string Command22 = "MWR";        ///         /// 注释读取        ///         const string Command23 = "RDC";        ///         /// 存储体切换        ///         const string Command24 = "BE";        ///         /// 读取扩展单元缓冲存储器        ///         const string Command25 = "URD";        ///         /// 写入扩展单元缓冲存储器        ///         const string Command26 = "UWR";        ///         /// 回车        ///         const string CR = "";        ///         /// 空格        ///         const string SP = " ";        #endregion        Encoding encoding = Encoding.ASCII;        ///         /// PLC连接状态        ///         public bool IsConnected { get; private set; }        ///         /// 发送超时时间        ///         public int SendTimeout { get; set; } = 2000;        ///         /// 接收超时时间        ///         public int ReceiveTimeout { get; set; } = 2000;        ///         /// 等待PLC响应周期,这里一个周期10ms        ///         public int MaxDelayCycle { get; set; } = 5;        string ip;        int port;        private Dictionary plcValue = new Dictionary();        ///         /// PLC值键值对        ///         public Dictionary PlcValue { get { return plcValue; } }        ///         /// 重连        ///         ///         public bool ReConnext()        {            if (ip == string.Empty || port == 0)            {                throw new Exception("没有IP和端口请调用Connect函数连接"); return false;            }           return  Connect(ip,port);        }        ///         /// 连接PLC        ///         ///         ///         ///         public bool Connect(string ip, int port=8501)        {            if (this.ip != ip) this.ip = ip;            if (this.port != port) this.port = port;            KVTCPClicent = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            KVTCPClicent.SendTimeout = SendTimeout;            KVTCPClicent.ReceiveTimeout = ReceiveTimeout;            IAsyncResult asyncResult=  KVTCPClicent.BeginConnect(new IPEndPoint(IPAddress.Parse(ip) ,port),null,null);            asyncResult.AsyncWaitHandle.WaitOne(3000,true);            if (!asyncResult.IsCompleted)            {                KVTCPClicent.Close();                return false;            }            return true;        }        ///         /// 改变PLC运行状态        ///         /// true让PLC运行false让PLC停止        ///         public string ChangeCPU(bool run = true)        {            string str = Command1 + (run ? "1" : "0") + CR;            return SendRecive(str);        }        ///         /// 查看PLC运行状态        ///         ///         public string SeeCPUState()        {            string str = Command5 + CR;            return SendRecive(str);        }        ///         /// 强制置位复位        ///         ///         ///         ///         public string Put(string address, bool value)        {            string str;            if (value)            {                str = Command7 + SP + address + CR;            }            else            {                str = Command8 + SP + address + CR;            }            return SendRecive(str);        }        ///         /// 连续强制置位复位        ///         ///         ///         ///         ///         public string Put(string address, bool Value, int count)        {            string str;            if (Value)            {                str = Command9 + SP + address + count + CR;            }            else            {                str = Command10 + SP + address + count + CR;            }            return SendRecive(str);        }        ///         /// 写入字数据        ///         ///         ///         ///         ///         public string Put(string address, string value, DataType tpye = DataType.N)        {            string str = Command14 + SP + address + GetDataType(tpye) + SP + value + CR;            return SendRecive(str);        }        ///         /// 写入单精度浮点数        ///         ///         ///         ///         public string Put(string address, float value)        {            List result = CalFloatToInt16(value);            return Put(address, result);        }        ///         /// 写入连续字数据        ///         ///         ///         ///         ///         public string Put(string address, List value, DataType tpye = DataType.N)        {            StringBuilder sb = new StringBuilder(Command15 + SP + address + GetDataType(tpye) + SP + value.Count);            for (int i = 0; i < value.Count; i++)            {                sb.Append(SP + value[i]);            }            sb.Append(CR);            return SendRecive(sb.ToString());        }        ///         /// 读取字数据        ///         ///         ///         ///         public string Get(string address, DataType tpye = DataType.N)        {            string str = Command11 + SP + address + GetDataType(tpye) + CR;            return SendRecive(str);        }        ///         /// 读取连续字数据        ///         ///         ///         ///         ///         public string Get(string address, int count, DataType tpye = DataType.N)        {            string str = Command13 + SP + address + GetDataType(tpye) + SP + count + CR;            return SendRecive(str);        }        public string CalRD(string str, string type, int address, int count)        {            string[] s1 = str.Split(' ');            if (count > s1.Length)            {                throw new Exception("映射长度过长");            }            for (int i = 0; i < count; i++)            {                plcValue[type + (address + i)] = s1[i].ToString();            }            return "OK";        }        ///         /// 32位浮点转16位字        ///         ///         ///         public List CalFloatToInt16(float value)        {            byte[] r1 = BitConverter.GetBytes(value);            byte[] r2 = new byte[2] { r1[0], r1[1] };            byte[] r3 = new byte[2] { r1[2], r1[3] };            int r4 = BitConverter.ToUInt16(r2, 0);            int r5 = BitConverter.ToUInt16(r3, 0);            return new List() { r4.ToString(), r5.ToString() };        }        ///         /// 2个16位字转32位浮点        ///         ///         ///         ///         public float CalInt16ToFlaot(string[] value, int startIndex)        {            byte[] r1 = BitConverter.GetBytes(Convert.ToInt16(value[startIndex]));            byte[] r2 = BitConverter.GetBytes(Convert.ToInt16(value[startIndex + 1]));            byte[] r3 = new byte[4] { r1[0], r1[1], r2[0], r2[1] };            return BitConverter.ToSingle(r3, 0);        }        ///         /// ASCII编码解码        ///         ///         ///         public string SendRecive(string str)        {            return encoding.GetString(SendRecive(encoding.GetBytes(str)));        }        ///         /// 发送接收字节数组报文        ///         ///         ///         public byte[] SendRecive(byte[] arry)        {            try            {                Monitor.Enter(LockKVTCP);                int delay = 0;                int reslut = KVTCPClicent.Send(arry);                while (KVTCPClicent.Available == 0)                {                    Thread.Sleep(10);                    delay++;                    if (delay > MaxDelayCycle)                    {                        break;                    }                }                byte[] ResByte = new byte[KVTCPClicent.Available];                reslut = KVTCPClicent.Receive(ResByte);                return ResByte;            }            catch (Exception)            {                IsConnected = false;                return null;            }            finally            {                Monitor.Exit(LockKVTCP);            }        }        ///         /// 根据数据类型生成报文        ///         ///         ///         public string GetDataType(DataType dataType)        {            string str = string.Empty;            switch (dataType)            {                case DataType.U:                    str = ".U";                    break;                case DataType.S:                    str = ".S";                    break;                case DataType.D:                    str = ".D";                    break;                case DataType.L:                    str = ".L";                    break;                case DataType.H:                    str = ".H";                    break;                case DataType.N:                    str = "";                    break;                default:                    str = "";                    break;            }            return str;        }    }}

#上位机# #PLC# #触摸屏# #电工交流圈#

如果对您有帮助,点赞关注转发,持续更新,帮大家解答工控问题

基恩士上位机链路通讯_基恩士PLC通讯源码相关推荐

  1. LabVIEW上位机学习 面对对象编程实例项目源码 与下位机PLC

    LabVIEW上位机学习 面对对象编程实例项目源码 与下位机PLC,工厂MES通讯,数据库,NI vision视觉等开发,及操作者框架源码可运行,想学习面对对象的可以参考 ID:16199647294 ...

  2. plc 上位机编译算法_西门子PLC的开放式TCP通信

    对于自控或电气工程师来说,西门子PLC是每个人都非常熟悉的一款PLC品牌:而对于上位机开发工程师来说,Socket通信或TCP/IP协议也是必须要掌握的一种通信方式. 相比较而言,西门子PLC对Soc ...

  3. 基恩士上位机链路通讯_【原创分享】ABB机器人与视觉控制器的通讯

    在一些工厂里你是否经常看到下图这样的一些自动化设备,他的里边有用到机器人与相机通讯完成物料位置的检测与物料抓放等功能,那么恭喜你,几分钟时间阅读完这篇文章你就懂了. 小编花了不少时间收集整理资料,下面 ...

  4. C#上位机基础学习_基于SOCKET实现与PLC服务器的TCP通信(二)

    C#上位机基础学习_基于SOCKET实现与PLC服务器的TCP通信(二) 测试软件: TIA PORTAL V15.1 S7-PLCSIM ADVANCED V3.0 Visual Studio 20 ...

  5. 西门子1200跟V90伺服总线通讯②_设置PLC

    西门子1200跟V90伺服总线通讯②_设置PLC 这一章主要说一下PLC这边的组态 1.0 首先从官网下载GSD文件,然后打开博途软件,选项--管理通用站描述文件,里面就是安装GSD文件的,在源路径设 ...

  6. java 通讯机_java 上位机与单片机通过串口485通讯

    java 上位机与单片机通过串口485通讯 java 2020-7-12 下载地址 https://www.codedown123.com/29387.html 上位机与单片机通过串口485通讯 co ...

  7. 工业监测自动化上位机软件开发,支持PLC通信,以及与各类品牌 的电流表、频谱仪、电子负载、功率计等通信

    工业监测自动化上位机软件开发,支持PLC通信,以及与各类品牌 的电流表.频谱仪.电子负载.功率计等通信 本人拥有十多年工业项目.仪器仪表产品.教学产品开发以及培训经验,精通STM32.Msp430等各 ...

  8. 三菱工控板底层源码_STM32 PLC底层源码/FX2N源码/断电保持

    标签: STM32 STM32系列基于专为要求高性能.低成本.低功耗的嵌入式应用专门设计的ARM Cortex-M3内核.按性能分成两个不同的系列:STM32F103"增强型"系列 ...

  9. 原生仿微信社交社区即时通讯聊天双端APP源码开源带PC客户端文档说明

    简介: 原生仿微信社交社区即时通讯聊天双端APP源码开源带PC客户端文档说明 5438亲测完美搭建 网盘下载地址: http://kekewangLuo.net/uiBBnJ8xQup0 图片:

最新文章

  1. 有关C/C++中,表达式计算顺序的问题,以及表达式内部变量“副作用”问题(转)...
  2. php url模式在哪修改,php如何修改url
  3. 谷歌AI乳腺癌检测超过人类?美国知名记者:让糟糕的医疗更糟罢了
  4. Linux学习总结(四十七)NFS服务配置 上篇
  5. 如何打通高薪的黄金通道 成为职场金领
  6. 时间轴CSS的Demo
  7. 《MySQL——关于grant赋权以及flush privileges》
  8. linunx 定位最耗资源的进程
  9. 在ASP.NET Web API2中启用Cross-Origin请求
  10. python怎么画图表_Python 使用pycha画图表
  11. PDFEdit编辑器使用教程为PDF增加封面
  12. 读什么,让你的生活既有诗又有远方
  13. vue3的生命周期函数
  14. 配色工具KULER的使用
  15. 爬虫数据云词图片怎么做?小姐姐教你用python做B站弹幕爬虫,并进行数据分析生成词云
  16. CSS grid 网格布局
  17. 关于OutputDebugStringW()输出调试的使用
  18. mysql 数字金钱转中文金钱函数
  19. EeePC 1005ha(1008ha)安装Ubuntu的完美方案
  20. matlab模拟频谱仪,毕业设计基于MATLAB的声音信号频谱分析仪源代码V3.1(手机版)...

热门文章

  1. Spring+Spring Security+JSTL实现的表单登陆的例子
  2. 全球500强企业人力资源管理之道
  3. 视觉标定,再来一波!!更简单粗暴!!!!!!
  4. CMOS Sensor的调试分享
  5. MFc消息映射机制理解
  6. H.264边缘块进行帧内预测时,上边缘和左边缘块的预测情况。
  7. ABB RAPID 在 Notepad++ 中语法高亮的实现
  8. orb-slam2在PC和ARM上运行
  9. c mysql备份还原数据库,MySQL数据库备份与恢复方法
  10. html怎么使图片无法另存为,如何禁止图片另存为?禁止网页另存为到本地的方法...