无法从SerialPort读取

时间:2016-12-03 18:53:48

标签: c# serial-port

任何人都可以查看下面的C#代码。在float sıcaklık = Convert.ToByte(seriPort.ReadExisting());有一个问题但我找不到有什么问题?我猜SerialPort无法获取数据。

public partial class Form1 : Form
{
    SerialPort seriPort;
    public Form1()
    {
        InitializeComponent();
        seriPort = new SerialPort();
        seriPort.BaudRate = 9600;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Start();            
        try
        {
            seriPort.PortName = textBox1.Text;
            if (!seriPort.IsOpen)
            MessageBox.Show("Bağlantı Kuruldu");
        }
        catch
        {
            MessageBox.Show("Bağlantı Kurulmadı!");
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        try
        { 
            seriPort.Write("temperature");
            float sıcaklık = Convert.ToByte(seriPort.ReadExisting());
            textBox2.Text = sıcaklık.ToString();
            comboBox1.Items.Add(textBox2.Text);
            System.Threading.Thread.Sleep(100);
        }
        catch (Exception) {}
    }

    private void button2_Click(object sender, EventArgs e)
    {
        timer1.Stop();
        seriPort.Close();
    }
}

0 个答案:

没有答案