0

我在我的 winform 应用程序中从串行端口读取数据时遇到问题......这是我的代码..

private void rtrvBtn_Click(object sender, EventArgs e)
    {
        btnid = 2;
        mySerialPort = new SerialPort(port);
        mySerialPort.Open();
        compacket(btnid);

        if (combuffer[0] != 0)
        {
            mySerialPort.Write(combuffer, 0, 4);
            System.Threading.Thread.Sleep(500);
            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(this.port_rec);
        }
        else
        {
            lblmsg.Text = "FL";
        }
        mySerialPort.Close();


        rtrvBtn.Enabled = false;
        conBtn.Enabled = true;
    }

public void port_rec(object sender, SerialDataReceivedEventArgs e)
    {
        string s = Convert.ToString(mySerialPort.ReadExisting());
        MessageBox.Show(s);
    }

所以在这里我没有得到任何数据,我也没有进入port_rec事件..任何人都可以帮助......我的串口设置是这样的......

 public void SettingRS232(string port)
    {
        try
        {
            mySerialPort = new SerialPort(port);
            mySerialPort.PortName = port;
            mySerialPort.BaudRate = 9600;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None;
            mySerialPort.ReadTimeout = 2000;
            mySerialPort.WriteTimeout = 500;

            mySerialPort.DtrEnable = true;
            mySerialPort.RtsEnable = true;


        }
        catch (Exception ex)
        {
            lblmsg.Text = ex.Message;
        }
    }

帮帮我,伙计们

4

1 回答 1

1

我猜这两个方法与 mySerialPort 字段在同一个类中。你不应该在点击事件中创建一个新的串口对象,你应该使用你已经拥有的那个。

于 2012-09-26T05:49:49.817 回答