c#中的串行通信

时间:2013-02-15 09:40:19

标签: c#

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

但是,无论何时发送内容,我都会收到来自硬件的回复,但C#不显示它。相反,它给出了一个例外,说该操作已经超时。我甚至用示波器来检查我是否收到了一些东西而且是积极的。但是C#不显示前面所述的代码。

我在下面附上我的代码。 Anyhelp将受到欢迎。提前谢谢。

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

2 个答案:

答案 0 :(得分:0)

  

It say

     

...不保证每收到一个字节都会引发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
    }

答案 1 :(得分:0)