C#串口通信,包括打开串口,接收发送数据

时间:2013-09-02 04:04:51
【文件属性】:

文件名称:C#串口通信,包括打开串口,接收发送数据

文件大小:39KB

文件格式:RAR

更新时间:2013-09-02 04:04:51

C#串口通信,包括打开串口,接收发送数据

public partial class Form1 : Form { public Form1() { InitializeComponent(); } SerialPort port1 = new SerialPort(); string InputData = String.Empty; delegate void SetTextCallback(string text); private void Port_Select() {//获取机器中的串口地址 string[] ports = SerialPort.GetPortNames(); foreach (string port in ports) { comboBox1.Items.Add(port); } } private void Form1_Load_1(object sender, EventArgs e) { Port_Select(); this.comboBox1.SelectedIndex = 0; this.comboBox2.SelectedIndex = 0; } private void button1_Click(object sender, EventArgs e) { if (button1.Text == "关闭串口") //当要关闭串口的时候 { port1.DiscardOutBuffer(); port1.DiscardInBuffer(); port1.Close(); button1.Text = "打开串口"; label3.Text = "串口当前状况:未打开"; comboBox1.Enabled = true; comboBox2.Enabled = true; } else if (button1.Text == "打开串口") //当要打开串口的时候 { try { port1.PortName = comboBox1.SelectedItem.ToString(); port1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem); port1.DataBits = 8; port1.RtsEnable = true; port1.Open(); port1.DiscardOutBuffer(); port1.DiscardInBuffer(); button1.Text = "关闭串口"; comboBox1.Enabled = false; comboBox2.Enabled = false; label3.Text = "串口:" + comboBox1.SelectedItem.ToString() + " 波特率:" + comboBox2.SelectedItem.ToString() + " 数据位:8 "; } catch { button1.Text = "打开串口"; label3.Text = "串口:" + comboBox1.SelectedItem.ToString() + "打开失败"; MessageBox.Show("该串口无法打开"); } } } 资源中部分代码


【文件预览】:
串口通讯C#
----WindowsApplication1()
--------WindowsApplication1()
--------串口通讯.suo(13KB)
--------串口通讯.sln(932B)

网友评论

  • 不是实时接收返回数据的,不完善,点击接收就宕机
  • 再看看,还可以
  • 还没有看到源码
  • 很不错的资源,正好解决我的问题
  • 很不错的资源,正好用的上
  • 好。不错。。嘻嘻
  • 不知道怎么用啊
  • 可以通信。很好地借鉴资料
  • 谢谢 学习了 不错的资源
  • 不错,刚好能用到
  • 点击接收数据就会死机
  • 非常,对我很有用
  • 看了知道很多东西。
  • 很好,正是我需要的项目,谢谢
  • 写的不错,对新手来说帮助很大!
  • 在代码中找到了我需要用到的模块,帮助很大
  • 很不错,程序写的比较全面,能把串口码,但是最后串口数据读不出来
  • 有借鉴作用,对于学习串口通信是个好资源。
  • 谢谢,学习了!不过程序读取串口数据不行
  • 不错,对于学习串口通信是个好资源。
  • C#串口通信,包括打开串口,接收发送数据 public partial class Form1 : Form { public Form1() { InitializeComponent(); } SerialPort port1 = new SerialPort(); string InputData = String.Emp...
  • 挺好的 对初学者有帮助
  • 还是不错的 用于日常开发
  • 不错,可以参考
  • 写的还可以,新手可参考
  • 好,对初学者来说有借鉴作用
  • 很不错,程序写的比较全面,能把串口码,但是最后串口数据读不出来
  • 虽然不是我要的,但还是学习到了串口用法,谢了
  • 很好的学习资料,值得借鉴
  • 学习的资料很好的