本文介绍了如何使用C#将数据写入/读取到SmartDevice中的串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友
我使用C#在智能设备中为串行端口编写代码,因为我可以连接/断开可用端口,但是我无法将数据发送到串行端口并读取,我也尝试过此代码,请非常紧急帮助我
在我的代码中具有
我在其中使用了两个标签
一个可用COM端口的标签
第二个用于连接和断开连接的标签显示在此标签上
一个combox
三个按钮(连接,断开连接,发送)
一个Rich文本框
一个文本框

//这是使用c#的智能设备项目

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#

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);
           }
       }

推荐答案

port.PortName = cbbCOMPorts.Text;
port.BaudRate = 96000;
port.Parity = Parity.None;
port.DataBits = 8;
port.StopBits = StopBits.One;

port.ReadTimeout = 500;
port.WriteTimeout = 500;


这篇关于如何使用C#将数据写入/读取到SmartDevice中的串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:51