串口发送按键信息

展开阅读全文

串口发送接收数据信息(接收不到信息超时)

10-26

我写了个发送接收程序,不知道哪里错了,我也刚了解这个,请各位大虾帮我找找问题出在哪?谢谢rn急!!!!!!!!!!!rnrnrnrnusing System;rnusing System.Collections.Generic;rnusing System.ComponentModel;rnusing System.Data;rnusing System.Drawing;rnusing System.Windows.Forms;rnusing System.IO.Ports;rnrnnamespace Serialexpamplernrn partial class Form1 : Formrn rn PropertyPage pp = new PropertyPage();rnrn SerialPort sp = new SerialPort();rnrn public Form1()rn rn InitializeComponent();rn rn rn private void propertyButton_Click(object sender, EventArgs e)rn rn pp.ShowDialog();rnrn propertyButton.Hide();rn rnrn private void sendButton_Click(object sender, EventArgs e)rn rn tryrn rn sp.WriteLine(textBox.Text);//发送数据rn textBox.Text = "";rn rn catch (System.Exception ex)rn rn baudRatelLabel.Text = ex.Message;rn rnrn rnrn private void ReadButton_Click(object sender, EventArgs e)rn rn tryrn rn textBox.Text = "";rn textBox.Text = sp.ReadLine();//接受数据rn rn catch(System.Exception ex)rn rn baudRatelLabel.Text = ex.Message;rn rn rnrn private void Form1_Load(object sender, EventArgs e)rn rnrn rnrn private void Form1_FormClosing(object sender, FormClosingEventArgs e)rn rn MessageBox.Show("Do u want to Close the App");rn sp.Close();rn rnrn private void startCommButton_Click(object sender, EventArgs e)rn rn startCommButton.Hide();rn sendButton.Show();rn ReadButton.Show();rn textBox.Show();rn rnrn private void saveStatusButton_Click_1(object sender, EventArgs e)rn rn if (pp.bRate == "" && pp.sBits == "")rn rn dataBitLabel.Text = "BaudRate = " + sp.BaudRate.ToString();//设置串口通讯波特率rn readTimeOutLabel.Text = "StopBits = " + sp.StopBits.ToString();//设置串口通讯停止位rn rn elsern rn dataBitLabel.Text = "BaudRate = " + pp.bRate;rn readTimeOutLabel.Text = "StopBits = " + pp.sBits;rn rnrn parityLabel.Text = "DataBits = " + sp.DataBits.ToString();//设置串口通讯数据位rn stopBitLabel.Text = "Parity = " + sp.Parity.ToString();//设置串口通讯奇偶校验位rn readTimeOutLabel.Text = "ReadTimeout = " + rn sp.ReadTimeout.ToString();rnrn if (propertyButton.Visible == true)rn propertyButton.Hide();rn saveStatusButton.Hide();rn startCommButton.Show();rnrn tryrn rn sp.Open();rn sp.ReadTimeout = 500;rn rn catch (System.Exception ex)rn rn baudRatelLabel.Text = ex.Message;rn rn rnrn rnrn 论坛

没有更多推荐了,返回首页