Exemple #1
0
        public void SetOperationMode(string hostIp, int port, [FromBody] sbyte operationMode)
        {
            Socket           s = ModbusSocket.GetConnection(hostIp, port);
            ModesOfOperation modesOfOperation = new ModesOfOperation();

            modesOfOperation.Write(s, (ModesOfOperation.ModesEnum)operationMode);
        }
        public bool Homing(string hostIp, int port)
        {
            Socket           s = ModbusSocket.GetConnection(hostIp, port);
            ModesOfOperation modesOfOperation = new ModesOfOperation();

            modesOfOperation.Write(s, ModesOfOperation.ModesEnum.Homing);
            while (modesOfOperation.ReadDisplay(s) != ModesOfOperation.ModesEnum.Homing)
            {
                Thread.Sleep(100);
            }
            ControlWord controlWord = new ControlWord();

            // Byte 19: 31
            controlWord.Bit00 = true; // 1
            controlWord.Bit01 = true; // 2
            controlWord.Bit02 = true; // 4
            controlWord.Bit03 = true; // 8
            controlWord.Bit04 = true; // 16
            controlWord.Write(s);

            StatusWord statusWord = new StatusWord();

            while (!(statusWord.Bit00 && statusWord.Bit01 && statusWord.Bit02 && statusWord.Bit05 && // 39
                     statusWord.Bit09 && statusWord.Bit10 && statusWord.Bit12)) // 22
            {
                statusWord.Read(s);
                Thread.Sleep(100);
            }
            return(true);
        }
Exemple #3
0
        /// <summary>
        /// 获取设备参数
        /// </summary>
        public void GetDeviceParameter()
        {
            ModbusSocket device = Z;

            device.F03(PLCSite.D(500), 17, (data) => {
                _clients.All.SendAsync("DeviceParameter", new { name = device.Name, data = ReceiveData.F03(data, 17) });
            });
        }
Exemple #4
0
        public sbyte GetOperationModeDisplay(string hostIp, int port)
        {
            Socket           s = ModbusSocket.GetConnection(hostIp, port);
            ModesOfOperation modesOfOperation = new ModesOfOperation();
            var currentMode = modesOfOperation.ReadDisplay(s);

            return((sbyte)currentMode);
        }
Exemple #5
0
        public ControlWord GetControlWord(string hostIp, int port)
        {
            Socket      s           = ModbusSocket.GetConnection(hostIp, port);
            ControlWord controlWord = new ControlWord();

            controlWord.Read(s);
            return(controlWord);
        }
        public StatusWord GetStatusWord(string hostIp, int port)
        {
            Socket     s          = ModbusSocket.GetConnection(hostIp, port);
            StatusWord statusWord = new StatusWord();

            statusWord.Read(s);
            return(statusWord);
        }
        public bool Init(string hostIp, int port)
        {
            Socket s = ModbusSocket.GetConnection(hostIp, port);

            ShutDown(s);
            SwitchOn(s);
            EnableOperation(s);
            return(true);
        }
Exemple #8
0
        public ushort GetPositionWindowTime(string hostIp, int port)
        {
            Socket s        = ModbusSocket.GetConnection(hostIp, port);
            var    telegram = new Telegram();

            telegram.Set(0, AddressConst.PositionWindowTime, 2);
            var response = telegram.SendAndReceive(s);
            var result   = BitConverter.ToUInt16(new byte[] { response.Byte19, response.Byte20 }, 0);

            return(result);
        }
Exemple #9
0
        public void SetTargetPosition(string hostIp, int port, [FromBody] int targetPosition)
        {
            Socket s = ModbusSocket.GetConnection(hostIp, port);

            byte[] data     = BitConverter.GetBytes(targetPosition);
            var    telegram = new Telegram();

            telegram.Length = 23;
            telegram.Set(1, AddressConst.TargetPosition, 4, data[0], data[1], data[2], data[3]);
            var response = telegram.SendAndReceive(s);
        }
        public void SetShaftRevolutions(string hostIp, int port, [FromBody] uint shaftRevolutions)
        {
            Socket s = ModbusSocket.GetConnection(hostIp, port);

            byte[] data     = BitConverter.GetBytes(shaftRevolutions);
            var    telegram = new Telegram();

            telegram.Length = 23;
            telegram.Set(1, AddressConst.ShaftRevolution, 4, data[0], data[1], data[2], data[3]);
            var response = telegram.SendAndReceive(s);
        }
        public uint GetShaftRevolutions(string hostIp, int port)
        {
            Socket s        = ModbusSocket.GetConnection(hostIp, port);
            var    telegram = new Telegram();

            telegram.Set(0, AddressConst.ShaftRevolution, 4);
            var response = telegram.SendAndReceive(s);
            var result   = BitConverter.ToUInt32(new byte[] { response.Byte19, response.Byte20, response.Byte21, response.Byte22 }, 0);

            return(result);
        }
        public sbyte GetMethod(string hostIp, int port)
        {
            Socket s        = ModbusSocket.GetConnection(hostIp, port);
            var    telegram = new Telegram();

            telegram.Set(0, AddressConst.HomingMethod, 4);
            var response = telegram.SendAndReceive(s);
            var result   = (sbyte)response.Byte19;

            return(result);
        }
        public void SetMethod(string hostIp, int port, [FromBody] sbyte homingMethod)
        {
            Socket s = ModbusSocket.GetConnection(hostIp, port);

            byte[] data     = BitConverter.GetBytes(homingMethod);
            var    telegram = new Telegram();

            telegram.Length = 20;
            telegram.Set(1, AddressConst.HomingMethod, 4, 0, data[0]);
            var response = telegram.SendAndReceive(s);
        }
        public void SetZeroSearchVelocity(string hostIp, int port, [FromBody] uint zeroSearchVelocity)
        {
            Socket s = ModbusSocket.GetConnection(hostIp, port);

            byte[] data     = BitConverter.GetBytes(zeroSearchVelocity);
            var    telegram = new Telegram();

            telegram.Length = 23;
            telegram.Set(1, AddressConst.HomingZeroSearchVelocity, 4, data[0], data[1], data[2], data[3]);
            var response = telegram.SendAndReceive(s);
        }
Exemple #15
0
        public int GetActualPosition(string hostIp, int port)
        {
            Socket s        = ModbusSocket.GetConnection(hostIp, port);
            var    telegram = new Telegram();

            telegram.Set(0, AddressConst.PositionActualValue, 4);
            var response = telegram.SendAndReceive(s);
            var result   = BitConverter.ToInt32(new byte[] { response.Byte19, response.Byte20, response.Byte21, response.Byte22 }, 0);

            return(result);
        }
Exemple #16
0
        public PLCHub()
        {
            TaskFactory tf = new TaskFactory();

            tf.StartNew(() => Console.WriteLine(Thread.CurrentThread.ManagedThreadId));

            if (Z == null)
            {
                Z = new ModbusSocket("192.168.181.101", 502, "主站", true);
                Z.ModbusLinkSuccess += ModbusLinkSuccess;
                Z.ModbusLinkError   += ModbusLinkError;
            }
            if (C == null)
            {
                C = new ModbusSocket("192.168.181.102", 502, "从站", false);
                C.ModbusLinkSuccess += ModbusLinkSuccess;
                C.ModbusLinkError   += ModbusLinkError;
            }
        }
        private void modbusTCPWorker(PLC plc)
        {
            if (plc == null)
            {
                return;
            }

            // initialize modbus TCP/IP
            socket = new ModbusSocket(plc.ip, Convert.ToUInt16(plc.port));

            string [] resultList = new string[plc.tags.Count];
            // read periodically
            while (socket != null && socket.connected)
            {
                try
                {
                    int i = 0;
                    foreach (Tag tag in plc.tags)
                    {
                        // get rawval from plc
                        byte[] result = modbusRead(tag.id, tag.addr, tag.type);
                        // manipulate rawval into presentation form
                        resultList[i] = tag.getVal(result).ToString();
                        i++;
                    }
                    // refresh data in project data view
                    if (presentView != null && !presentView.IsDisposed && !presentView.Disposing)
                    {
                        presentView.RefreshTagList(resultList);
                    }
                    // spin wait
                    SpinWait.SpinUntil(() => false, plc.polling_rate);
                }
                catch (Exception)
                {
                }
            }
        }
Exemple #18
0
 private void OnShutdown()
 {
     ModbusSocket.CLoseAll();
 }
Exemple #19
0
        public void SetControlWord(string hostIp, int port, [FromBody] ControlWord controlWord)
        {
            Socket s = ModbusSocket.GetConnection(hostIp, port);

            controlWord.Write(s);
        }