Click here to Skip to main content
16,023,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends
i write code for serial port in smart device using C# in that i can connect/disconnect the Available ports but I cant send the data to serial port and read also I tried this code please help me it very urgent
in my code having
i used Two Labels in that
one Label for Available COM Ports
second Label for connect and disconnect show on this label
one combox
three buttons (connect,disconnect,send)
one Rich text box
one text box

//THSI IS SMART DEVICE PROJECT USING c#

C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SerialPort port = new SerialPort();
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                #region Display all available COM Ports
                string[] ports = SerialPort.GetPortNames();
                // Add all port names to the combo box:
                foreach (string port in ports)
                {
                    //this.cboPortName.Items.Add(port);
                    this.cbbCOMPorts.Items.Add(port);
                }
                #endregion
                this.btnDisconnect.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnConnect_Click_1(object sender, EventArgs e)
        {
            if (port.IsOpen)
            {
                port.Close();
            }
            try
            {
                {
                    port.PortName = cbbCOMPorts.Text;
                    port.BaudRate = 96000;
                    port.Parity = Parity.None;
                    port.DataBits = 8;
                    port.StopBits = StopBits.One;
                }
                //.Encoding = System.Text.Encoding.Unicode 
                port.Open();
                lblMessage.Text = cbbCOMPorts.Text + " Connected. ";
                btnConnect.Enabled = false;
                btnDisconnect.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            txtDataToSend.Focus();
        }
        private void btnDisconnect_Click_1(object sender, EventArgs e)
        {
            try
            {
                port.Close();
                lblMessage.Text = port.PortName + " disconnected.";
                btnConnect.Enabled = true;
                btnDisconnect.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                port.Write(txtDataToSend.Text);
               //port.writline(txtDataToSend.Text);

                //txtDataReceived->IS a data Received Text Box name 
               //txtDataToSend->is a data send Text Box name

       txtDataReceived.Text = txtDataReceived.Text + txtDataToSend.Text;
                txtDataReceived.ScrollToCaret();
               }
               txtDataToSend.Text = string.Empty;
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
Posted
Updated 8-Feb-10 20:00pm
v4

1 solution

Set the read and write timeout values as well, as the default values are InfiniteTimeout, it may be that the send will timeout with a useful message on why if you set the timeout values.
MIDL
port.PortName = cbbCOMPorts.Text;
port.BaudRate = 96000;
port.Parity = Parity.None;
port.DataBits = 8;
port.StopBits = StopBits.One;

port.ReadTimeout = 500;
port.WriteTimeout = 500;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900