/// <summary> /// �����豸�����������������ͨѶ�����ַ��� /// </summary> /// <param name="devpms">�豸�������ϼ���</param> /// <param name="cmdpms">ָ��������ϼ���</param> /// <returns>ͨѶ�ַ���</returns> public string CommandString(string devpms, string cmdpms, string datapms, ref devObj devobj) { //�����豸���� string[] datapmss = datapms.Split(','); devobj = setDevObj(devpms); string strpm = ""; string[] arrpm = cmdpms.Split(';'); string str = ""; int ilength = 0; pmObj pmXorObj = new pmObj(); for (int i = 0; i < arrpm.Length; i++) { string[] arr = arrpm[i].Split(','); NameObjectList lst = NameValueTag(arrpm[i]); pmObj pmobj = new pmObj(); pmobj.Name = getVarNameByHmStr(arr[0]); pmobj.Len = toIntval(valtag(arrpm[i], "{len}")); pmobj.DataType = valtag(arrpm[i], "{datatype}"); pmobj.Format = valtag(arrpm[i], "{format}"); if (pmobj.Format.StartsWith("��")) pmobj.Format = pmobj.Format.Substring(1); pmobj.Formats = valtag(arrpm[i], "{formats}"); pmobj.Scale = toFloatValue(valtag(arrpm[i], "{scale}")) == 0 ? 1 : toFloatValue(valtag(arrpm[i], "{scale}")); //pmobj.Scale = toIntval(valtag(arrpm[i], "{scale}")) == 0 ? 1 : toIntval(valtag(arrpm[i], "{scale}")); pmobj.Add = toIntval(valtag(arrpm[i], "{add}")); pmobj.SubItems = valtag(arrpm[i], "{subitems}"); pmobj.Value = lst.AllStringValues[0].ToLower(); string strlh = valtag(arrpm[i], "{lh}"); if (strlh == "") pmobj.LH = devobj.LH; else { if (strlh == "0") pmobj.LH = false; else pmobj.LH = true; } if (lst.Keys[0] == "{�豸��ַ}") { if (pmobj.DataType == "word") str = str + exchangeLH(devobj.ID, pmobj.LH); else str = str + exchangeLH(sVal10To16(devobj.ID, pmobj.Len / 8), pmobj.LH); continue; } string svalue = pmobj.Value; //ֵ ilength = basefun.toIntval(lst.AllStringValues[1].ToLower()); //λ�� string datetype = lst.AllStringValues[2].ToLower(); //�������� string format = lst.AllStringValues[3]; //��ʽ //int icale = basefun.toIntval(lst.AllStringValues[5].ToLower()); //�������� //if (icale == 0) icale = 1; switch (datetype) { case "string": if (pmobj.Value == "" || pmobj.Value == "?") svalue = basefun.valtag(datapms, pmobj.Name); long _iv = basefun.toIntval(svalue); svalue = this.sVal10To16(_iv.ToString()); break; case "asc": if (pmobj.Value == "" || pmobj.Value == "?") svalue = (basefun.valtag(datapms, pmobj.Name)).Trim(); svalue = AscToCharactor(svalue); int _lenerr = (pmobj.Len / 8 - svalue.Length / 2); if (_lenerr % 2 == 0) { string strsign = ""; for (int ib = 0; ib < _lenerr; ib++) { strsign = strsign + "20"; } svalue = strsign + svalue; } break; case "asc2": if (pmobj.Value == "" || pmobj.Value == "?") svalue = (basefun.valtag(datapms, pmobj.Name)).Trim(); svalue = AscToCharactor(svalue); _lenerr = (pmobj.Len / 8 - svalue.Length / 2); if (_lenerr % 2 == 0) { string strsign = ""; for (int ib = 0; ib < _lenerr; ib++) { strsign = strsign + "20"; } svalue = strsign + svalue; } if (svalue.Length > 16) svalue = svalue.Substring(0, 16); break; case "int": case "long": case "integer": if (pmobj.SubItems != "") { string[] subs = pmobj.SubItems.Split('+'); int isubs = 0; string _sbit = ""; for (int m = 0; m < subs.Length; m++) { string[] asubs = subs[m].Split('='); string[] asubpms = asubs[1].Split('#'); string subValue = asubpms[0]; if (subValue == "" || subValue == "?") subValue = basefun.valtag(datapms, asubs[0]); if (string.IsNullOrEmpty(subValue)) subValue = "0"; int iSublen = basefun.toIntval(asubpms[1]); string subDataType = asubpms[2]; string subFormat = asubpms[3]; string subScale = asubpms[4]; switch (subDataType.ToLower()) { case "double": //תΪ������ if (subFormat == "") subFormat = "{0:.00}"; if (subFormat.IndexOf("{") > -1) { subValue = string.Format(subFormat, Convert.ToDouble(subValue) * pmobj.Scale); } else subValue = string.Format("{" + subFormat + "}", Convert.ToDouble(subValue) * pmobj.Scale); subValue = basefun.toIntval(subValue).ToString(); subValue = sVal10To2(subValue, iSublen); break; case "integer": case "int": subValue = sVal10To2(subValue, iSublen); break; default: subValue = subValue; break; } //rtns = basefun.setvaltag(rtns, asubs[0], subValue); _sbit = _sbit + subValue; } svalue = this.sVal10To16(Convert.ToInt32(_sbit, 2).ToString()); } else { if (pmobj.Value == "" || pmobj.Value == "?") svalue = basefun.valtag(datapms, pmobj.Name); if (string.IsNullOrEmpty(svalue)) svalue = "0"; long _lv = Convert.ToInt64(basefun.toIntval64(svalue) * pmobj.Scale); svalue = this.sVal10To16(_lv.ToString()); if (pmobj.Format == "db") { svalue = basefun.toIntval64(svalue).ToString() + basefun.toIntval64(svalue).ToString(); } } break; case "float": case "double": if (pmobj.Value == "" || pmobj.Value == "?") svalue = basefun.valtag(datapms, pmobj.Name); if (string.IsNullOrEmpty(svalue)) svalue = "0"; if (pmobj.Format.IndexOf("{") > -1) svalue = string.Format(pmobj.Format, Convert.ToDouble(svalue) * pmobj.Scale); else if (!string.IsNullOrEmpty(pmobj.Format)) svalue = string.Format("{" + pmobj.Format + "}", Convert.ToDouble(svalue) * pmobj.Scale); else svalue = Convert.ToInt32(Convert.ToDouble(svalue) * pmobj.Scale).ToString(); svalue = basefun.toIntval(svalue).ToString(); svalue = this.sVal10To16(svalue, "F".PadLeft(pmobj.Len / 4, 'F')); break; case "bit": //�ѻ�����=1#1#String##+�ѻ�ʱ��=0.1#15#Double#{0:.0}#10; if (pmobj.SubItems != "") { string[] subs = pmobj.SubItems.Split('+'); int isubs = 0; string _sbit = ""; for (int m = 0; m < subs.Length; m++) { string[] asubs = subs[m].Split('='); string[] asubpms = asubs[1].Split('#'); string subValue = asubpms[0]; if (subValue == "" || subValue == "?") subValue = basefun.valtag(datapms, pmobj.Name + ".{" + asubs[0] + "}"); int iSublen = basefun.toIntval(asubpms[1]); string subDataType = asubpms[2]; string subFormat = asubpms[3]; string subScale = asubpms[4]; switch (subDataType.ToLower()) { case "double": //תΪ������ if (string.IsNullOrEmpty(subValue)) subValue = "0"; if (subFormat == "") subFormat = "{0:.00}"; if (subFormat.IndexOf("{") > -1) { subValue = string.Format(subFormat, Convert.ToDouble(subValue) * 10); } else subValue = string.Format("{" + subFormat + "}", Convert.ToDouble(subValue) * pmobj.Scale); subValue = sVal10To2(subValue, iSublen); break; case "integer": case "int": if (string.IsNullOrEmpty(subValue)) subValue = "0"; subValue = sVal10To2(subValue, iSublen); break; default: subValue = subValue; break; } //rtns = basefun.setvaltag(rtns, asubs[0], subValue); _sbit = _sbit + subValue; } try { svalue = this.sVal10To16(Convert.ToInt32(_sbit, 2).ToString()); } catch (Exception ex) { svalue = "00"; }; } else { if (string.IsNullOrEmpty(svalue)) svalue = "0"; if (pmobj.Value == "" || pmobj.Value == "?") svalue = basefun.valtag(datapms, pmobj.Name); if (string.IsNullOrEmpty(svalue)) { return ""; } svalue = this.sVal10To16(Convert.ToInt32(svalue, 2).ToString()); } break; case "date": case "datetime": if (pmobj.Value == "" || pmobj.Value == "?") svalue = basefun.valtag(datapms, pmobj.Name); if (string.IsNullOrEmpty(svalue)) svalue = DateTime.Now.ToString(); if (format.EndsWith("~BCD")) svalue = basefun.toBCDDatetime(svalue, format); else svalue = basefun.toHexDatetime(svalue, format); break; default: if (pmobj.Value == "" || pmobj.Value == "?") svalue = basefun.valtag(datapms, pmobj.Name); break; } if (ilength == 0) ilength = 8 * svalue.Length / 2; if (lst.Keys[0] == "{У����}") { pmXorObj = pmobj; break; } if (svalue == "") svalue = "0"; svalue = ComplementingBits(svalue, ilength); if (!format.EndsWith("~BCD")) svalue = exchangeLH(svalue, pmobj.LH); str = str + svalue; Debug.WriteLine(str); } // -- ĩβ����У���� -- CrcClass crc = new CrcClass(); string xor = ""; switch (devobj.Paritymode) { case "0": xor = crc.CRCXOR(str.Substring(2, str.Length - 2)); break; case "1": xor = crc.CRCSUM(str.Substring(2, str.Length - 2)); break; } string vstr = exchangeLH(ComplementingBits(xor, ilength), pmXorObj.LH); //����ת���ַ����Բ�����֡ͷ��֡β���ֽڽ���ת�崦�� str = str.Substring(2, str.Length - 2) + vstr; str = transferWords(str, devobj.Exchanges); NameObjectList lst0 = NameValueTag(arrpm[0]); NameObjectList lst1 = NameValueTag(arrpm[arrpm.Length - 1]); string strCmd = lst0.AllStringValues[0] + str + lst1.AllStringValues[0]; strCmd = strCmd.ToUpper(); return strCmd; }
/// <summary> /// ���ݷ���ֵ�IJ����ṹ�������ֵ,������У��,������״ֵ̬ /// </summary> /// <param name="strResult">���ѻ������ַ���</param> /// <returns></returns> public string AnalysisEateryResults(string strResult, string strpms, devObj devobj, ref string state) { if (string.IsNullOrEmpty(strResult)) return null; //ȡ���ؽ���IJ����ṹ //���㷵��ֵ�����۳��� string[] arrpm = strpms.Split(';'); int ilength = 0; int verlength = 0; //У���ֵ��ֽ��� int bit0len = 0; //У���ֵ��ֽ��� int bit1len = 0; //У���ֵ��ֽ��� for (int i = 0; i < arrpm.Length; i++) { NameObjectList lst = NameValueTag(arrpm[i]); if (i == 0) bit0len = basefun.toIntval(lst.AllStringValues[1].ToLower()) / 8; if (i == arrpm.Length - 1) bit1len = basefun.toIntval(lst.AllStringValues[1].ToLower()) / 8; if (lst.Keys[0] == "{У����}") verlength = basefun.toIntval(lst.AllStringValues[1].ToLower()) / 8; ilength = ilength + basefun.toIntval(lst.AllStringValues[1].ToLower()) / 8; //�ֽ��� } if (strResult.Length / 2 != ilength) return null; //��������ַ������Ȳ�����8������Ч���� //У����,������ԣ��ؿ� CrcClass crc = new CrcClass(); string vstr = strResult.Substring(bit0len * 2, strResult.Length - bit0len * 2 - bit1len * 2 - verlength * 2); if ("0" == devobj.Paritymode) vstr = exchangeLH(crc.CRCXOR(vstr), devobj.LH); else vstr = exchangeLH(crc.CRCSUM(vstr), devobj.LH); if (strResult.Substring(strResult.Length - bit1len * 2 - verlength * 2, verlength * 2).ToLower() != vstr.ToLower()) return null; string[] strResults = String16ToArry(strResult); //����ת���ĸ�ʽҪ�������ֵ���� string svalue = getValByHmFormat(strpms, strResults, devobj, ref state); //string rdata = strRtnData.Substring(4, strRtnData.Length - 4); //if (state == "00") return String16ToArry(rdata); return svalue; }
/// <summary> /// 处理时,结果字符串示noHE的定义而确定是否已包含或未包含帧头帧尾。根据返回值的参数结构解析结果值,含多重校验,并返回状态值 /// </summary> /// <param name="strResult">待解析的结果字符串</param> /// <param name="strpms">参数配置集合</param> /// <param name="devobj">设备参数</param> /// <param name="noHE">包含帧头帧尾的状态字</param> /// <param name="state">结果集中的返回状态值</param> /// <returns></returns> public string AnalysisEateryResults(string strResult, string strpms, devObj devobj, Boolean noHE, ref string state) { if (string.IsNullOrEmpty(strResult)) return null; //取返回结果的参数结构 //计算返回值的理论长度 string[] arrpm = strpms.Split(';'); int ilength = 0; int verlength = 0; //校验字的字节数 int bit0len = 0; int bit1len = 0; for (int i = 0; i < arrpm.Length; i++) { if (noHE) { if (i == 0 || i == arrpm.Length - 1) continue; } NameObjectList lst = NameValueTag(arrpm[i]); string strval = lst.AllStringValues[1].ToLower(); if (i == 0) bit0len = basefun.toIntval(strval) / 8; if (i == arrpm.Length - 1) bit1len = basefun.toIntval(strval) / 8; if (lst.Keys[0] == "{校验字}") verlength = basefun.toIntval(strval) / 8; ilength += basefun.toIntval(strval) / 8; //字节数 } if (strResult.Length / 2 < ilength && 14 == strResult.Length) { strpms="@{帧头}=02,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{设备地址}=,@{len}=8,@{datatype}=string,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{状态}=,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{命令长度}=0,@{len}=16,@{datatype}=long,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{校验字}=,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{帧尾}=03,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}="; return AnalysisEateryResults(strResult, strpms, devobj, noHE, ref state); } if (strResult.Length / 2 < ilength && 16 == strResult.Length) { strpms = "@{帧头}=02,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{设备地址}=,@{len}=8,@{datatype}=string,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{控制场}=01,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{长度场}=2,@{len}=8,@{datatype}=int,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{状态}=,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{命令长度}=0,@{len}=8,@{datatype}=long,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{校验字}=,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{帧尾}=03,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}="; return AnalysisEateryResults(strResult, strpms, devobj, noHE, ref state); } if (strResult.Length / 2 != ilength) return null; //如果返回字符串长度不大于8,则无效返回 //校验字,如果不对,则返回空 CrcClass crc = new CrcClass(); string xor = ""; switch (devobj.Paritymode) { case "0": xor = crc.CRCXOR(strResult.Substring(bit0len * 2, strResult.Length - bit0len * 2 - bit1len * 2 - verlength * 2)); break; case "1": xor = crc.CRCSUM(strResult.Substring(bit0len * 2, strResult.Length - bit0len * 2 - bit1len * 2 - verlength * 2)); break; } //修正校验字长度未补位错误,暂不对数据校验码判断 string _xor = this.ComplementingBits(xor, verlength * 2, true, true); string vstr = exchangeLH(_xor, devobj.LH); //if (strResult.Substring(strResult.Length - bit1len * 2 - verlength * 2, verlength * 2).ToLower() != vstr.ToLower()) // return null; string[] strResults = String16ToArry(strResult); //根据转出的格式要求处理返回值集合 string svalue = getValByHmFormat(strpms, strResults, devobj, noHE, ref state); //string rdata = strRtnData.Substring(4, strRtnData.Length - 4); //if (state == "00") return String16ToArry(rdata); return svalue; }
/// <summary> /// ����ʱ������ַ���ʾnoHE�Ķ����ȷ���Ƿ��Ѱ�����δ����֡ͷ֡β�����ݷ���ֵ�IJ����ṹ�������ֵ,������У��,������״ֵ̬ /// </summary> /// <param name="strResult">�������Ľ���ַ���</param> /// <param name="strpms">�������ü���</param> /// <param name="devobj">�豸����</param> /// <param name="noHE">����֡ͷ֡β��״̬��</param> /// <param name="state">������еķ���״ֵ̬</param> /// <returns></returns> public string AnalysisEateryResults(string strResult, string strpms, devObj devobj, Boolean noHE, ref string state) { if (string.IsNullOrEmpty(strResult)) return null; //ȡ���ؽ���IJ����ṹ //���㷵��ֵ�����۳��� string[] arrpm = strpms.Split(';'); int ilength = 0; int verlength = 0; //У���ֵ��ֽ��� int bit0len = 0; int bit1len = 0; for (int i = 0; i < arrpm.Length; i++) { if (noHE) { if (i == 0 || i == arrpm.Length - 1) continue; } NameObjectList lst = NameValueTag(arrpm[i]); if (i == 0) bit0len = basefun.toIntval(lst.AllStringValues[1].ToLower()) / 8; if (i == arrpm.Length - 1) bit1len = basefun.toIntval(lst.AllStringValues[1].ToLower()) / 8; if (lst.Keys[0] == "{У����}") verlength = basefun.toIntval(lst.AllStringValues[1].ToLower()) / 8; ilength = ilength + basefun.toIntval(lst.AllStringValues[1].ToLower()) / 8; //�ֽ��� } if (strResult.Length / 2 < ilength && 14 == strResult.Length) { strpms = "@{֡ͷ}=02,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{�豸��ַ}=,@{len}=8,@{datatype}=string,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{״̬}=,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{�����}=0,@{len}=16,@{datatype}=long,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{У����}=,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{֡β}=03,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}="; return AnalysisEateryResults(strResult, strpms, devobj, noHE, ref state); } if (strResult.Length / 2 < ilength && 16 == strResult.Length) { strpms = "@{֡ͷ}=02,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{�豸��ַ}=,@{len}=8,@{datatype}=string,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{���Ƴ�}=01,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{���ȳ�}=2,@{len}=8,@{datatype}=int,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{״̬}=,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{�����}=0,@{len}=8,@{datatype}=long,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{У����}=,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}=;@{֡β}=03,@{len}=8,@{datatype}=word,@{format}=,@{formats}=,@{scale}=,@{subitems}="; return AnalysisEateryResults(strResult, strpms, devobj, noHE, ref state); } if (strResult.Length / 2 != ilength) return null; //��������ַ������Ȳ�����8������Ч���� //У����,������ԣ��ؿ� CrcClass crc = new CrcClass(); string xor = ""; switch (devobj.Paritymode) { case "0": xor = crc.CRCXOR(strResult.Substring(bit0len * 2, strResult.Length - bit0len * 2 - bit1len * 2 - verlength * 2)); break; case "1": xor = crc.CRCSUM(strResult.Substring(bit0len * 2, strResult.Length - bit0len * 2 - bit1len * 2 - verlength * 2)); break; } //����У���ֳ���δ��λ����,�ݲ�������У�����ж� string _xor = this.ComplementingBits(xor, verlength * 2, true, true); string vstr = exchangeLH(_xor, devobj.LH); //if (strResult.Substring(strResult.Length - bit1len * 2 - verlength * 2, verlength * 2).ToLower() != vstr.ToLower()) // return null; string[] strResults = String16ToArry(strResult); //����ת���ĸ�ʽҪ�������ֵ���� string svalue = getValByHmFormat(strpms, strResults, devobj, noHE, ref state); //string rdata = strRtnData.Substring(4, strRtnData.Length - 4); //if (state == "00") return String16ToArry(rdata); return svalue; }
/// <summary> /// 根据返回值的参数结构解析结果值,含多重校验,并返回状态值 /// </summary> /// <param name="strResult">消费机返回字符串</param> /// <returns></returns> public string AnalysisEateryResults(string strResult, string strpms, devObj devobj, ref string state) { if (string.IsNullOrEmpty(strResult)) return null; //取返回结果的参数结构 //计算返回值的理论长度 string[] arrpm = strpms.Split(';'); int ilength = 0; int verlength = 0; //校验字的字节数 int bit0len = 0; //校验字的字节数 int bit1len = 0; //校验字的字节数 for (int i = 0; i < arrpm.Length; i++) { NameObjectList lst = NameValueTag(arrpm[i]); if (i == 0) bit0len = basefun.toIntval(lst.AllStringValues[1].ToLower()) / 8; if (i == arrpm.Length - 1) bit1len = basefun.toIntval(lst.AllStringValues[1].ToLower()) / 8; if (lst.Keys[0] == "{校验字}") verlength = basefun.toIntval(lst.AllStringValues[1].ToLower()) / 8; ilength = ilength + basefun.toIntval(lst.AllStringValues[1].ToLower()) / 8; //字节数 } if (strResult.Length / 2 != ilength) return null; //如果返回字符串长度不大于8,则无效返回 //校验字,如果不对,则返回空 CrcClass crc = new CrcClass(); string vstr = strResult.Substring(bit0len * 2, strResult.Length - bit0len * 2 - bit1len * 2 - verlength * 2); if ("0" == devobj.Paritymode) vstr = exchangeLH(crc.CRCXOR(vstr), devobj.LH); else vstr = exchangeLH(crc.CRCSUM(vstr), devobj.LH); if (strResult.Substring(strResult.Length - bit1len * 2 - verlength * 2, verlength * 2).ToLower() != vstr.ToLower()) return null; string[] strResults = String16ToArry(strResult); //根据转出的格式要求处理返回值集合 string svalue = getValByHmFormat(strpms, strResults, devobj, ref state); //string rdata = strRtnData.Substring(4, strRtnData.Length - 4); //if (state == "00") return String16ToArry(rdata); return svalue; }