• .net串口通信


    背景:
      前一段时间需要写一个向蓝牙模块发消息的功能。
      对蓝牙的机制不太了解,所以一直在查资料,
      但始终没找到我需要的东西,还误以为需要配套的一套开发模板和开发包,
      

      偶然间发现只需要简单的串口通信,并且.net已经集成好相关的函数。大雾
      实际上对方已经告诉我要做串口了,一直没往那个方向查

      

      再细看参数,发现都是微机课上学的,巨简单(当初上课还以为没什么用)

    代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.IO;
     6 using System.IO.Ports;
     7 using System.Configuration;
     8 
     9 namespace MorrisSpace
    10 {
    11     class SerialPortSample
    12     {
    13         static SerialPort spConnector=null;
    14         
    15         public static void SetSerialPort()
    16         {
    17             string portName = "COM5";
    18             int baudRate = 9600;
    19             int dataBits = 8;
    20             parity = Parity.None; break;
    21             stopBits = StopBits.One; break;
    22             try
    23             {
    24                 spConnector = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
    25             }
    26             catch (IOException e)
    27             {
    28                 System.Windows.MessageBox.Show(e.ToString());
    29             }
    30         }
    31 
    32         public static void SendString(String str)
    33         {
    34             if (spConnector == null)
    35             {
    36                 SetSerialPort();
    37             }
    38             byte[] bytedata = Encoding.UTF8.GetBytes(str);
    39             try
    40             {
    41                 spConnector.Open();
    42                 spConnector.Write(bytedata, 0, bytedata.Length);
    43                 spConnector.Close();
    44             }
    45             catch (IOException e)
    46             {
    47                 System.Windows.MessageBox.Show(e.ToString());
    48             }
    49         }
    50     }
    51 }

    --------

    这里只是演示  串口参数是固定的

  • 相关阅读:
    ES的基本操作
    Tomcat常规配置
    20220606 JDK 915 新版本特性
    style="wordbreak: breakall;" 用于 对应 td 文本内容过长自适应换行适用
    Springboot项目远程dubug调试
    矩阵可逆的充要条件
    机器学习基础知识
    Linux第一章
    QT linux QT更改默认构建目录debug与release生成的目录放在项目的文件夹内部
    通讯方式通信协议通讯通信区别by txwtech
  • 原文地址:https://www.cnblogs.com/dusmos/p/3633266.html
Copyright © 2020-2023  润新知