none
Use c# to connect 2 arduinos, and monitor the data

    Pertanyaan

  • I designed a UI based on c#, which can received the data form one arduino, use serialPoort communication, it works well, so i am trying to design another serialPort communication in my UI, let it can connect another arduino at same time, i totally did the same thing like serialPort 1, andi found the serialPort 1 but works  serialPort2 doesnt. i had add the new system.io.port.serialPort. and the code is same with previous one, so can you help me to find out the question? thank you !!!!!!!!!!!!!!
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO.Ports;
    using System.IO;
    using System.Threading;
    
    namespace energy
    {
        public partial class Smart_meter : Form
        {
            string from_bs_2;
            string from_EM;
            double[,] power = new double[4, 23];
            int chushihua = 0;
            int chuqi_changshu = 0;
            int n_1 = 0;
            int n_2 = 0;
            double[,] power_display_f = new double[4, 23];
            string path =   "C:\\Users\\Public\\data\\data_logging.txt";
            string path_m = "C:\\Users\\Public\\data\\energy_monitoring.txt";
    
            public Smart_meter()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                String[] input = SerialPort.GetPortNames();
                comboBox1.Items.AddRange(input);
                comboBox16.Items.AddRange(input);
            }
    
            void button1(object sender, EventArgs e)
            {
                try
                {
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
                    serialPort1.DataBits = Convert.ToInt32(comboBox3.Text);
                    serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox4.Text);
                    serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox5.Text);
                    serialPort1.Open();
                    progressBar1.Value = 100;
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();
                    progressBar1.Value = 0;
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                if (serialPort1.IsOpen)
                {
                    string output;
                    output = textBox2.Text;
                    serialPort1.Write(output);
                    textBox2.Text = "";
                    from_bs_2 = "";
                    serialPort1.DiscardOutBuffer();
                }
            }
    
            void button7_Click(object sender, EventArgs e)
            {
                try
                {  
                    serialPort2.PortName = comboBox16.Text;
                    serialPort2.BaudRate = Convert.ToInt32(comboBox15.Text);
                    serialPort2.DataBits = Convert.ToInt32(comboBox13.Text);
                    serialPort2.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox14.Text);
                    serialPort2.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox12.Text);
                    serialPort2.Open();
                    progressBar2.Value = 100;
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            private void button8_Click(object sender, EventArgs e)
            {
                if (serialPort2.IsOpen)
                {
                serialPort2.Close();
                progressBar2.Value = 0;
                }
            }
    
            private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                Thread.Sleep(1000);  // important
                from_bs_2 += serialPort1.ReadExisting();
                Thread.Sleep(1000);
                this.Invoke(new EventHandler(showdata));
                Thread.Sleep(1000);
            }
    
            private void serialPort2_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                Thread.Sleep(1000);  // important
                from_EM += serialPort2.ReadExisting();
                Thread.Sleep(1000);
                this.Invoke(new EventHandler(showdata_2));
                Thread.Sleep(1000);
                //  textBox1.Text += "laishujule";
            }


    Kamis, 19 Juli 2018 12.34

Semua Balasan

  • Hi Simlin Wang,

    Thank you for posting here.

    Since I do not have Arduino, I could not test your code.

    If you want to connect to Arduino, you could refer to the examples in Arduino official website.

    https://playground.arduino.cc/Csharp/SerialCommsCSharp

    Best Regards,

    Wendy

    Note: This response contains a reference to a third-party World Wide Web site. Microsoft is providing this information as a convenience to you.
    Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.
    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Senin, 23 Juli 2018 03.18