none
Serial Communication RRS feed

  • Question

  • Hey everyone i'm having a problem with a project for school

    we have to make something in windows forms with visual studios.

    We have to control a led with serial communication (wich includes we can pick our COM port with a combobox), have a clock, a keypad for a login and some other stuff.

    now i have made the clock the and the login in one form and the led control in another.

    but if i try to add them they don't work, if i add the led control to form 1 everything except the led control works and if i add the login to form 2 everything except the login works.

    I tought it might be something with the amount of buttons bcs i have quite some, but nothing on the internet says it has a max amount of buttons.

    I really don't know what to do and hope you can help me.

    My regards

    Ps. the code when i add the led control to the login in the attachements

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO.Ports;
    using System.Collections;
    using System.IO;
    
    namespace ConsoleKamerfinale
    {
        public partial class btnOn : Form
        {
            Timer t = new Timer();
            int user;
            int pressedTimes = 0;
    
            public btnOn()
            {
                InitializeComponent();
    
                foreach (string Poort in SerialPort.GetPortNames())
                {
                    cmbPoorten.Items.Add(Poort);
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                t.Interval = 1000;  
                t.Tick += new EventHandler(this.t_Tick);
                t.Start();
            }
    
            private void btnOn_Click(object sender, EventArgs e)
            {
                serialPort1.Write("ENGINE_1_ON#");
            }
    
            private void btnOff_Click(object sender, EventArgs e)
            {
                serialPort1.Write("ENGINE_1_OFF#");
            }
    
            private void btnOpen_Click(object sender, EventArgs e)
            {
                serialPort1.PortName = cmbPoorten.SelectedItem.ToString();
                serialPort1.Open();
            }
    
            private void btnLogin_Click(object sender, EventArgs e)
            {
                
    
                pressedTimes += 1;
                user = int.Parse(tbUsercode.Text);
                
                if (pressedTimes % 2 == 0)
                {
                    btnLogin.Text = "Sign in";
                    tbUsercode.Text = null;
                }
    
                else
                {
                    btnLogin.Text = "Sign out";                
                }
            }
    
            
            private void t_Tick(object sender, EventArgs e)
            {
                int hh = DateTime.Now.Hour;
                int mm = DateTime.Now.Minute;
                int ss = DateTime.Now.Second;
    
                string time = "";
    
                if (hh < 10)
                {
                    time += "0" + hh;
                }
                else
                {
                    time += hh;
                }
                time += ":";
    
                if (mm < 10)
                {
                    time += "0" + mm;
                }
                else
                {
                    time += mm;
                }
                time += ":";
    
                if (ss < 10)
                {
                    time += "0" + ss;
                }
                else
                {
                    time += ss;
                }
    
                lblDigitaleklok.Text = time;
            }
    
            private void btn1_Click(object sender, EventArgs e)
            {
                tbUsercode.Text += 1;
            }
    
            private void btn2_Click(object sender, EventArgs e)
            {
                tbUsercode.Text += 2;
            }
    
            private void btn3_Click(object sender, EventArgs e)
            {
                tbUsercode.Text += 3;
            }
    
            private void btn4_Click(object sender, EventArgs e)
            {
                tbUsercode.Text += 4;
            }
    
            private void btn5_Click(object sender, EventArgs e)
            {
                tbUsercode.Text += 5;
            }
    
            private void btn6_Click(object sender, EventArgs e)
            {
                tbUsercode.Text += 6;
            }
    
            private void btn7_Click(object sender, EventArgs e)
            {
                tbUsercode.Text += 7;
            }
    
            private void button8_Click(object sender, EventArgs e)
            {
                tbUsercode.Text += 8;
            }
    
            private void btn9_Click(object sender, EventArgs e)
            {
                tbUsercode.Text += 9;
            }
    
            private void btn0_Click(object sender, EventArgs e)
            {
                tbUsercode.Text += 0;
            }
        }
    }

    Saturday, May 5, 2018 3:59 PM

All replies

  • You need to do serial communications in a separate thread. Serial communications is not easy, especially not for beginners.

    We don't know what "don't work" means. It is necessary to explain what is happening or not happening.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, May 5, 2018 6:15 PM