c#--无线通信,实现简单有人透传云通信

学习文档:http://cloud.usr.cn/Public/sdk/dll/#USR-Init-%E5%88%9D%E5%A7%8B%E5%8C%96%E6%8E%A5%E5%8F%A3

学习过串口通信后,在连线不方便或者距离比较远的使用场景,会用到4G模块。
本文使用的是有人物联网旗下的4G透传模块(新版本) WH

目录

    • 1.配置有人透传云管理系统
    • 2.在VS项目上连接云关系系统
      • (1)连接云管理系统:
      • (2)断开云管理系统:
      • (3)订阅设备:
      • (4)与设备通信:
    • 3.完整代码例子:

1.配置有人透传云管理系统

一般来说,都是配置好了,我们仅需要知道账号及密码,用于登录,并查看需要通信的设备id.
下面简单说明操作:
地址:https://console.usr.cn/#/login
登录账号及密码
c#--无线通信,实现简单有人透传云通信_第1张图片
添加设备得到设备:
c#--无线通信,实现简单有人透传云通信_第2张图片

2.在VS项目上连接云关系系统

先把引用文档一次性引用进来:

///////////////////////////////
        ///////  初始化和释放  ////////
        ///////////////////////////////

        /// 
        /// 获取版本
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_GetVer", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_GetVer();
        /// 
        /// 初始化
        /// 
        /// 
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_Init", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_Init(string host, ushort port, int vertion);
        /// 
        /// 释放
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_Release", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_Release();

        ///////////////////////////////
        ////////  连接和断开  /////////
        ///////////////////////////////

        /// 
        /// 连接回调
        /// 
        /// 
        /// 
        public delegate void TUSR_ConnAckEvent(int returnCode, IntPtr description);
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_OnConnAck", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_OnConnAck(TUSR_ConnAckEvent OnConnAck);
        /// 
        /// 连接
        /// 
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_Connect", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_Connect(string username, string password);
        /// 
        /// 断开
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_DisConnect", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_DisConnect();

        ///////////////////////////////
        //////  订阅和取消订阅  ///////
        ///////////////////////////////
        /// 
        /// 订阅回调
        /// 
        /// 消息ID
        /// 函数名称,用于判断用户执行的哪个订阅函数, 得到了服务器的回应。可能的取值有:SubscribeDevParsed,SubscribeUserParsed,SubscribeDevRaw,SubscribeUserRaw
        /// SubParam值跟执行的订阅函数有关:如果订阅的是”单个设备的消息”, 则SubParam为设备ID;如果订阅的是”用户所有设备的消息”, 则SubParam为用户名
        /// 0、1、2:订阅成功;  128:订阅失败
        public delegate void TUSR_SubscribeAckEvent(int messageID, IntPtr SubFunName, IntPtr SubParam, IntPtr returnCode);
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_OnSubscribeAck", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_OnSubscribeAck(TUSR_SubscribeAckEvent OnSubscribeAck);
        /// 
        /// 取消订阅回调
        /// 
        /// 
        /// 
        /// 
        public delegate void TUSR_UnSubscribeAckEvent(int messageID, IntPtr UnSubFunName, IntPtr UnSubParam);
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_OnUnSubscribeAck", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_OnUnSubscribeAck(TUSR_UnSubscribeAckEvent OnUnSubscribeAck);

        /// 
        /// 订阅单个设备解析后的数据  【云组态】
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_SubscribeDevParsed", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_SubscribeDevParsed(string devId);
        /// 
        /// 订阅账户下所有设备解析后的数据  【云组态】
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_SubscribeUserParsed", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_SubscribeUserParsed(string Username);
        /// 
        /// 取消订阅单个设备解析后的数据  【云组态】
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_UnSubscribeDevParsed", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_UnSubscribeDevParsed(string DevId);
        /// 
        /// 取消订阅账户下所有设备解析后的数据  【云组态】
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_UnSubscribeUserParsed", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_UnSubscribeUserParsed(string Username);

        /// 
        /// 订阅单个设备原始数据流 【云交换机】
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_SubscribeDevRaw", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_SubscribeDevRaw(string devId);
        /// 
        /// 订阅账户下所有设备原始数据流 【云交换机】
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_SubscribeUserRaw", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_SubscribeUserRaw(string Username);
        /// 
        /// 取消订阅单个设备原始数据流 【云交换机】
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_UnSubscribeDevRaw", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_UnSubscribeDevRaw(string DevId);
        /// 
        /// 取消订阅账户下所有设备原始数据流 【云交换机】
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_UnSubscribeUserRaw", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_UnSubscribeUserRaw(string Username);

        //订阅回调 【不再推荐使用】
        public delegate void TUSR_SubAckEvent(int messageID, IntPtr devId, IntPtr returnCode);
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_OnSubAck", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_OnSubAck(TUSR_SubAckEvent OnSubAck);
        //订阅 【不再推荐使用】
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_Subscribe", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_Subscribe(string devId);
        //取消订阅回调 【不再推荐使用】
        public delegate void TUSR_UnSubAckEvent(int messageID, IntPtr devId);
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_OnUnSubAck", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_OnUnSubAck(TUSR_UnSubAckEvent OnUnSubAck);
        //取消订阅 【不再推荐使用】
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_UnSubscribe", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_UnSubscribe(string devId);

        ///////////////////////////////
        /////////  推送消息 ///////////
        ///////////////////////////////
        /// 
        /// 推送回调
        /// 
        /// 
        public delegate void TUSR_PubAckEvent(int MessageID);
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_OnPubAck", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_OnPubAck(TUSR_PubAckEvent OnPubAck);

        /// 
        /// 设置数据点值【云组态】
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_PublishParsedSetSlaveDataPoint", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_PublishParsedSetSlaveDataPoint(string DevId, string SlaveIndex, string PointId, string Value);
        /// 
        /// 查询数据点值【云组态】
        /// 
        /// 
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_PublishParsedQuerySlaveDataPoint", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_PublishParsedQuerySlaveDataPoint(string DevId, string SlaveIndex, string PointId);

        /// 
        /// 设置单台设备数据点值【云组态】 ---- 已弃, 用 USR_PublishParsedQuerySlaveDataPoint 代替
        /// 
        /// 
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_PublishParsedSetDataPoint", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_PublishParsedSetDataPoint(string DevId, string PointId, string Value);
        /// 
        /// 查询单台设备数据点值【云组态】 ---- 已弃, 用 USR_PublishParsedQuerySlaveDataPoint 代替
        /// 
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_PublishParsedQueryDataPoint", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_PublishParsedQueryDataPoint(string DevId, string PointId);

        /// 
        /// 向单台设备推送原始数据流 【云交换机】
        /// 
        /// 
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_PublishRawToDev", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_PublishRawToDev(string DevId, byte[] pData, int DataLen);

        /// 
        /// 向账户下所有设备推送原始数据流 【云交换机】
        /// 
        /// 
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_PublishRawToUser", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_PublishRawToUser(string Username, byte[] pData, int DataLen);

        //推送【不再推荐使用】
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_Publish", CallingConvention = CallingConvention.StdCall)]
        public static extern int USR_Publish(string DevId, byte[] pData, int DataLen);

        ///////////////////////////////
        /////////  接收消息 ///////////
        ///////////////////////////////
        /// 
        /// 接收设备解析后的数据 事件定义 【云组态】
        /// 
        /// 
        /// 
        /// 
        public delegate void TUSR_RcvParsedEvent(int MessageID, IntPtr DevId, IntPtr JsonStr);
        /// 
        /// 设置 接收数据点推送 回调函数 【云组态】
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_OnRcvParsedDataPointPush", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_OnRcvParsedDataPointPush(TUSR_RcvParsedEvent OnRcvParsed);
        /// 
        /// 设置 接收设备上下线推送 回调函数 【云组态】
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_OnRcvParsedDevStatusPush", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_OnRcvParsedDevStatusPush(TUSR_RcvParsedEvent OnRcvParsed);
        /// 
        /// 设置 接收设备报警推送 回调函数 【云组态】
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_OnRcvParsedDevAlarmPush", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_OnRcvParsedDevAlarmPush(TUSR_RcvParsedEvent OnRcvParsed);
        /// 
        /// 设置 接收数据点操作应答 【云组态】
        /// 
        /// 
        /// 
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_OnRcvParsedOptionResponseReturn", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_OnRcvParsedOptionResponseReturn(TUSR_RcvParsedEvent OnRcvParsed);

        /// 
        /// 接收设备原始数据流 事件定义 【云交换机】 
        /// 
        /// 
        /// 
        /// 
        /// 
        public delegate void TUSR_RcvRawFromDevEvent(int MessageID, IntPtr DevId, IntPtr pData, int DataLen);
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_OnRcvRawFromDev", CallingConvention = CallingConvention.StdCall)]
        /// 
        /// 设置 接收设备原始数据流 回调函数 【云交换机】
        /// 
        /// 
        /// 
        public static extern bool USR_OnRcvRawFromDev(TUSR_RcvRawFromDevEvent OnRcvRawFromDev);

        //接收回调 【不再推荐使用】
        public delegate void TUSR_OnRcvEvent(int MessageID, IntPtr DevId, IntPtr pData, int DataLen);
        [DllImport("UsrCloud.dll", CharSet = CharSet.Auto, EntryPoint = "USR_OnRcv", CallingConvention = CallingConvention.StdCall)]
        public static extern bool USR_OnRcv(TUSR_OnRcvEvent OnRcvEvent);

还有UsrCloud.dll文件,放在debug下或者release下面

(1)连接云管理系统:

使用的时候Open(账户,密码)

public static void Init()
{
	FConnAck_CBF = new TUSR_ConnAckEvent(ConnAck_CBF);
	FSubscribeAck_CBF = new TUSR_SubscribeAckEvent(SubscribeAck_CBF);
	FUnSubscribeAck_CBF = new TUSR_UnSubscribeAckEvent(UnSubscribeAck_CBF);
	FPubAck_CBF = new TUSR_PubAckEvent(PubAck_CBF);
	FRcvParsedDataPointPush_CBF = new TUSR_RcvParsedEvent(RcvParsedDataPointPush_CBF);
	FRcvParsedDevStatusPush_CBF = new TUSR_RcvParsedEvent(RcvParsedDevStatusPush_CBF);
	FRcvParsedDevAlarmPush_CBF = new TUSR_RcvParsedEvent(RcvParsedDevAlarmPush_CBF);
	FRcvParsedOptionResponseReturn_CBF = new TUSR_RcvParsedEvent(RcvParsedOptionResponseReturn_CBF);
	FRcvRawFromDev_CBF = new TUSR_RcvRawFromDevEvent(RcvRawFromDev_CBF);
}
public static bool Open(string userName,string passWord)
{
	Init();
    string ip = "clouddata.usr.cn";//透传云服务器地址, 不改
    ushort port = 1883;//透传云服务器端口, 不改
    int vertion = 1;
    if (USR_Init(ip, port, vertion))
    {
        USR_OnConnAck(FConnAck_CBF);
        USR_OnSubscribeAck(FSubscribeAck_CBF);
        USR_OnUnSubscribeAck(FUnSubscribeAck_CBF);
        USR_OnPubAck(FPubAck_CBF);
        USR_OnRcvParsedDataPointPush(FRcvParsedDataPointPush_CBF);
        USR_OnRcvParsedDevStatusPush(FRcvParsedDevStatusPush_CBF);
        USR_OnRcvParsedDevAlarmPush(FRcvParsedDevAlarmPush_CBF);
        USR_OnRcvParsedOptionResponseReturn(FRcvParsedOptionResponseReturn_CBF);
        USR_OnRcvRawFromDev(FRcvRawFromDev_CBF);
        if (USR_Connect(userName, passWord))
        {
            return true;
        }
       
    }
    return false;
}

注意:上面只是发生成功了,要知道能不能登录是回调函数:ConnAck_CBF
我是用的是委托的方式通知。
c#--无线通信,实现简单有人透传云通信_第3张图片

(2)断开云管理系统:

 public static bool Close()
 {
      if (USR_DisConnect() && USR_Release())
      {
          return true;
      }
      return false;
  }

(3)订阅设备:

想要接收哪个设备发来的消息,就要先订阅哪个设备的消息。

/// 
        /// 订阅设备
        /// 
        /// 多个设备编号逗号隔开
        public static bool  Subscription(string devIds)
        {
            int messageId = USR_SubscribeDevRaw(devIds);
            if (messageId > -1)
            {
                return true;
            }
            return false;
        }

(4)与设备通信:

向设备发送指令:

public static bool Connection(string devId,string info, bool hex )
        {
            byte[] byteArray;
            if (hex)
            {
                byteArray = HexStringToByteArray(info);
            }
            else
            {
                byteArray =Encoding.Default.GetBytes(info);
            }

            int messageId = USR_PublishRawToDev(devId, byteArray, byteArray.Length);
            if (messageId > -1)
            {
                return true;
            }
            return false;
        }

如果设备会对此进行回复:
接收设备回的数据:RcvRawFromDev_CBF事件

3.完整代码例子:

完整程序代码例子在:https://download.csdn.net/download/Yyuanyuxin/12510882
如图:
c#--无线通信,实现简单有人透传云通信_第4张图片

你可能感兴趣的:(c#,#,c#,非控件常用操作,无线通信,有人,透传云,串口无线,c#)