首先建立基础设备信息基类,保存搜索到的网络设备信息,各项属性与具体设备无关,作为所有类型网络设备的基础信息
using System; namespace Mvcms.Common.Net { /// <summary> /// 网络设备基础信息 /// </summary> public class BaseDevice { /// <summary> /// 构造函数 /// </summary> public BaseDevice() { } /// <summary> /// 构造函数 /// </summary> /// <param name="ipaddress">设备IP地址</param> /// <param name="receiveBytes">结束到的设备发送的数据</param> public BaseDevice(string ipaddress, byte[] receiveBytes) { } /// <summary> /// 远程设备IP地址 /// </summary> public string RemoteIPAddress { get; set; } /// <summary> /// 远程设备发送数据的端口号 /// </summary> public int RemotePort { get; set; } /// <summary> /// MAC地址 /// </summary> public string MAC { get; set; } /// <summary> /// 设备名称 /// </summary> public string Name { get; set; } /// <summary> /// 设备子名称 /// </summary> public string SubName { get; set; } /// <summary> /// 本地设备IP地址 /// </summary> public string LocalIPAddress { get; set; } /// <summary> /// 本地设备接收数据的端口号 /// </summary> public int LocalPort { get; set; } /// <summary> /// 接收到的字符串数据 /// </summary> public string ReceiveText { get; set; } /// <summary> /// 接收到的字节数据 /// </summary> public byte[] ReceiveBytes { get; set; } /// <summary> /// 是否在线 /// </summary> public bool OnLine { get; set; } } }
在编写一个BaseDevice派生类,保存RFID读头设备信息,可依据回复的数据获取读头的IP地址等关键信息。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Mvcms.Common.Net { public class ReaderDevice: BaseDevice { public ReaderDevice() : base() { } public ReaderDevice(string ipaddress, byte[] receiveBytes) : base(ipaddress, receiveBytes) { ReceiveText = Encoding.ASCII.GetString(receiveBytes);//转换接收的字节数据为ASCII文本 //receive示例数据:A0.34.111.253.138.91/6000/N**M*/**/RD915M01/RD915M01 // | MAC |Port| | |UserName|DeviceName| RemoteIPAddress = ipaddress; if (ReceiveText.Substring(0, 1) == "A") { //RD915M-2000读写一体机回复的数据首字符都是大写A IsReaderDevice = true; string[] ss = ReceiveText.Split('/'); if (ss.Length == 6) { MAC = ss[0].TrimStart('A'); RemotePort = int.Parse(ss[1]); SubName = ss[4];//用户名称 Name = ss[5];//设备名称 } } else { IsReaderDevice = false; } } /// <summary> /// 是否是UHF读头设备 /// </summary> public bool IsReaderDevice { get; set; } } }
设备搜索方法
/// <summary> /// 搜索UHF网络设备 /// </summary> /// <returns></returns> public List<ReaderDevice> ScanDevices() { List<ReaderDevice> result = new List<ReaderDevice>(); try { //初始化一个Socket实例,采用UDP传输 using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) { //初始化一个发送广播和指定端口的网络端口实例 IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Broadcast, 65535); EndPoint endPoint = (EndPoint)ipEndPoint; //设置该Socket实例的发送形式 socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); string request = "X";//初始化需要发送的数据 byte[] sendBytes = Encoding.ASCII.GetBytes(request); socket.SendTo(sendBytes, ipEndPoint); for (int i = 0; i < 255; i++) { int c = 0; while (c <= 3) { c++; byte[] buffer = new byte[1000]; socket.ReceiveTimeout = 10; int m_count = socket.ReceiveFrom(buffer, ref endPoint); if (m_count > 0) { byte[] receiveBytes = new byte[m_count]; Array.Copy(buffer, receiveBytes, m_count);//从缓冲区复制有效数据 string ipaddress = endPoint.ToString().Split(':')[0]; //建立RFID读头设备数据实体 ReaderDevice dev = new ReaderDevice(ipaddress, receiveBytes); if (dev.IsReaderDevice) { result.Add(dev); } break; } Thread.Sleep(10); } } socket.Close(); } } catch (Exception e) { message = e.Message; } return result; }
控制器动作代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using Mvcms.Common.Net; namespace Mvcms.Web.Controllers.admin.IOT { public class device_listController: UI.Controllers.BaseManageController { public ActionResult Index() { IsPlugin = true; ReaderDeviceHelper helper = new ReaderDeviceHelper(); List<ReaderDevice> list = helper.ScanDevices(); ViewBag.msg = ReaderDeviceHelper.Message; return View(GetViewName("/IOT/device_list.cshtml"), list); } } }
视图代码(局部代码,关注列表如何展示即可)
@model List<Mvcms.Common.Net.ReaderDevice>//声明Model数据类型,Model既是控制器传入的list数据实体列表对象 <!--列表--> <div class="table-container"> <table border="0" class="ltable" id="ltable"> <tr> <th><span class="item-mobile-hide">选择</span><i class="iconfont icon-check item-mobile-show"></i></th> <th>IP</th> <th>PORT</th> <th>MAC</th> <th>Name</th> <th>SubName</th> <th>备注</th> <th>状态</th> <th>操作</th> </tr> @foreach (Mvcms.Common.Net.BaseDevice item in Model) { <tr> <td> <span class="checkall"> <input type="checkbox" /></span> <label style="display: none">@item.RemoteIPAddress</label> </td> <td>@item.RemoteIPAddress</td> <td>@item.RemotePort</td> <td>@item.MAC</td> <td>@item.Name</td> <td>@item.SubName</td> <td>@item.ReceiveText</td> <td> @if (item.OnLine) { <i class="icon-box green"></i> } else { <i class="icon-box red"></i> } </td> <td class="item-mobile-hide"> <a title="id=@item.LocalIPAddress" href="javascript:;" onclick="getReceiveData(this);">查看接收数据</a> <a title="id=@item.LocalIPAddress" href="../device_edit/index?action=@TEnums.ActionEnum.Edit&ip=@item.RemoteIPAddress&port=@item.RemotePort">修改</a> </td> </tr> } @if (Model.Count() == 0) { <tr> @if (string.IsNullOrEmpty(ViewBag.msg)) { <td colspan="8">暂无记录</td> } else { <td colspan="8">@ViewBag.msg</td> } </tr> } </table> </div> <!--/列表-->
运行界面,目前只有一台网源RD915M-2000读写一体机作为研发试验用机
这台设备出厂时是设定为服务端模式,可通过SDK中Demo程序进行所有配置,包括网络连接设备,实际上线运行的设备都应该工作在客户端模式(此模式下将不再支持打开网络设备等操作),即主动上报数据到服务器指定端口,服务器端只需要通过Socket监听上报端口,解析上报的的数据即可。
共有条评论 网友评论