-4

I am trying to get read data from rs 485 communication interface and write in the textbox but i am not getting the data from this code. I am working on water measurement and I am beginner in c#. I have seen similar question like this but unable to get the answer. The data format is like this. D014802,+000.042,+000.082,003680,+000805.66,+025.25,0193FA,0.99697,0000,B7C9 Help me out .

 public partial class MainForm : Form
    {

    SerialPort aSerialPort;
    InputRegister mobjGlobalform2;
    Form3 mobjGlobalform3;
    LoginForm AdminLogin = new LoginForm();
    //Form6 softwareVersion = new Form6();
    bool isUserMode = true;
    public MainForm()
    {
        InitializeComponent();
        getAvailablePorts();
    }

    private void btn_close_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        InputRegister mobjform2 = new InputRegister();

        if (checkBox1.Checked)
        {
            mobjGlobalform2 = mobjform2;
            mobjform2.Show();
            if(isUserMode==true)
            {
                mobjform2.groupbox_form2Takuwa.Hide(); 
                mobjform2.Height = 215;
                mobjform2.Width = 575;

            }
        }
        else
        {
            mobjGlobalform2.Close();

            //mobjform2.Close();
        }
    }

    private void checkBox2_CheckedChanged(object sender, EventArgs e)
    {
        Form3 mobjform3 = new Form3();
        if (checkBox2.Checked)
        {
            mobjGlobalform3 = mobjform3;
            mobjform3.Show();
            if(isUserMode==true)
            {
                mobjform3.groupbx_form3takuwamode.Hide(); 
                mobjform3.Height = 254;
                mobjform3.Width = 407;
            }
        }
        else
        {
            mobjGlobalform3.Close();
            //mobjform2.Close();
        }
    }

    private void timer1_Tick_1(object sender, EventArgs e)
    {
        label13.Text = DateTime.Now.ToString();

       // splitContainer1.Panel2.Controls.Add(label13);
        if(AdminLogin.isAdminMode)
        {
            lbl_AdminLogout.Show();
            isUserMode = false;
        }
    }

    private void btn_Clear_Click(object sender, EventArgs e)
    {
        txtbox_ShowData.Text = String.Empty;                  // clear the data from text box
    }

    private void MenuStrip_AdminLogin_Click(object sender, EventArgs e)
    {
        //Form5 mobjform5 = new Form5();
        AdminLogin.Show();
    }

    private void lbl_AdminLogout_Click(object sender, EventArgs e)
    {
        AdminLogin.isAdminMode = false;
        isUserMode = true;
        lbl_AdminLogout.Hide();
        MessageBox.Show("Logout Successful");
    }

    private void btn_SoftwareVersion_Click(object sender, EventArgs e)
    {
        //softwareVersion.Show();
        Form6 Versionform6 = new Form6(); 
        Versionform6.Height = 272;
        Versionform6.Width = 507;
        Versionform6.Show();
    }

    private void btn_connectaddress_Click(object sender, EventArgs e)
    {
        Setting addressconnectform6 = new Setting();
        addressconnectform6.Show();
        if(isUserMode == true)
        {
            addressconnectform6.groupbx_takuwaform7.Hide(); 
            addressconnectform6.Height = 257;
            addressconnectform6.Width = 657;
        }
        else
        {
            addressconnectform6.groupbx_takuwaform7.Show();

        }
   }
    #region combox
    // port show in combobox
    private void getAvailablePorts()
    {
        string[] ports = SerialPort.GetPortNames();
        cbbx_comport.Items.Clear();
        foreach (string comport in ports)
        {
            cbbx_comport.Items.Add(comport);
        }

    }
    #endregion

    private void btn_Connect_Click(object sender, EventArgs e)
    {
        initializeSensor();
        aSerialPort.DataReceived += new SerialDataReceivedEventHandler(Rs485DataReceivedEventHandler);

    }
    private void Rs485DataReceivedEventHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sData = sender as SerialPort;
        string recvData = sData.ReadLine();

      this.Invoke((MethodInvoker)delegate { DataReceived(recvData); });
    }
    private void initializeSensor()
    {
        aSerialPort = new SerialPort(cbbx_comport.Text);
        aSerialPort.BaudRate = 38400;
        aSerialPort.Parity = Parity.None;
        aSerialPort.StopBits = StopBits.One;
        aSerialPort.DataBits = 8;
        if (aSerialPort.IsOpen == false)
        {
            try
            {
                aSerialPort.Open();
                //aSerialPort.WriteLine("c");   //clear 
                //aSerialPort.WriteLine("o");
            }
            catch { }

        }
    }
    private void DataReceived(string recvData)
    {

        txtbx_sensorData.Text = recvData;

    }
sanjana
  • 1
  • 2
  • 1
    You have got to learn [how to use a debugger](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Dour High Arch Apr 02 '18 at 00:49
  • "not getting the data from this code" doesn't describe the problem. What is happening? Is there an error, if so what is it? Is the computer burning a hole through your desk? Please read [Why is β€œCan someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236) Provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) so others can *run* the code and actually help you. Who knows, just by creating a standalone piece of code you may discover the solution on your own. – NightOwl888 Apr 05 '18 at 03:01

1 Answers1

1

You are instantiating a local variable, not the field.

Instead of

SerialPort aSerialPort = new SerialPort(cbbx_comport.Text);

Try

aSerialPort = new SerialPort(cbbx_comport.Text);
Alex Wiese
  • 8,142
  • 6
  • 42
  • 71