private void appendReceiveBytes(byte[] receive, int len) { string gap; if (checkBoxGap.Checked) { gap = " "; } else { gap = ""; } if (receive == null) { return; } len = len > receive.Length ? receive.Length : len; if (len == 0) { return; } byte[] data = new byte[len]; Array.Copy(receive, data, len); receiveShowCount += len; switch (currentCode) { case CODE_BIT: { if (currentBitOrder == BIT_LSB_FIRST) { textBoxReceive.Text += Assist.BytesToBitsLsbString(data, gap) + gap; } else { textBoxReceive.Text += Assist.BytesToBitsMsbString(data, gap) + gap; } } break; case CODE_HEX: { textBoxReceive.Text += Assist.ToHexString(data, gap) + gap; } break; case CODE_ASCII: default: textBoxReceive.Text += System.Text.Encoding.Default.GetString(data); break; } textBoxReceive.ScrollToCaret(); labelReceiveCount.Text = receiveCount.ToString(); }
private void buttonHdlcEncode_Click(object sender, EventArgs e) { string gap; if (checkBoxGap.Checked) { gap = " "; } else { gap = ""; } byte[] data; data = getInputBytes(); if (data == null) { return; } byte[] hdlc = Assist.HdlcEncode(data); textBoxInput.Text = Assist.ToHexString(hdlc, gap); currentCode = CODE_HEX; radioButtonHex.Checked = true; }
private string getHdlcInfoString(byte[] hdlc) { int index; string gap; if (checkBoxGap.Checked) { gap = " "; } else { gap = ""; } string hdlc_info = string.Empty; if (hdlc == null || hdlc.Length < 4) { return(string.Empty); } index = Array.IndexOf(hdlc, (byte)0x7E, 1);//寻找7E while (index > 1) { byte[] hdlc_item = hdlc.Take(index + 1).ToArray(); hdlc_info += Assist.ToHexString(hdlc_item, gap) + "\r\n[ \r\n"; hdlc_info += parse_hdlc_bytes(hdlc_item) + " \r\n]\r\n\r\n"; hdlc = hdlc.Skip(index + 1).ToArray(); if (hdlc.Length > 0) { index = Array.IndexOf(hdlc, (byte)0x7e, 1); } else { break; } } return(hdlc_info); }
private void radioButtonHex_CheckedChanged(object sender, EventArgs e) { string gap; if (checkBoxGap.Checked) { gap = " "; } else { gap = ""; } if (radioButtonHex.Checked != true || currentCode == CODE_HEX) { return; } Byte[] data = getInputBytes(); currentCode = CODE_HEX; if (data == null) { return; } textBoxInput.Text = Assist.ToHexString(data, gap); }
string parse_hdlc_bytes(byte[] hdlc_bytes) { const byte CTRL_U_FRAME_SNRM = 0x10; //置正常响应模式 const byte CTRL_U_FRAME_SARM_DM = 0x03; //置异步响应模式/断开方式 const byte CTRL_U_FRAME_SABM = 0x07; //置异步平衡模式 const byte CTRL_U_FRAME_SNRME = 0x1B; //置扩充正常响应模式 const byte CTRL_U_FRAME_SARME = 0x0b; //置扩充异步响应模式 const byte CTRL_U_FRAME_SABME = 0x0f; //置扩充异步平衡模式 const byte CTRL_U_FRAME_DISC_RD = 0x08; //断链/请求断链 const byte CTRL_U_FRAME_SIM_RIM = 0x01; //置初始化方式/请求初始化方式 const byte CTRL_U_FRAME_UP = 0x04; //无编号探询 const byte CTRL_U_FRAME_UI = 0x00; //无编号信息 const byte CTRL_U_FRAME_XID = 0x17; //交换识别 const byte CTRL_U_FRAME_RESET = 0x13; //复位 const byte CTRL_U_FRAME_FRMR = 0x11; //帧拒绝 const byte CTRL_U_FRAME_UA = 0x0C; //无编号确认 string show_info = "地址:0x{0:X02}\r\n" + "类型:{1}帧 CTRL:0x{2:X02} {3}"; byte[] hdlc = Assist.HdlcDecode(hdlc_bytes); if (hdlc == null) { return(string.Empty); } byte ctrl = hdlc[1]; string frame_type; string frame_ctrl_info; if ((ctrl & 0x01) == 0) { int ns = (ctrl & 0x0e) >> 1; int nr = (ctrl & 0xe0) >> 5; frame_type = "I"; frame_ctrl_info = string.Format("N(S)={0},N(R)={1}", ns, nr); } else if ((ctrl & 0x03) == 1) { byte s = (byte)((ctrl & 0x0C) >> 2); frame_type = "S"; string type; switch (s) { case 0: type = "RR"; break; case 1: type = "RNR"; break; case 2: type = "REJ"; break; case 3: default: type = "SREJ"; break; } frame_ctrl_info = string.Format("S={0}({1}),N(R)={2}", s, type, (ctrl & 0xe0) >> 5); } else { frame_type = "U"; byte cmd = (byte)((((ctrl) & 0xE0) >> 3) | (((ctrl) & 0x0c) >> 2)); frame_ctrl_info = string.Format("CMD=0x{0:X02} ", cmd); switch (cmd) { case CTRL_U_FRAME_SNRM: frame_ctrl_info += "SNRM"; break; case CTRL_U_FRAME_SARM_DM: frame_ctrl_info += "SARM_DM"; break; case CTRL_U_FRAME_SABM: frame_ctrl_info += "SABM"; break; case CTRL_U_FRAME_SNRME: frame_ctrl_info += "SNRME"; break; case CTRL_U_FRAME_SARME: frame_ctrl_info += "SARME"; break; case CTRL_U_FRAME_SABME: frame_ctrl_info += "SAME"; break; case CTRL_U_FRAME_DISC_RD: frame_ctrl_info += "DISC_RD"; break; case CTRL_U_FRAME_SIM_RIM: frame_ctrl_info += "SIM_RIM"; break; case CTRL_U_FRAME_UP: frame_ctrl_info += "UP"; break; case CTRL_U_FRAME_UI: frame_ctrl_info += "UI"; break; case CTRL_U_FRAME_XID: frame_ctrl_info += "XID"; break; case CTRL_U_FRAME_RESET: frame_ctrl_info += "RESET"; break; case CTRL_U_FRAME_FRMR: frame_ctrl_info += "FRMR"; break; case CTRL_U_FRAME_UA: frame_ctrl_info += "UA"; break; default: frame_ctrl_info += "未知指令"; break; } } string gap; if (checkBoxGap.Checked) { gap = " "; } else { gap = ""; } return(Assist.ToHexString(hdlc, gap) + "\r\n" + String.Format(show_info, hdlc[0], frame_type, ctrl, frame_ctrl_info)); }