Flutter 鸿蒙化实战:flutter_jsbridge_plugin 适配 OpenHarmony,WebView 与 JS 双向通信开箱即用
2026/9/27 14:41:26
提供的 LabVIEW 程序框图,这段逻辑的主要功能是从输入的字符串/字节流中按固定偏移提取数据,解析为板卡状态及温度数据,并进行阈值比较与报警提示。
以下是转换后的C# 代码实现及逻辑说明:
首先定义一个结构体或类,用于存储从数据流中解析出的“板卡状态”簇(Cluster)信息:
publicclassBoardStatus{publicushortBoardId{get;set;}// 板卡编号publicushortHardwareVersion1{get;set;}// 板卡硬件版本1publicushortHardwareVersion2{get;set;}// 板卡硬件版本2publicushortSoftwareVersion1{get;set;}// 板卡软件版本publicushortSensor1AlarmThreshold{get;set;}// 传感器1报警阈值温度publicushortSensor2AlarmThreshold{get;set;}// 传感器2报警阈值温度publicushortSensor3AlarmThreshold{get;set;}// 传感器3报警阈值温度publicdoubleChannel1Temp{get;set;}// 通道1温度publicdoubleChannel2Temp{get;set;}// 通道2温度publicdoubleChannel3Temp{get;set;}// 通道3温度publicushortSoftwareVersion2{get;set;}// 板卡软件版本2}usingSystem;usingSystem.Collections.Generic;usingSystem.Text;publicclassLabViewParser{/// <summary>/// 解析输入数据并判断是否存在超温报警/// </summary>/// <param name="inputData">输入的字节数组(LabVIEW中为粉色的字符串/字节数据)</param>publicstaticvoidProcessData(byte[]inputData){// 确保数据长度足够(最大偏移量20,读取2字节,至少需要22字节)if(inputData==null||inputData.Length<22){thrownewArgumentException("输入数据长度不足");}// --- 1. 截取字符串/字节并转为数值 (String Subset + Hex/Byte Convert) ---// LabVIEW中截取偏移量分别为 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20,长度均为 2ushortval0=ReadUInt16(inputData,0);// 板卡编号ushortval2=ReadUInt16(inputData,2);// 板卡硬件版本1ushortval4=ReadUInt16(inputData,4);// 板卡硬件版本2ushortval6=ReadUInt16(inputData,6);// 板卡软件版本ushortval8=ReadUInt16(inputData,8);// 传感器1报警阈值温度ushortval10=ReadUInt16(inputData,10);// 传感器2报警阈值温度ushortval12=ReadUInt16(inputData,12);// 传感器3报警阈值温度ushortval14=ReadUInt16(inputData,14);// 通道1原始值ushortval16=ReadUInt16(inputData,16);// 通道2原始值ushortval18=ReadUInt16(inputData,18);// 通道3原始值ushortval20=ReadUInt16(inputData,20);// 板卡软件版本2// --- 2. 温度换算 (除以10,若小于0则取0) ---doubletemp1=Math.Max(0.0,val14/10.0);doubletemp2=Math.Max(0.0,val16/10.0);doubletemp3=Math.Max(0.0,val18/10.0);// --- 3. 打包成板卡状态簇 ---BoardStatusboardStatus=newBoardStatus{BoardId=val0,HardwareVersion1=val2,HardwareVersion2=val4,SoftwareVersion1=val6,Sensor1AlarmThreshold=val8,Sensor2AlarmThreshold=val10,Sensor3AlarmThreshold=val12,Channel1Temp=temp1,Channel2Temp=temp2,Channel3Temp=temp3,SoftwareVersion2=val20};// --- 4. 比较逻辑运算 (AND / OR 条件判断) ---// 4.1 通道温度与各自报警阈值比较 (Temp > Threshold)boolalarmCond1=temp1>val8;boolalarmCond2=temp2>val10;boolalarmCond3=temp3>val12;// 4.2 通道温度与固定值 101 ℃ 比较 (Temp > 101)boolhardLimit1=temp1>101;boolhardLimit2=temp2>101;boolhardLimit3=temp3>101;// 4.3 条件汇总// OR 门汇总:是否有任意通道超过其设定的阈值,或者超过101度boolhasOverTempThreshold=alarmCond1||alarmCond2||alarmCond3;boolhasOverHardLimit=hardLimit1||hardLimit2||hardLimit3;// 条件结构(Case Structure)触发判断:// 框图传入 Case 条件端子的是: (任意通道 > 101) OR (任意通道 > 阈值)boolcaseCondition=hasOverHardLimit||hasOverTempThreshold;// --- 5. Case 结构内部逻辑 [ True / 真 ] ---if(caseCondition){// 拼接提示字符串stringalarmMessage="H_Other: 负载超温报警 "+$"通道1温度:{temp1:F2}"+$"通道2温度:{temp2:F2}"+$"通道3温度:{temp3:F2}";// 输出报警信息或进行后续逻辑处理Console.WriteLine(alarmMessage);// 如果有需求,可以将结果输出到 UI 界面或触发系统报警 (对应 s? tf 输出)boolresultStatus=true;// 对应右侧逻辑与运算后的结果输出}}/// <summary>/// 辅助方法:从指定偏移量读取 2 个字节的大端序/小端序 16 位无符号整数/// </summary>privatestaticushortReadUInt16(byte[]data,intoffset){// 根据实际 LabVIEW 数据的字节序调整(通常网络/串口通信为大端序)// 如果是小端序,请改为:BitConverter.ToUInt16(data, offset);return(ushort)((data[offset]<<8)|data[offset+1]);}}| LabVIEW 图标/控件 | C# 实现对应 | 说明 |
|---|---|---|
| String Subset (截取字符串) | ReadUInt16(data, offset) | 从offset = 0, 2, 4 ... 20处截取 2 个字节的数据 |
| 打捆 (Bundle) | new BoardStatus { ... } | 将解析出的参数组合为一个对象/簇 |
取最大值与0比较 (Max(x, 0)) | Math.Max(0.0, val / 10.0) | 对原始数据除以10换算温度,并确保结果不小于0 |
大于比较 (>) | temp > threshold&temp > 101 | 对比传感器实际温度与阈值及硬性上限(101℃) |
或逻辑 (OR) | ` | |
| 条件结构 (Case Structure) | if (caseCondition) { ... } | 当触发超温条件时,执行字符串格式化输出及后续报警提示 |