订阅
纠错
加入自媒体

一文了解基于WebApi实现ModbusTCP数据服务

前言

在上位机开发过程中,有时候会遇到需要提供数据接口给MES或者其他系统,今天跟大家分享一下,如何在Winform等桌面应用程序中,开发WebApi接口,提供对外数据服务。

为了更好地演示应用场景,本案例以读取ModbusTCP设备为例,开发好WeiApi接口后,第三方系统可以通过该接口读取到设备数据。

实现过程

1、创建一个Winform程序,设计UI界面如下,主要包括ModbusTCP的设备IP及端口,以及本地WepApi的Http服务及端口:

2、实现ModbusTCP连接

(1)Nuget搜索xktComm并安装,便于后续可以实现ModbusTCP连接

(2)建立ModbusTCP连接

      private void btn_Connect_Click(object sender, EventArgs e)
       {
           if (CommonMethods.modbusTcp.Connect(this.txt_DevIp.Text, this.txt_DevPort.Text))
           {
               MessageBox.Show("设备连接成功");
           }
           else
           {
               MessageBox.Show("设备连接失败");
           }
       }

(3)断开ModbusTCP连接

      private void btn_DisConn_Click(object sender, EventArgs e)
       {
           CommonMethods.modbusTcp.DisConnect();
       }

3、创建HttpServer

首先通过Nuget搜索这两个库,添加一下引用:

Microsoft.AspNet.WebApi.ClientMicrosoft.AspNet.WebApi.SelfHost

HttpServer主要是对HttpSelfHostServer的封装,HttpServer类如下:

  public class HttpServer
   {
       private HttpSelfHostServer server;
       public HttpServer(string ip, int port)
       {
           var config = new HttpSelfHostConfiguration($"http://{ip}:{port}");
           config.MapHttpAttributeRoutes();
           config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{action}");
           server = new HttpSelfHostServer(config);
       }
       public Task StartHttpServer()
       {
           return server.OpenAsync();
       }
       public Task CloseHttpServer()
       {
           return server.CloseAsync();
       }
   }

4、创建Controller创建一个控制器HomeController,以读取保持寄存器为例,编写了一个方法可以读取一个保持寄存器存储区数据,代码如下所示:

  public class HomeController : ApiController
   {
       [HttpGet]
       public IHttpActionResult ReadKeepReg(int address)
       {
           byte[] res = CommonMethods.modbusTcp.ReadKeepReg(address, 1);
           return Json(res[0]*256+res[1]);
       }
   }

5、开启HttpServer

(1)创建HttpServer对象

  private HttpServer httpServer = null;

(2)开启HttpServer服务

      private async void btn_Start_Click(object sender, EventArgs e)
       {
           try
           {
               httpServer = new HttpServer(this.txt_Ip.Text, int.Parse(this.txt_Port.Text));
               await httpServer.StartHttpServer();
               MessageBox.Show("开始服务成功");
           }
           catch (Exception ex)
           {
               MessageBox.Show("开始服务失败:"+ex.Message);
           }
       }

(3)停止HttpServer服务

      private async void btn_Stop_Click(object sender, EventArgs e)
       {
           try
           {
               httpServer = new HttpServer(this.txt_Ip.Text, int.Parse(this.txt_Port.Text));
               await httpServer.CloseHttpServer();
           }
           catch (Exception ex)
           {
               MessageBox.Show("停止服务失败:" + ex.Message);
           }
       }
功能测试

首先用Modbus Slave开一个仿真:

运行上位机软件后,连接设备并开启服务:

打开浏览器,输入 http://127.0.0.1:2000/api/home/ReadKeepReg?address=0,即可获取到40001的数据。

声明: 本文由入驻维科号的作者撰写,观点仅代表作者本人,不代表OFweek立场。如有侵权或其他问题,请联系举报。

发表评论

0条评论,0人参与

请输入评论内容...

请输入评论/评论长度6~500个字

您提交的评论过于频繁,请输入验证码继续

暂无评论

暂无评论

人工智能 猎头职位 更多
扫码关注公众号
OFweek人工智能网
获取更多精彩内容
文章纠错
x
*文字标题:
*纠错内容:
联系邮箱:
*验 证 码:

粤公网安备 44030502002758号