繁体   English   中英

C#中的串行通讯

[英]Serial communication in c#

我用ComboBox,TextBox和两个Button制作了一个简单的Windows窗体,以使用我的硬件设置串行协议。

但是,每当我发送一些东西时,都会收到硬件的答复,但C#不会显示它。 相反,它给出了一个异常,说明该操作已超时。 我什至用示波器检查我是否收到了一些东西,而且结果是肯定的。 但是C#不会显示前面所述的代码。

我在下面附上我的代码。 任何帮助都将受到欢迎。 提前致谢。

public partial class Form3 : Form
{
    string buffer;
    public SerialPort myComPort = new SerialPort();
    delegate void setTextCallback(string text);


    public Form3()
    {
        InitializeComponent();
    }

    private void Form3_Load(object sender, EventArgs e)
    {
        try
        {
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\CIMV2",
                "SELECT * FROM Win32_PnPEntity");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                if (queryObj["Caption"].ToString().Contains("(COM"))
                {

                    comboBox1.Items.Add(queryObj["Caption"]);
                }
            }
            comboBox1.Text = comboBox1.Items[0].ToString();
        }
        catch (ManagementException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void setText(string text)
    {
        if (textBox1.InvokeRequired)
        {
            setTextCallback tcb = new setTextCallback(setText);
            this.Invoke(tcb, new object[] { text });
        }
        else
        {
            textBox1.Text = text;
        }
    }

    void myComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            string myString = myComPort.ReadLine();
            setText(myString);
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
        myComPort.Close();

          //  button1.Enabled = false;
            string name = comboBox1.Text;
            string[] words = name.Split('(', ')');
            myComPort.PortName = words[1];
            myComPort.ReadTimeout = 5000;
           // myComPort.WriteTimeout = 500;
            myComPort.BaudRate = 9600;
            myComPort.DataBits = 8;
            myComPort.StopBits = StopBits.One;
            myComPort.Parity = Parity.None;
            myComPort.DataReceived += new SerialDataReceivedEventHandler(myComPort_DataReceived);

            myComPort.Open();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        myComPort.WriteLine("?GV1\r");   
    }
}

...不保证每个接收到的字节都会引发DataReceived事件...

尝试类似:

    private static void DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        // prevent error with closed port to appears
        if (!_port.IsOpen)
            return;
        // read data
        if (_port.BytesToRead >= 1)
        {
            // ...
            // read data into a buffer _port.ReadByte()

            DataReceived(sender, e);
        }

        // ...
        // if buffer contains data, process them
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM