0

我用 ComboBox、TextBox 和两个按钮制作了一个简单的 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");   
    }
}
4

2 回答 2

0

它说

... 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
    }
于 2013-02-15T10:15:12.147 回答
0

看看这个网址:

http://csharp.simpleserial.com/

而这个 WMI 的网址:

http://www.codeproject.com/Articles/32330/A-Useful-WMI-Tool-How-To-Find-USB-to-Serial-Adapto

于 2013-02-15T10:18:19.163 回答