抖音粉丝群1
『7x24小时有问必答』

-Begin-
前言
在上位机开发过程中,有时候会遇到需要提供数据接口给MES或者其他系统,今天跟大家分享一下,如何在Winform等桌面应用程序中,开发WebApi接口,提供对外数据服务。
为了更好地演示应用场景,本案例以读取ModbusTCP设备为例,开发好WeiApi接口后,第三方系统可以通过该接口读取到设备数据。
实现过程
1、创建一个Winform程序,设计UI界面如下,主要包括ModbusTCP的设备IP及端口,以及本地WepApi的Http服务及端口:
2.png
2、实现ModbusTCP连接
(1)Nuget搜索xktComm并安装,便于后续可以实现ModbusTCP连接
3.png
(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.Client
    Microsoft.AspNet.WebApi.SelfHost
    4.png
    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开一个仿真:
    5.png
    运行上位机软件后,连接设备并开启服务:
    打开浏览器,输入 http://127.0.0.1:2000/api/home/ReadKeepReg?address=0,即可获取到40001的数据。
    6.png

-END-

后台回复『软件』『文章,可以获取更多软件和相关原创技术文章。
欢迎关注我的公众号,回复加群』按规则加入技术交流群。
点击阅读原文』,快速进入免费直播课,欢迎点赞、在看、分享、收藏

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

上一主题上一主题         下一主题下一主题
QQ手机版小黑屋粤ICP备17165530号

关于我们·投诉举报· 用户帮助· 联系我们 · 本站服务 · 版权声明· 隐私政策 · 投搞指南

法律保护:PLC技术网,plcjs.com,plcjs.net等字样
Copyright 2010-2030. All rights reserved. 


微信公众号二维码 抖音二维码 百家号二维码 今日头条二维码哔哩哔哩二维码