//串口发送数据 public void SerialPort_Send(byte[] sendBytes) { try { sp.Write(sendBytes, 0, sendBytes.Length); if (_RichTextBox.InvokeRequired) { _RichTextBox.Invoke(new EventHandler(delegate { _RichTextBox.AppendText("发送:" + Function_Module.RecvProgress_STR(sendBytes) + "\n"); })); } else { _RichTextBox.AppendText("发送:" + Function_Module.RecvProgress_STR(sendBytes) + "\n"); } } catch (Exception ex) { if (_RichTextBox.InvokeRequired) { _RichTextBox.Invoke(new EventHandler(delegate { _RichTextBox.AppendText("发送:" + ex.Message + "\n"); })); } else { _RichTextBox.AppendText("发送:" + ex.Message + "\n"); } } }
/// <summary> /// 跨位转换进阶:输入指定位数,自动补零并有空格隔开 /// </summary> /// <param name="number">100</param> /// <param name="weishu">4</param> /// <returns>00 64</returns> public static string ByteBeyond_Selected(int number, int weishu) { //判断位数是否是偶数 if (weishu % 2 != 0) { return("-1"); } else { //判断使用ByteBeyond转换后去除空格的长度是否等于指定长度 if (Function_Module.ByteBeyond(number).Replace(" ", "").Length == Convert.ToInt32(weishu)) { return(Function_Module.ByteBeyond(number)); } else if ((Function_Module.ByteBeyond(number).Replace(" ", "").Length > weishu)) { return("-1"); } else { int changeValue = Convert.ToInt32(weishu) - Function_Module.ByteBeyond(number).Replace(" ", "").Length; string result = Function_Module.ByteBeyond(number); //判断应该加几个00 changeValue = changeValue / 2; for (int i = 1; i <= changeValue; i++) { result = "00 " + result; } return(result); } } }
public MainForm() { InitializeComponent(); dockPanel.ClosingPanel += DockPanel_ClosingPanel; //打开串口 textEdit_串口号.Text = Function_Module.GetConfig("串口号"); SerialPort = new SerialPort_Module(richTextBox1); SerialPortControl(true); _DataModule = new DataModule(); //开始测试模块 ucStartTest2 ucstart = CreateModule("延迟线收发组件_十六通道.Modules.ucStartTest2", _DataModule) as ucStartTest2; }
/// <summary> /// 跨位转换:输入一个十进制数,按十六进制每两位一个空格隔开并输出 /// </summary> /// <param name="number">500</param> /// <returns>01 F4</returns> public static string ByteBeyond(int number) { string init16 = Convert.ToString(number, 16); //检查长度是否为偶数 if (init16.Length % 2 == 0) { string two16 = init16; int quotinents = (two16.Length / 2) - 1; if (quotinents == 0) { return(two16); } else { for (int i = 1; i <= quotinents; i++) { two16 = two16.Insert(i * 2 + (i - 1), " "); } return(two16); } } //奇数情况 else { //补成偶数位 string two16 = Function_Module.AddZero_ToN(init16, init16.Length + 1); int quotinents = (two16.Length / 2) - 1; if (quotinents == 0) { return(two16); } else { for (int i = 1; i <= quotinents; i++) { two16 = two16.Insert(i * 2 + (i - 1), " "); } return(two16); } } }
/// <summary> /// 开关串口视图控制 /// </summary> /// <param name="Open"></param> private void SerialPortControl(bool Open) { try { if (Open) { SerialPort.SerialPort_Set(textEdit_串口号.Text, Convert.ToInt32(Function_Module.GetConfig("波特率"))); SerialPort.SerialPort_Open(); windowsUIButtonPanel1.Buttons.FindFirst(m => m.Properties.Caption == "打开串口(Q)").Properties.Image = global::延迟线收发组件_十六通道.Properties.Resources.squeeze_32x32; windowsUIButtonPanel1.Buttons.FindFirst(m => m.Properties.Caption == "打开串口(Q)").Properties.Caption = "关闭串口(Q)"; } else { SerialPort.SerialPort_Close(); windowsUIButtonPanel1.Buttons.FindFirst(m => m.Properties.Caption == "关闭串口(Q)").Properties.Image = global::延迟线收发组件_十六通道.Properties.Resources.stretch_32x32; windowsUIButtonPanel1.Buttons.FindFirst(m => m.Properties.Caption == "关闭串口(Q)").Properties.Caption = "打开串口(Q)"; } } catch (Exception ex) { XtraMessageBox.Show(ex.Message); } }