zoukankan      html  css  js  c++  java
  • C# 从串口读取数据

    最近要做系统集成,需要从串口读取数据,随学习一下相关知识:

    以下是从串口读取数据

    public static void Main()
    {
        SerialPort mySerialPort = new SerialPort("COM1");
    
        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
    
        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
    
        mySerialPort.Open();
    
        Console.WriteLine("Press any key to continue...");
        Console.WriteLine();
        Console.ReadKey();
        mySerialPort.Close();
    }
    
    private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        Debug.Print("Data Received:");
        Debug.Print(indata);
    }

    一旦有数据,DataReceivedHandler将触发并输出相关数据。

    出处:http://stackoverflow.com/questions/16215741/c-sharp-read-only-serial-port-when-data-comes

  • 相关阅读:
    Python Turtle
    Python 键盘记录
    Django框架学习
    MongoDB数据库安装与连接
    Python 进程间通信
    Powershell脚本执行权限
    Python 端口,IP扫描
    Exchange超级实用命令行
    Exchange管理界面
    window7 配置node.js 和coffeescript环境
  • 原文地址:https://www.cnblogs.com/zhangzhi19861216/p/4182338.html
Copyright © 2011-2022 走看看