using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; using uPLibrary.Networking.M2Mqtt; using uPLibrary.Networking.M2Mqtt.Messages; namespace MQTTDemo { class Program { /// /// 0:DO 继电器输出接口 /// 1:DI 开关量输入接口 /// 2:AI 模拟量输入接口 /// 3:VO 语音输出接口 /// 4:PC电脑开关机接口 /// public static int ExecType = 0; // 接受消息后的操作 static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { //设备返回的数据在 e.Message 中 Console.WriteLine("Received = " + Encoding.UTF8.GetString(e.Message) + " on topic " + e.Topic); } static void Main(string[] args) { // create client instance MqttClient client = new MqttClient(IPAddress.Parse("192.168.10.173"));//MQTT_BROKER_ADDRESS client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; client.Subscribe(new string[] { "/user/update" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE }); // sub 的qos=1 string clientId = Guid.NewGuid().ToString(); client.Connect(clientId); string strValue = ""; switch (ExecType) { case 0: { Console.WriteLine("DO 继电器输出接口测试"); //控制 DO 继电器输出接口 OUT1 吸合 strValue = "AT+STACH1=1\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //时间单位改成秒 0:秒 1:100毫秒 strValue = "AT+DLYUNIT=0\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //控制 DO 继电器输出接口 OUT1 点动开,开启后延时5秒后关闭 strValue = "AT+STACH1=3,5,100000,1000000\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(6000); //控制 DO 继电器输出接口 OUT1~OUT4 间隔 1 秒轮流吸合 strValue = "AT+DODLY=1,1,4,1,1\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(5000); //继电器 OUT1 每天早上 8 点定时吸合 strValue = "AT+AUTOCONT=2,TASK1,[CYC:1],[T:3,0|1|2|3|4|5|6,08:00:00],[DO:0,1,1,100000,100000,1000000],[N:1,0]\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //IN1/DI1 联动控制继电器 OUT1 点动输出 5 秒 strValue = "AT+LINKAGE1=3,5,100000,1,1000000\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //上电默认状态设置为吸合 strValue = "AT+DODEFSTA=1\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //使能状态主动上传 strValue = "AT+KPUPLOAD=1\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //使能掉电保存 strValue = "AT+KPKEEP=1\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(100000); } break; case 1: { //上传类型 uploadType 0:被动上传 1:定时上传 2:触发上传(所有通道) 3:触发上传(单个通道) int uploadType = 1; int uploadInterval = 300; //上传间隔 单位:10ms Console.WriteLine("DI 开关量输入接口测试"); //读取 DI 开关量输入接口 IN1/DI1 的状态 strValue = "AT+OCCH1=?\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //设置DI上传模式 strValue = $"AT+OCMOD={uploadType},{(uploadType == 1 ? uploadInterval : 0)}\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(100000); } break; case 2: { Console.WriteLine("AI 模拟量输入接口"); //上传类型 uploadType 0:被动上传 1:定时上传 2:触发上传 3:定时+触发上传 int uploadType = 1; int uploadInterval = 30; //上传间隔 单位:100ms //设置 AI1 接口的信号类型是电压型 strValue = "AT+AIMODE1=0\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //读取 AI1 接口实时采样值 strValue = "AT+AIVALUE1=?\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //设置AI上传模式 1:定时上传 2:触发上传 3:定时+触发上传 if (uploadType == 1) { //设置3秒上传1次AI1接口采样值(定时上传) uploadInterval = 30; strValue = $"AT+AIUPLOAD1={uploadType},{uploadInterval}\r\n"; } else if (uploadType == 2) { //设置AI1接口采样值高于2000并持续3秒触发上传 uploadInterval = 0; strValue = $"AT+AIUPLOAD1={uploadType},{uploadInterval},20,0,2000,0,0\r\n"; } else if (uploadType == 3) { //设置3秒上传1次和AI1接口采样值高于2000并持续3秒触发上传 uploadInterval = 30; strValue = $"AT+AIUPLOAD1={uploadType},{uploadInterval},20,0,2000,0,0\r\n"; } Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(100000); } break; case 3: { /* 发音人 0:晓玲(女声) 1:尹小坚(男声) 2:易小强(男声) 3:田蓓蓓(女声) 4:唐老鸭(效果器) 5:小燕子(女童声)*/ int TtsPronunciationPeople = 4; /* 语调 0 - 不识别汉语拼音 1 - 识别汉语拼音 注意: 1)拼音格式为:1 至 6 位字母 + 1 位数字 2)声调用 1 位数字表示 :1 阴平、2 阳平、3 上声、4 去声、5 轻声*/ int ToneOfVoice = 0; // 语速 1-10档 int TtsSpeed = 5; // 语调 1-10档 int TtsIntonation = 5; // 音量 1-10档 int TtsVolume = 5; // TTS循环次数 int TtsTimes = 1; // TTS间隔时间 int TtsInterval = 1; Console.WriteLine("VO 语音输出接口"); //控制 VO1 接口播放文字“泥人科技” strValue = "AT+TTS1=1,0,8\r\n泥人科技"; Console.WriteLine($"Send Command:AT+TTS1=1,0,8\\r\\n泥人科技"); client.Publish("/user/get", Encoding.GetEncoding("GB2312").GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //控制 VO1 接口播放语音文件 1 //strValue = "AT+AUDIO1=1,10\r\n"; //Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); //client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); //Thread.Sleep(1000); //VO1 接口每天早上 8 点播放语音文件 1 strValue = "AT+AUTOCONT=2,TASK1,[CYC:1],[T:3,0|1|2|3|4|5|6,08:00:00],[VO:0,1,1,10,1,0],[N:1,0]\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //IN1/DI1 联动播放语音文件 1 strValue = "AT+AUTOCONT=2,TASK1,[CYC:1],[DI:3,1,1],[VO:0,1,1,10,1,0],[N:1,0]\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //文本播放拓展(发音人、声调、语速、语调) //使用VO1 接口、晓玲的声音 语速:5 语调:5 音量:5播放“泥人科技1234567890” string[] pronunciationPeople = new string[] { "3", "51", "52", "53", "54", "55" }; //if (string.IsNullOrEmpty(txt)) return; string txt = "[m" + pronunciationPeople[TtsPronunciationPeople] + "][s" + TtsSpeed.ToString() + "][t" + TtsIntonation.ToString() + "][v" + TtsVolume.ToString() + "]" + $"欢迎[i{ToneOfVoice}]shi3用泥人科技"; //以GB2312的格式转为byte数组 byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(txt); strValue = "AT+TTS1=1,0," + TtsTimes.ToString() + "," + TtsInterval.ToString() + "," + bytes.Length.ToString() + "\r\n" + txt; Console.WriteLine($"Send Command:AT+TTS1=1,0,{TtsTimes},{TtsInterval},{bytes.Length}\\r\\n{txt}"); client.Publish("/user/get", Encoding.GetEncoding("GB2312").GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(100000); } break; case 4: { //上传类型 uploadType 0:被动上传 1:定时上传 2:触发上传 3:定时+触发上传 int uploadType = 1; int uploadInterval = 30; //上传间隔 单位:100ms Console.WriteLine("电脑开关机接口测试"); //控制电脑开机 strValue = "AT+PCSSSTA=1\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //电脑每天早上 8 点定时开机 strValue = "AT+AUTOCONT=2,TASK1,[CYC:1],[T:3,0|1|2|3|4|5|6,08:00:00],[PC:0,1],[N:1,0]\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //读取电脑开关机状态 strValue = "AT+PCSSSTA=?\r\n"; Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(1000); //定时上传、触发上传、定时 + 触发上传 if (uploadType == 1) { //设置 3 秒上传 1 次电脑开关机状态 strValue = $"AT+PCSSSTA={uploadType},{uploadInterval}\r\n"; } else if (uploadType == 2) { //设置电脑开关机状态触发上传 strValue = $"AT+PCSSSTA={uploadType}\r\n"; } else if (uploadType == 3) { //设置3秒和触发电脑开关机 上传1次电脑开关机状态 strValue = $"AT+PCSSSTA={uploadType},{uploadInterval}\r\n"; } Console.WriteLine($"Send Command:{strValue.Trim()}\\r\\n"); client.Publish("/user/get", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); Thread.Sleep(100000); } break; default: break; } } } }