Beispiel #1
0
        private ModbusResult ReadRegister(dynamic parameters)
        {
            var result = new ModbusResult();

            try
            {
                result.IpAddress    = parameters.ipaddress;
                result.StartAddress = parameters.start_address;
                bool ok = true;

                using (TcpClient client = new TcpClient(result.IpAddress, 502))
                {
                    client.SendTimeout = 1000;

                    var factory = new ModbusFactory();

                    IModbusMaster master = factory.CreateMaster(client);
                    master.Transport.Retries      = 0;
                    master.Transport.ReadTimeout  = 300;
                    master.Transport.WriteTimeout = 300;

                    //if (!client.ConnectAsync(result.IpAddress, 502).Wait(1000))
                    //{
                    //    // connection failure
                    //    ok = false;
                    //}

                    if (ok)
                    {
                        ushort startAddress = (ushort)result.StartAddress;
                        ushort numInputs    = (ushort)int.Parse(parameters.number_of_points);
                        bool[] inputs       = master.ReadInputs(0, startAddress, numInputs);

                        for (int i = 0; i < numInputs; i++)
                        {
                            result.data.Add(inputs[i]);

                            if (parameters.with_html == true)
                            {
                                result.ApiCall            = Request.Url.SiteBase + $"/api/modbus/{parameters.ipaddress}/{parameters.start_address}/{parameters.number_of_points}";
                                result.BitsStringForHtml += $"no {i} = {(inputs[i] ? "1" : "0")}<br />\r\n";
                            }

                            result.HasData = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                result.HasError     = true;
                result.ErrorMessage = ex.Message + Environment.NewLine + ex.StackTrace;
            }

            result.serverTime = DateTime.Now;
            return(result);
        }
Beispiel #2
0
        private dynamic HandlePost(dynamic parameters)
        {
            parameters.ipaddress        = this.Request.Form["IpAddress"];
            parameters.start_address    = this.Request.Form["StartAddress"];
            parameters.number_of_points = this.Request.Form["CountOfInputs"];
            parameters.with_html        = true;

            ModbusResult r = ReadRegister(parameters);

            return(View["modbus", r]);
        }
Beispiel #3
0
        public ModbusRestApi()
        {
            Get["ModbusFormView", "/modbus/{ipaddress}"] = parameters => View["modbus", new ModbusApiResult()
                                                                              {
                                                                                  IpAddress = parameters.ipaddress
                                                                              }];
            Post["ModbusFormView", "/modbus"]            = parameters => HandlePost(parameters);

            Get["ReadModbusData", "/api/modbus/{ipaddress}/{start_address}/{number_of_points}"] = parameters =>
            {
                ModbusResult    r  = ReadRegister(parameters);
                ModbusApiResult rr = new ModbusApiResult()
                {
                    data         = r.data,
                    IpAddress    = r.IpAddress,
                    StartAddress = r.StartAddress,
                    ErrorMessage = r.ErrorMessage
                };
                var json = Response.AsJson(rr);
                return(json);
            };
        }