C# WCS上位机与三菱PLC协议之McProtocol

using System;
using System.Collections.Generic;

namespace MCProtocol
{
    internal class Frame
    {
        private struct CompleteCode
        {
            public const int NoData = 0xF000;
            public const int InvalidData = 0xF001;
        }

        private const byte _PCNo = 0xff;
        private const byte _IONo_L = 0xff;//0x03ff
        private const byte _IONo_H = 0x03;//0x03ff
        private const byte _CPUTimer_L = 0x10;//0x0010
        private const byte _CPUTimer_H = 0x00;
        private const int _receiceDataMinimumBytes = 11;
        private readonly byte _networkNo = 0x00;
        private readonly byte _stationNo = 0x00;

        public readonly int MaximumWords = 960;

        public Frame3E(int networkNo = 0, int stationNo = 0)
        {
            _networkNo = (byte)networkNo;
            _stationNo = (byte)stationNo;
        }

        private byte[] CreateFrame(int mainCmd, int subCmd, int address, byte deviceCode, int size, IReadOnlyCollection<byte> data)
        {
            int dataLength = 12 + data.Count;

            var frame = new List<byte>()
            {
                0x50, 0x00, //3E Frame
                _networkNo,
                _PCNo,
                _IONo_L,
                _IONo_H,
                _stationNo,
                (byte)(dataLength % 256),
                (byte)(dataLength / 256),
                _CPUTimer_L , _CPUTimer_H,
                (byte)mainCmd, (byte)(mainCmd >> 8),
                (byte)subCmd, (byte)(subCmd >> 8),
                (byte)address, (byte)(address >> 8),
                (byte)(address >> 16),
                deviceCode,
                (byte)size,
                (byte)(size >> 8),
            };

            frame.AddRange(data);

            return frame.ToArray();
        }

        public byte[] CreateReadWordsFrame(MCDevice device, int length)
        {
            if (device == null && length < 1)
            {
                return null;
            }

            if (length > MaximumWords)
            {
                length = MaximumWords;
            }

            int mainCmd = 0x0401;
            int subCmd = 0x0000;
            int address = device.Address;
            byte deviceCode = device.BinaryDeviceCode;
            int size = length;

            return CreateFrame(mainCmd, subCmd, address, deviceCode, size, new List<byte>());
        }

        public byte[] CreateWriteWordsFrame(MCDevice device, int[] data)
        {
            if (device == null && data == null)
            {
                return null;
            }

            int mainCmd = 0x1401;
            int subCmd = 0x0000;
            int address = device.Address;
            byte deviceCode = device.BinaryDeviceCode;
            int size = Math.Min(data.Length, MaximumWords);

            var byteData = new List<byte>();
            for (int i = 0; i < Math.Min(data.Length, MaximumWords); i++)
            {
                byteData.Add((byte)data[i]);
                byteData.Add((byte)(data[i] >> 8));
            }

            return CreateFrame(mainCmd, subCmd, address, deviceCode, size, byteData);
        }

        public byte[] CreateReadBitFrame(MCDevice device)
        {
            if (device == null)
            {
                return null;
            }

            int mainCmd = 0x0401;
            int subCmd = 0x0001;
            int address = device.Address;
            byte deviceCode = device.BinaryDeviceCode;
            int size = 1;

            return CreateFrame(mainCmd, subCmd, address, deviceCode, size, new List<byte>());
        }

        public byte[] CreateWriteBitFrame(MCDevice device, bool isOn)
        {
            if (device == null)
            {
                return null;
            }

            int mainCmd = 0x1401;
            int subCmd = 0x0001;
            int address = device.Address;
            byte deviceCode = device.BinaryDeviceCode;
            int size = 1;
            var byteData = new List<byte>();
            byteData.Add((byte)(isOn ? 0x10 : 0x00));

            return CreateFrame(mainCmd, subCmd, address, deviceCode, size, byteData);
        }

        public int ResolveReadBitReturnFrame(byte[] receiveData, out bool value)
        {
            if (receiveData == null || receiveData.Length < _receiceDataMinimumBytes)
            {
                value = false;
                return CompleteCode.NoData;
            }

            ushort dataBytesLength = BitConverter.ToUInt16(receiveData, 7); //+ CompleteCode 2Bytes
            ushort completeCode = BitConverter.ToUInt16(receiveData, 9);

            if (receiveData.Length != 9 + dataBytesLength)
            {
                value = false;
                return CompleteCode.InvalidData;
            }

            value = receiveData[11] == 0x10;

            return completeCode;
        }

        public int ResolveWriteBitReturnFrame(byte[] receiveData)
        {
            if (receiveData == null || receiveData.Length < _receiceDataMinimumBytes)
            {
                return CompleteCode.NoData;
            }

            ushort dataBytesLength = BitConverter.ToUInt16(receiveData, 7); //+ CompleteCode 2Bytes
            ushort completeCode = BitConverter.ToUInt16(receiveData, 9);

            if (receiveData.Length != 9 + dataBytesLength)
            {
                return CompleteCode.InvalidData;
            }

            return completeCode;
        }

        public int ResolveReadWordsReturnFrame(byte[] receiveData, out int[] data)
        {
            if (receiveData == null || receiveData.Length < _receiceDataMinimumBytes)
            {
                data = null;
                return CompleteCode.NoData;
            }

            ushort dataBytesLength = BitConverter.ToUInt16(receiveData, 7); //+ CompleteCode 2Bytes
            ushort completeCode = BitConverter.ToUInt16(receiveData, 9);

            if (receiveData.Length != 9 + dataBytesLength)
            {
                data = null;
                return CompleteCode.InvalidData;
            }

            data = new int[(dataBytesLength - 2) / 2];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = BitConverter.ToUInt16(receiveData, _receiceDataMinimumBytes + (i * 2));
            }

            return completeCode;
        }

        public int ResolveWriteWordsReturnFrame(byte[] receiveData)
        {
            if (receiveData == null || receiveData.Length < _receiceDataMinimumBytes)
            {
                return CompleteCode.NoData;
            }

            ushort dataBytesLength = BitConverter.ToUInt16(receiveData, 7); //+ CompleteCode 2Bytes
            ushort completeCode = BitConverter.ToUInt16(receiveData, 9);

            if (receiveData.Length != 9 + dataBytesLength)
            {
                return CompleteCode.InvalidData;
            }

            return completeCode;
        }
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
wcsplc通讯使用的是S7协议,S7协议是西门子公司开发的一种用于工业自动化领域的通讯协议。它定义了数据传输的格式、通信方式以及通信命令等内容,为wcsplc之间的数据交互提供了标准化的规范。 具体来说,wcs作为一个控制系统,可以通过S7协议plc进行通信以实现对plc的控制。wcs可以向plc发送各种命令和指令,例如读取和写入plc的数据、控制plc的输入和输出等。同时,wcs还可以从plc获取各种信息,包括plc的状态、运行数据、故障信息等。 在wcsplc通信过程中,使用S7协议可以提供高效、稳定的数据传输。S7协议采用了面向连接的通信方式,数据的传输是经过可靠性保证的。此外,S7协议还支持多种数据类型的传输,如位、字节、整数、浮点数、字符串等,能够满足不同的数据传输需求。 为了实现wcsplc的通信,需要在wcs系统和plc系统中分别设置相应的S7协议配置。wcs系统需要配置plc设备的IP地址和端口号,以便与plc建立通信连接。plc系统则需要设置S7协议的通信参数,如通信速率、数据位、奇偶校验等。 总结起来,wcsplc通讯通过S7协议实现了高效、稳定的数据交互。通过S7协议wcs可以向plc发送各种指令和获取plc的信息,从而实现对plc的控制和监控。S7协议的使用为wcsplc之间的通信提供了标准化的规范,提高了系统的可靠性和兼容性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

!chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值