单页背景(单页背景怎么弄)
786
2022-05-28
标题虽然这么长,但是做法却十分简单 总共分为两个步骤 第一:获取传感器消息 第二:传给mqtt网络 第三:网页根据mqtt消息采取动作 总结:就是硬件上面操作,网页上实时查看硬件的操作动作 我们先来看第一个:获取传感器消息 首先看一下c#中对于串口通讯的操作(急于实现功能的各位可以跳过直接看下面的源码)
SerialPort类的常用属性
名 称
说 明
BaseStream
获取 SerialPort 对象的基础 Stream 对象
BaudRate
获取或设置串行波特率
BreakState
获取或设置中断信号状态
BytesToRead
获取发送缓冲区中数据的字节数
BytesToWrite
获取端口的载波检测行的状态
CDHolding
获取“可以发送”行的状态
CtsHolding
获取或设置每个字节的标准数据位长度
DiscardNull
获取或设置一个值,该值指示 Null 字节在端口和接收缓冲区之间传输时是否被忽略
DsrHolding
获取数据设置就绪 (DSR) 信号的状态
DtrEnable
获取或设置一个值,该值在串行通信过程中启用数据终端就绪 (DTR) 信号
Encoding
获取或设置传输前后文本转换的字节编码
Handshake
获取或设置串行端口数据传输的握手协议
IsOpen
获取一个值,该值指示 SerialPort 对象的打开或关闭状态
NewLine
获取或设置用于解释 ReadLine( )和WriteLine( )方法调用结束的值
Parity
获取或设置奇偶校验检查协议
ParityReplace
获取或设置一个字节,该字节在发生奇偶校验错误时替换数据流中的无效字节
PortName
获取或设置通信端口,包括但不限于所有可用的 COM 端口
ReadBufferSize
获取或设置 SerialPort 输入缓冲区的大小
ReadTimeout
获取或设置读取操作未完成时发生超时之前的毫秒数
ReceivedBytesThreshold
获取或设置 DataReceived 事件发生前内部输入缓冲区中的字节数
RtsEnable
获取或设置一个值,该值指示在串行通信中是否启用请求发送 (RTS) 信号
StopBits
获取或设置每个字节的标准停止位数
WriteBufferSize
获取或设置串行端口输出缓冲区的大小
WriteTimeout
获取或设置写入操作未完成时发生超时之前的毫秒数
名 称
说 明
Close
关闭端口连接,将 IsOpen 属性设置为False,并释放内部 Stream 对象
Open
从 SerialPort 输入缓冲区中读取
Read
从 SerialPort 输入缓冲区中读取
ReadByte
从 SerialPort 输入缓冲区中同步读取一个字节
ReadChar
从 SerialPort 输入缓冲区中同步读取一个字符
ReadLine
一直读取到输入缓冲区中的 NewLine 值
ReadTo
一直读取到输入缓冲区中指定 value 的字符串
Write
已重载。将数据写入串行端口输出缓冲区
WriteLine
将指定的字符串和 NewLine 值写入输出缓冲区
首先要添加
using System.IO; using System.IO.Ports;
1...在类的内部定义SerialPort com;
2...打开串口
com = new SerialPort(); com.BaudRate = 115200; com.PortName = "COM1"; com.DataBits = 8; com.Open();//打开串口
3...发送数据
Byte[] TxData ={1,2,3,4,5,6,7,8 }; com.Write(TxData, 0, 8);
4...接收数据 见下方源码
using System; using System.IO.Ports; using System.Linq; using System.Management; using System.Threading; using System.Windows.Forms; namespace CefSharp.MinimalExample.WinForms { public static class COMCtrl { public static string portName = ""; public static int baudRate = 115200; public static Parity parity = Parity.None; public static int dataBits = 8; public static StopBits stopBits = StopBits.One; public static string[] data;//用于存储6位数据 static SerialPort sp = null;//串口控制 public static string sensorNum = "";//传感器的序号 static CalcOnCrc16 OnCrc16; public static string audioName; public static void Init() { OnCrc16 = new CalcOnCrc16(); portName = GetPortName(); OpenPort(); } #region 打开与关闭端口 //获取 static string GetPortName() { string i = GetComNum().ToString(); return "COM" + i; } //打开端口 public static void OpenPort() { sp = new SerialPort(portName, baudRate, parity, dataBits, stopBits); try { sp.Open(); sp.ReadTimeout = 400; } catch (Exception ex) { MessageBox.Show(ex.Message); } } //关闭端口 public static void ClosePort() { try { sp.Close(); } catch (Exception ex) { MessageBox.Show("端口关闭失败" + ex.Message); } } #endregion static string Lock_Str = "Lock"; #region 获取端口号 public static int GetComNum() { int comNum = -1; string[] strArr = GetHarewareInfo(HardwareEnum.Win32_PnPEntity, "Name"); foreach (string s in strArr) { if (s.Length >= 23 && s.Contains("CH340")) { int start = s.IndexOf("(") + 3; int end = s.IndexOf(")"); comNum = Convert.ToInt32(s.Substring(start + 1, end - start - 1)); } } return comNum; } private static string[] GetHarewareInfo(HardwareEnum hardType, string propKey) { System.Collections.Generic.List
里面还有一个crc检验
using System; namespace CefSharp.MinimalExample.WinForms { public class CalcOnCrc16 { public ushort CRC16(byte[] bytes) { ushort value; ushort newLoad = 0xffff, In_value; int count = 0; for (int i = 0; i < bytes.Length; i++) { value = (ushort)bytes[i]; newLoad = (ushort)(Convert.ToInt32(value) ^ Convert.ToInt32(newLoad)); In_value = 0xA001; while (count < 8) { if (Convert.ToInt32(newLoad) % 2 == 1)//判断最低位是否为1 { newLoad -= 0x00001; newLoad = (ushort)(Convert.ToInt32(newLoad) / 2);//右移一位 count++;//计数器加一 newLoad = (ushort)(Convert.ToInt32(newLoad) ^ Convert.ToInt32(In_value));//异或操作 } else { newLoad = (ushort)(Convert.ToInt32(newLoad) / 2);//右移一位 count++;//计数器加一 } } count = 0; } return newLoad; } } }
PS:这个单片机是我请求一次,它才发回消息,不是主动的发,所以我的程序里面还有计时器循环请求状态. 下面就是发送到mqtt消息平台,首先接收
教程是这里的,如果看不懂直接看我源码 https://help.aliyun.com/document_detail/59721.html?spm=a2c4g.11186623.6.590.666432b1pvEQrL
using System; using System.Security.Cryptography; using System.Text; using System.Windows.Forms; using uPLibrary.Networking.M2Mqtt; using uPLibrary.Networking.M2Mqtt.Messages; namespace CefSharp.MinimalExample.WinForms { public class MqttCtrl { // //实例 ID,购买后从控制台获取 //static String instanceId = " "; // //此处填写购买得到的 MQTT 接入点域名 // static String brokerUrl = " "; // //此处填写阿里云帐号 AccessKey // static String accessKey = " "; // //此处填写阿里云帐号 SecretKey // static String secretKey = ""; // //此处填写在 MQ 控制台创建的 Topic,作为 MQTT 的一级 Topic // static String parentTopic = " "; // //此处填写客户端 ClientId,需要保证全局唯一,其中前缀部分即 GroupId 需要先在 MQ 控制台创建 // static String clientId = " " + DateTime.Now.Ticks.ToString(); // static MqttClient client = new MqttClient(brokerUrl); //实例 ID,购买后从控制台获取 static String instanceId = " "; //此处填写购买得到的 MQTT 接入点域名 static String brokerUrl = " "; //此处填写阿里云帐号 AccessKey static String accessKey = " "; //此处填写阿里云帐号 SecretKey static String secretKey = ""; //此处填写在 MQ 控制台创建的 Topic,作为 MQTT 的一级 Topic static String parentTopic = "SJJK"; //此处填写客户端 ClientId,需要保证全局唯一,其中前缀部分即 GroupId 需要先在 MQ 控制台创建 static String clientId = " " + DateTime.Now.Ticks.ToString(); static MqttClient client = new MqttClient(brokerUrl); public static void Init() { client.MqttMsgPublishReceived += client_recvMsg; client.MqttMsgPublished += client_publishSuccess; client.ConnectionClosed += client_connectLose; //username和 Password 签名模式下的设置方法,参考文档 https://help.aliyun.com/document_detail/48271.html?spm=a2c4g.11186623.6.553.217831c3BSFry7 String userName = "Signature|" + accessKey + "|" + instanceId; String passWord = HMACSHA1(secretKey, clientId); client.Connect(clientId, userName, passWord, true, 60); //订阅 Topic,支持多个 Topic,以及多级 Topic //string[] subTopicArray = { parentTopic + "/SamJan", parentTopic + "/subDemo2/level3" }; string[] subTopicArray = { parentTopic + "/SamJan", parentTopic + "/SensorState" }; //string[] subTopicArray = { parentTopic + "/SensorState" }; byte[] qosLevels = { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE }; //订阅 client.Subscribe(subTopicArray, qosLevels); //client.Publish(parentTopic + "/SamJan", Encoding.UTF8.GetBytes("hello mqtt,尚哲智能"), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false); //发送 P2P 消息,二级 topic 必须是 p2p,三级 topic 是接收客户端的 clientId //client.Publish(parentTopic + "/p2p/" + clientId, Encoding.UTF8.GetBytes("(P2P) hello mqtt"), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false); //System.Threading.Thread.Sleep(10000); //client.Disconnect(); } // public static void client_sendMsg(string msg) { client.Publish(parentTopic + "/SamJan", Encoding.UTF8.GetBytes(msg), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false); } static void client_sendMsgByP2P(string msg) { client.Publish(parentTopic + "/p2p/" + clientId, Encoding.UTF8.GetBytes(msg), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false); } static void client_disConnect() { client.Disconnect(); } static void client_recvMsg(object sender, MqttMsgPublishEventArgs e) { // access data bytes throug e.Message //MessageBox.Show("Recv Msg : Topic is " + e.Topic + " ,Body is " + Encoding.UTF8.GetString(e.Message)); if (e.Topic == "SJJK/SensorState") { COMCtrl.RequestSensor(Encoding.UTF8.GetString(e.Message)); } } static void client_publishSuccess(object sender, MqttMsgPublishedEventArgs e) { // access data bytes throug e.Message Console.WriteLine("Publish Msg Success"); } static void client_connectLose(object sender, EventArgs e) { // access data bytes throug e.Message Console.WriteLine("Connect Lost,Try Reconnect"); Init(); } public static string HMACSHA1(string key, string dataToSign) { Byte[] secretBytes = UTF8Encoding.UTF8.GetBytes(key); HMACSHA1 hmac = new HMACSHA1(secretBytes); Byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(dataToSign); Byte[] calcHash = hmac.ComputeHash(dataBytes); String calcHashString = Convert.ToBase64String(calcHash); return calcHashString; } } }
官方Demo: https://code.aliyun.com/aliware_mqtt/mqtt-demo/tree/master?spm=a2c4g.11186623.2.38.4181775ewvOQVo
与前端妹子沟通好,你们就可以愉快的吵架了.
MQTT web前端 硬件开发
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。