zoukankan      html  css  js  c++  java
  • C#串口SerialPort常用属性方法

    SerialPort():

    //属性

    .BaudRate;获取或设置波特率

    .BytesToRead;得到 接收到数据的字节数

    .BytesToWrites;得到送往串口的字节数

    .DataBits;获取或设置数据位

    .IsOpen;获取一个值,判断串口是否打开

    .Pariy;获取或设置校验位

    .PortName;串口名称

    .ReadBufferSize;获取或设置读取数据的缓存大小

    .ReadTimeout;读取超时时间

    .StopBits;停止位

    .NewLine;用于解释通过ReadLine()与WriteLine()的值

    .WriteBufferSize;与ReadBufferSize相对

    .WriteTimeout;与ReadTimeout相对

    方法:

    .Close();判断串口

    .Open();打开串口

    .Read(Byte[], int32, int32);读取数据

    .Read(Char[], int32, int32);

    .ReadByte();读取一个字节的数据

    .ReadChar();读取一个字符的数据

    .ReadLine();一直读取到输入缓冲区的NewLine值:返回string类型

    .ReadExisting();读取可用的字节

    .ReadTo(string value);读取数据,直到读到该value时停止。

    .Write(string);写入数据

    .Write(byte[], int32, int32);

    .Write(char[], int32, int32);

    .WriteLine(string value);将指定的value值与NewLine值一起写入

    事件:

    DataReceived

    例子MSDN:

    SerialPort mySerialPort = new SerialPort("COM2");

    mySerialPort.BaudRate = 9600;

    mySerialPort.Parity=Parity.None;

    mySerialPort.StopBits = StopBits.One;

    mySerialPort.DataBits = 8;

    mySerialPort.Handshake = Handshake.Non;

    mySerialPort.DataReceived += new SerialDataReceivedEvenHandler(DataReceive_Method);

    mySerialPort.Open();

    ....

    mySerialPort.Close();

    static void DataReceive_Method(object sender, SerialPortDataReceivedEventArgs e)

    {

    SerialPort sp = (SerialPort)seder;

    string indata = sp.ReadExisting();

    Console.Write(indata);

    }

  • 相关阅读:
    ajax上传文件
    nginx location指令详解
    总结php删除html标签和标签内的内容的方法
    useBuiltIns: 'usage'
    uni-app如何页面传参数的几种方法总结
    基于 schema 的数据校验
    canvas时点击事件和长按冲突
    vue 下载文件流,后台是get方式 ,并且导出出现excel乱码问题
    uni-app canvas 实现文字居中
    git reflog 回退
  • 原文地址:https://www.cnblogs.com/janeaiai/p/4899876.html
Copyright © 2011-2022 走看看