通过二次开发可以扩展新的设备型号,以插件的方式快速集成到系统。下面几个步骤快速实现一个简单的电子秤驱动。
预备动作,先了解一下系统介绍,文章最下面有下载链接。
称重系统免费下载,支持耀华、顶尖等多款设备型号
使用插件式开发称重仪表驱动,RS232串口对接各类地磅秤数据实现ERP管理
1、新建一个控制台工程(.Net 4.6版本及以上)

2、添加引用DDS.IOT.Weight.exe
文件在安装目录中获取


3、新建一个测试电子秤类
继承DDS.IOT.Weight.WeightBase,并实现虚方法。

利用后台任务实现一个模拟电子秤,定时2秒输出一个随机重量。

完整代码- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Demo
- {
- public class TestWeight : DDS.IOT.Weight.WeightBase
- {
- /// <summary>
- /// 驱动类型,使用英文字符,保证唯一
- /// </summary>
- public override string Type => "MyTestWeight";
- /// <summary>
- /// 驱动名称
- /// </summary>
- public override string Name => "我的测试秤";
- /// <summary>
- /// 读取到重量事件
- /// </summary>
- public override event ReadedWeight OnReadWeight;
- /// <summary>
- /// 初始化接口
- /// </summary>
- /// <param name="port"></param>
- /// <param name="baudRate"></param>
- /// <param name="extendData"></param>
- /// <returns></returns>
- public override bool Init(int port, int baudRate = 9600, string extendData = null)
- {
- this.Port = port;
- // 再次实现打开串口等操作,下面是一段模拟定时输出随机重量的代码
- Task.Run(() => {
- while (true)
- {
- if (OnReadWeight != null)
- {
- Weight = float.Parse((new Random().NextDouble() * 100).ToString("N1"));
- OnReadWeight(this.Id, Weight);
- }
- Thread.Sleep(2000);
- }
- });
- return true;
- }
- public override bool Open()
- {
- // 在此实现关闭串口
- return true;
- }
- public override bool Close()
- {
- // 在此实现关闭串口方法
- return true;
- }
- }
- }
复制代码
4、本机测试一下


5、集成到系统
a.拷贝编译后exe文件

b.粘贴到系统目录下Plugins/Weight

c.启动系统

d.检查系统是否识别到新的驱动

e.设置后测试是否接收到重量

Ok,经过上面几个步骤一个简单的模拟秤就开发完成了,真实驱动开发时把模拟部分逻辑改成接收串口数据即可。
示例代码下载:https://files.cnblogs.com/files/yizhuqing/Demo1.zip?t=1703234473&download=true
联系方式:微信 billy_yi QQ: 909501683出处:http://yizhuqing.cnblogs.com/ 多年.NET平台开发经验,擅长物联网各类设备(地磅、轨道秤、RFID等)集成开发。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |