/// <summary> /// 为指定的APDU创建信息体并返回,但不自动添加进ASDU。 /// </summary> /// <param name="apdu">目标APDU</param> /// <param name="addr">信息体地址,为零时创建不含地址的顺序信息体。</param> /// <returns></returns> protected virtual Message CreateMessageForAPDU(APDU apdu, uint addr) { Message m; if (addr == 0) { if (!apdu.ASDU.SQ || apdu.ASDU.ActualMsgCount == 0) { throw new Exception("非顺序信息体或首个信息体地址不能为0"); } m = CreateNewMessageNoAddr(); } else { if (apdu.ASDU.SQ && apdu.ASDU.ActualMsgCount > 0) { if (addr == apdu.ASDU.Messages.Last().Address + 1) { m = CreateNewMessageNoAddr(); } else { throw new Exception("顺序信息体地址不连续"); } } else { m = CreateNewMessageWithAddr(); m.Address = addr; } } return(m); }
public void PutData(APDU apdu, Int16 val, uint addr = 0, bool iv = false, bool nt = false, bool sb = false, bool bl = false, bool ov = false) { var m = CreateMessageForAPDU(apdu, addr); m.SVA = val; m.QDS_IV = iv; m.QDS_NT = nt; m.QDS_SB = sb; m.QDS_BL = bl; m.QDS_OV = ov; apdu.ASDU.Messages.Add(m); }
public virtual APDU CreateAPDU(byte asduAddr, bool sq = false, bool pn = false, bool test = false) { var v = new APDU(this); v.ASDU = new ASDU(); v.ASDU.Type = ASDUType; v.ASDU.Address = asduAddr; v.ASDU.SQ = sq; v.ASDU.PN = pn; v.ASDU.Test = test; return(v); }
public void PutData(APDU apdu, float val, DateTime time, uint addr = 0, bool iv = false, bool nt = false, bool sb = false, bool bl = false, bool ov = false) { var m = CreateMessageForAPDU(apdu, addr); m.R = val; m.QDS_IV = iv; m.QDS_NT = nt; m.QDS_SB = sb; m.QDS_BL = bl; m.QDS_OV = ov; SetTime(m, time); apdu.ASDU.Messages.Add(m); }
/// <summary> /// 使用默认或指定的格式器组解析报文。 /// </summary> /// <param name="buf">报文数据</param> /// <param name="formatters">指定格式器组</param> /// <returns></returns> public ParseResult ParseAPDU(byte[] buf, Dictionary <byte, DatagramFormatterBase> formatters = null) { if (formatters == null) { formatters = DefaultDatagrams; } ParseResult result; result.APDU = null; result.Datagram = null; try { DatagramFormatterBase formatter = null; var APDU = new APDU(buf); if (APDU.Format == DatagramFormat.InformationTransmit) { var ASDU = new ASDU(buf, APDU.APCILength); APDU.ASDU = ASDU; if (formatters.TryGetValue(ASDU.Type, out formatter)) { APDU.Formatter = formatter; byte bi = APDU.APCILength + ASDU.HeaderLength; if (ASDU.SQ) { var m = new Message(formatter.ElementType, formatter.AddrLength, formatter.ExtraLength, formatter.TimeStampLength); for (int i = 0; i < formatter.AddrLength; i++) { m.Addr[i] = buf[bi++]; } for (int i = 0; i < formatter.ElementType.Length(); i++) { m.Element[i] = buf[bi++]; } for (int i = 0; i < formatter.ExtraLength; i++) { m.Extra[i] = buf[bi++]; } for (int i = 0; i < formatter.TimeStampLength; i++) { m.TimeStamp[i] = buf[bi++]; } ASDU.Messages.Add(m); var addr = m.Address; for (int k = 1; k < ASDU.MsgCount; k++) { m = new Message(formatter.ElementType, 0, formatter.ExtraLength, formatter.TimeStampLength); m.Address = ++addr; for (int i = 0; i < formatter.ElementType.Length(); i++) { m.Element[i] = buf[bi++]; } for (int i = 0; i < formatter.ExtraLength; i++) { m.Extra[i] = buf[bi++]; } for (int i = 0; i < formatter.TimeStampLength; i++) { m.TimeStamp[i] = buf[bi++]; } ASDU.Messages.Add(m); } } else { for (int k = 0; k < ASDU.MsgCount; k++) { var m = new Message(formatter.ElementType, formatter.AddrLength, formatter.ExtraLength, formatter.TimeStampLength); for (int i = 0; i < formatter.AddrLength; i++) { m.Addr[i] = buf[bi++]; } for (int i = 0; i < formatter.ElementType.Length(); i++) { m.Element[i] = buf[bi++]; } for (int i = 0; i < formatter.ExtraLength; i++) { m.Extra[i] = buf[bi++]; } for (int i = 0; i < formatter.TimeStampLength; i++) { m.TimeStamp[i] = buf[bi++]; } ASDU.Messages.Add(m); } } } } result.Datagram = formatter; result.APDU = APDU; } catch (Exception) { } return(result); }