none
C# 多串口通信 RRS feed

  • 问题

  • 网上找来个串口的activex控件,建立个工程,单串口,首发OK

    但是,想要操作多串口时,增加一个串口,目前就要拖一个串口控件到FORM1上,这样的话,界面就非常乱了,甚至放置不下

     

    // form1 里的代码:
    namespace AllComContronl
    {
        public partial class Form1 : Form
        {       
            public event LeafEvent.DataSendHandler ComEventDataSend;
    
            public Form1()
            {
                InitializeComponent();
                this.ComEventDataSend += new LeafSoft.Lib.LeafEvent.DataSendHandler(this.ComDataSender_EventDataSend);           
                netRs2321.DataReceived += new LeafEvent.DataReceivedHandler(this.Configer_DataReceived);
            }
            private void Configer_DataReceived(object sender, byte[] data)
            {           
                this.BeginInvoke(new MethodInvoker(delegate
                {
                    listBox1.Items.Add(sender.ToString()+" :  " + AddData(data));
                }));
            }
    
            public StringBuilder AddData(byte[] data)
            { 
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                {
                    sb.AppendFormat("{0:x2}" + " ", data[i]);
                }
                return sb;           
            }
    
            private bool ComDataSender_EventDataSend(byte[] data)
            {
                if (netRs2321.SendData(data) == true)
                {
                    return true;
                }           
                return false;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                //点击了发送按钮 
                if (ComEventDataSend != null)
                {
                    byte[] data = { 0x40, 0x06, 0x0EE, 0x01, 0x00, 0x00, 0x00, 0xcb };
                    ComEventDataSend(data);
                }
            }
        }
    }
    

    上面代码,或者说调用串口控件,感觉增加控件次数越多,越乱会

    初步想做成如下效果

      在窗体上增加一个树形控件

      调用一次串口控件(在菜单里,或按钮),在树形控件上以 串口(COM1,COM2...)号为子项名增加一个子项, 这样增加串口数量,也不占用太多界面空间。

      使用时,选择某个串口,就可以对某个串口操作。当然可以选择多个串口同时操作也。

     这样,又不知道如何调用串口控件...

    //串口控件代码:
    namespace LeafSoft.Units
    {
        public partial class NetRs232 :UserControl, ICommunication
        {
            private SerialPort ComDevice = new SerialPort();
            
            public NetRs232()
            {
                InitializeComponent();
                drpComList.Items.AddRange(SerialPort.GetPortNames());
                if (drpComList.Items.Count > 0)
                {
                    drpComList.SelectedIndex = 0;
                    btnCom.Enabled = true;
                }
                drpBaudRate.SelectedIndex = 5;
                drpParity.SelectedIndex = 0;
                drpDataBits.SelectedIndex = 0;
                drpStopBits.SelectedIndex = 0;
                ComDevice.DataReceived+=new SerialDataReceivedEventHandler(Com_DataReceived);
            }
    
            /// <summary>
            /// 输出数据
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                byte[] ReDatas = new byte[ComDevice.BytesToRead];
                ComDevice.Read(ReDatas, 0, ReDatas.Length);//读取数据
                DataReceived(this,ReDatas);//输出数据
            }
    
            /// <summary>
            /// 串口打开/关闭
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnCom_Click(object sender, EventArgs e)
            {
                if (ComDevice.IsOpen==false)
                {
                    ComDevice.PortName = drpComList.SelectedItem.ToString();
                    ComDevice.BaudRate = Convert.ToInt32(drpBaudRate.SelectedItem.ToString());
                    ComDevice.Parity = (Parity)Convert.ToInt32(drpParity.SelectedIndex.ToString());
                    ComDevice.DataBits = Convert.ToInt32(drpDataBits.SelectedItem.ToString());
                    ComDevice.StopBits = (StopBits)Convert.ToInt32(drpStopBits.SelectedItem.ToString());
                    try
                    {
                        ComDevice.Open();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    btnCom.Text = "关闭串口";
                    //picComStatus.BackgroundImage = Properties.Resources.greenlight;
                }
                else
                {
                    try
                    {
                        ComDevice.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    btnCom.Text = "打开串口";
                    //picComStatus.BackgroundImage = Properties.Resources.redlight;
                }
    
                drpComList.Enabled = !ComDevice.IsOpen;
                drpBaudRate.Enabled = !ComDevice.IsOpen;
                drpParity.Enabled = !ComDevice.IsOpen;
                drpDataBits.Enabled = !ComDevice.IsOpen;
                drpStopBits.Enabled = !ComDevice.IsOpen;
            }
    
            public event LeafEvent.DataReceivedHandler DataReceived;
            /// <summary>
            /// 发送数据
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="data"></param>
            public bool SendData(byte[] data)
            {
                if (ComDevice.IsOpen)
                {
                    try
                    {
                        ComDevice.Write(data, 0, data.Length);//发送数据
                        return true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("串口未打开", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return false;
            }
    
            /// <summary>
            /// 关闭串口
            /// </summary>
            public void ClearSelf()
            {
                if (ComDevice.IsOpen)
                {
                    ComDevice.Close();
                }
            }
        }
    }

    2014年9月3日 2:07

答案

  • 你好,

    关于Winform程序串口通信的问题, 建议你重新把这个帖子发到window form forum.

    http://social.msdn.microsoft.com/Forums/windows/en-US/home?forum=winforms

    谢谢


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    2014年9月4日 3:29
    版主

全部回复

  • 你好,

    根据上述,没有看到WCF服务相关的内容。这里是WCF论坛,您的问题如果不是关于WCF的就请关闭该帖。

    谢谢


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    2014年9月3日 7:30
    版主
  • 转不了到合适的板块呀

    没权限

    请版主转一下

    2014年9月4日 2:36
  • 你好,

    关于Winform程序串口通信的问题, 建议你重新把这个帖子发到window form forum.

    http://social.msdn.microsoft.com/Forums/windows/en-US/home?forum=winforms

    谢谢


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    2014年9月4日 3:29
    版主