private string calcScale(ParamItem paramItem) { if (paramItem.Scale != 0) { return((float.Parse(paramItem.ParamValue) * paramItem.Scale).ToString()); } return(paramItem.ParamValue); }
public Dictionary <string, ParamItem> parse(byte[] package) { Dictionary <string, ParamItem> map = new Dictionary <string, ParamItem>(); List <byte> ls = new List <byte>(package.Length); ls.AddRange(package); ls.RemoveRange(0, 7); ls.RemoveRange(package.Length - 10, 3); while (ls.Count > 0) { ParamItem p = newParamItem(ls); if (p != null) { map.Add(p.ParamName, p); } } return(map); }
private void pvParamValueNum_ValueChanged(object sender, EventArgs e) { MainForm main = (MainForm)this.Owner; if (paramId == "F002") { List <ParamItem> paramList = new List <ParamItem>(); ParamItem pm = null; byte[] data = new byte[2];//特殊处理(高位在前,低位在后) data[0] = 8; data[1] = byte.Parse(pvParamValueNum.Value.ToString()); int value = System.BitConverter.ToUInt16(data, 0); pm = new ParamItem("F002", "pvModuleAddr", 2, value.ToString(), 0, "uint2", ""); paramList.Add(pm); main.serialSetBase(paramList); main.logger("设置模块地址: " + pvParamValueNum.Value); } else if (paramId == "0024") { List <ParamItem> paramList = new List <ParamItem>(); ParamItem pm = null; pm = new ParamItem("0024", "pvDigitalAtt", 1, pvParamValueNum.Value.ToString(), 2, "uint1", "dB"); paramList.Add(pm); main.serialSetBase(paramList); main.logger("设置ATT: " + pvParamValueNum.Value); } else if (paramId == "0018") { List <ParamItem> paramList = new List <ParamItem>(); ParamItem pm = null; pm = new ParamItem("0018", "pvAlcControl", 2, pvParamValueNum.Value.ToString(), 10, "sint2", "dBm"); paramList.Add(pm); main.serialSetBase(paramList); main.logger("设置ALC: " + pvParamValueNum.Value); } }
private ParamItem newParamItem(List <byte> buffer) { string paramId = ""; byte[] pv = new byte[3]; string paramType = ""; try { paramId = getParamId(buffer, 0, 2).ToUpper(); int paramLength = getParamLength(buffer, 2); pv = getParamValue(buffer, 3, paramLength); buffer.RemoveRange(0, paramLength + 3); if (!Program.paramsMap.ContainsKey(paramId)) { return(null); } ParamItem paramItem = Program.paramsMap[paramId]; paramType = paramItem.Type; int paramScale = paramItem.Scale; string paramName = paramItem.ParamName; string paramUnit = paramItem.Unit; string paramValue = ""; if (paramType == "string") { /*List<byte> ls = new List<byte>(); * ls.AddRange(pv); * for (int m = pv.Length - 1; m >= 0; m--) * { * if (pv[m] == 0) * ls.RemoveAt(m); * else * break; * } * paramValue = System.Text.Encoding.Default.GetString(ls.ToArray<byte>());*/ paramValue = System.Text.Encoding.ASCII.GetString(pv); } else if (paramType.IndexOf("table") > -1) { if (paramType == "table type:sint2+uint2*10;scale:10,0") { StringBuilder sb = new StringBuilder(); List <byte> ls = new List <byte>(); ls.AddRange(pv); for (int i = 0; i < 10; i++) { byte[] v1 = new byte[2]; byte[] v2 = new byte[2]; ls.CopyTo(0, v1, 0, 2); ls.CopyTo(2, v2, 0, 2); ls.RemoveRange(0, 4); float value1 = System.BitConverter.ToInt16(v1, 0); value1 = value1 / 10; float value2 = System.BitConverter.ToUInt16(v2, 0); sb.Append(value1); sb.Append(","); sb.Append(value2); sb.Append(" "); } sb.Remove(sb.Length - 1, 1); paramValue = sb.ToString(); } else if (paramType == "table type:uint1+uint1+sint1*3;scale:0,0,2") { StringBuilder sb = new StringBuilder(); List <byte> ls = new List <byte>(); ls.AddRange(pv); for (int i = 0; i < 3; i++) { byte[] data = new byte[3]; ls.CopyTo(0, data, 0, 3); ls.RemoveRange(0, 3); int value1 = data[0]; int value2 = data[1]; float value3 = 0; int t = data[2]; if (t <= 127) { value3 = t; } else { //value3 = 127 - t; value3 = t - 256; } value3 = value3 / 2; sb.Append(value1); sb.Append(","); sb.Append(value2); sb.Append(","); sb.Append(value3); sb.Append(" "); } sb.Remove(sb.Length - 1, 1); paramValue = sb.ToString(); } } else { float value = 0; if (paramType == "sint2") { value = System.BitConverter.ToInt16(pv, 0); } else if (paramType == "uint2") { value = System.BitConverter.ToUInt16(pv, 0); } else if (paramType == "sint1") { int t = pv[0]; if (t <= 127) { value = t; } else { //value = 127 - t; value = t - 256; } } else if (paramType == "uint1") { value = pv[0]; } if (paramScale <= 0) { paramValue = value.ToString(); } else { paramValue = (value / paramScale).ToString(); } } return(new ParamItem(paramId, paramName, paramLength, paramValue, paramScale, paramType, paramUnit)); } catch (Exception e) { //解析参数失败 throw new Exception("解析参数失败: paramId:" + paramId + ",paramType:" + paramType + ", pv:" + System.Text.Encoding.ASCII.GetString(pv) + ", " + e.Message); } }