C#实现串口通讯

        .NET提供了SerialPort类进行串口通信。

串口主要有以下几个参数: 

1.串口名称(PortName) 

2.波特率(BaudRate)

3.数据位(DataBits)

4.奇偶效应(Parity) 

5.停止位(StopBits)

        使用很简单,连我这个.NET新手也能很快上手.以下是从网上找到并自己修改后的参考代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;

namespace CsharpComm
  {
      /// <summary>
 /// Window1.xaml 的交互逻辑
 /// </summary>
      public partial class Window1 : Window
      {
          public Window1()
          {
              InitializeComponent();
          }
  
          //定义 SerialPort对象
          SerialPort port1;
  
          //初始化SerialPort对象方法.PortName为COM口名称,例如"COM1","COM2"等,注意是string类型
          public void InitCOM(string PortName)
          {
              port1 = new SerialPort(PortName);
              port1.BaudRate = 9600;//波特率
              port1.Parity  = Parity.None;//无奇偶校验位
              port1.StopBits = StopBits.Two;//两个停止位
              port1.Handshake = Handshake.RequestToSend;//控制协议
              port1.ReceivedBytesThreshold = 4;//设置 DataReceived 事件发生前内部输入缓冲区中的字节数
              port1.DataReceived += new SerialDataReceivedEventHandler(port1_DataReceived);//DataReceived事件委托
          }
  
          //DataReceived事件委托方法
          private void port1_DataReceived(object sender, SerialDataReceivedEventArgs e)
          {
              try
              {
                  StringBuilder currentline = new StringBuilder();
                  //循环接收数据
                  while (port1.BytesToRead > 0)
                  {
                     char ch = (char)port1.ReadByte();
                      currentline.Append(ch);
                  }
                  //在这里对接收到的数据进行处理
                  //
                  currentline = new StringBuilder();
              }
              catch(Exception ex)
              {
                  Console.WriteLine(ex.Message.ToString());
              }
  
          }
  
          //打开串口的方法
          public void OpenPort()
          {
              try
              {
                  port1.Open();
              }
              catch { }
              if (port1.IsOpen)
              {
                 Console.WriteLine("the port is opened!");
             }
             else
             {
                 Console.WriteLine("failure to open the port!");
             }
         }
 
         //关闭串口的方法
         public void ClosePort()
         {
             port1.Close();
             if (!port1.IsOpen)
             {
                 Console.WriteLine("the port is already closed!");
             }
         }
 
         //向串口发送数据
         public void SendCommand(string CommandString)
         {
             byte[] WriteBuffer = Encoding.ASCII.GetBytes(CommandString);
             port1.Write(WriteBuffer, 0, WriteBuffer.Length);
         }
         
         //调用实例
         private void btnOpen_Click(object sender, RoutedEventArgs e)
         {
             //我现在用的COM1端口,按需要可改成COM2,COM3
             InitCOM("COM1");
             OpenPort();
         }
     }
 }  

        值得注意的是:

1. port1.ReceivedBytesThreshold = 4; ReceivedBytesThreshold属性设置触发一次DataReceived事件时将接收到的数据字节数.由于我的硬件是一次发上来4个字节估设置为4.如果不能正确设置这个属性的话,在SerialPort对象第一次触发DataReceived事件时还是正确的(4个字节),但是从第二次触发之后都是一个字节触发一次DataReceived事件...为什么这样搞不清楚...

2.如果在 DataReceived 委托事件中使用了不是DataReceived委托事件所在线程创建的UI控件,函数等,需要使用到Dispatcher 类来达到线程安全,不然会报错.以下是MSDN中Dispatcher类的例子(XAML),简单明了:

private delegate void AddTextDelegate(Panel p, String text);
 
 private void AddText(Panel p, String text)
 {
     p.Children.Clear();
     p.Children.Add(new TextBlock { Text = text });
 }
 
 private void TestBeginInvokeWithParameters(Panel p)
 {
     if (p.Dispatcher.CheckAccess()) AddText(p, "Added directly.");
     else p.Dispatcher.BeginInvoke(
         new AddTextDelegate(AddText), p, "Added by Dispatcher.");
 }

        还有一个比较简单的方法。

  //create a serialport object,with COM1,baudrate 57600,parity bit:none,data bit:8
            //stopbits:one
SerialPort sport = new SerialPort("COM1",57600,Parity.None,8,StopBits.One);      
 private void seriallistenfun()
        {
 try
            {
                    
                    if (sport.IsOpen)
                    {
                        sport.Close();
                        sport.Open(); //open com
                    }
                    else
                    {
                        sport.Open();//open com
                    }

                    String data;
                    string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //得到当前路径
                    path = path+@"/newfile.txt";
                    FileStream TextFile = File.Open(path, FileMode.Create, FileAccess.Write,FileShare.Read);//创建文件
                   byte [] Info ;
                   
                    
                    while (true)
                    {


                        if (sport.BytesToRead != 0)
                        {
                            data = sport.ReadExisting().ToString();//读取串口数据
                            this.BeginInvoke(dfun, new object[] { data });
                            data += "/r/n";
                            Info = new UTF8Encoding(true).GetBytes(data);//转换成字节流
                            
                            TextFile.Write(Info,0,Info.Length);//写入文件
                            Thread.Sleep(10);
                        }
                        else
                        {
                            //this.BeginInvoke(dfun, new object[] { "Serialrev:NULL" });
                            Thread.Sleep(50);
                        }
                    }
            }
            catch(SystemException e)
            {
                this.BeginInvoke(dfun, new object[] { e.ToString() });
            }
            






  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值