zoukankan      html  css  js  c++  java
  • .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 }

    --------

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

  • 相关阅读:
    chrome设置中显示“由贵单位管理”的解决办法
    使用Record Espresso test脚本录制功能
    win10启动.net framework 3.5失败的解决办法
    Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncExceptio解决办法
    Python+Selenium学习--自动化测试用例实例
    linux之awk命令(转载)
    linux之shell1
    linux之egrep命令
    linux之grep命令
    python3安装pcap遇到的问题
  • 原文地址:https://www.cnblogs.com/dusmos/p/3633266.html
Copyright © 2011-2022 走看看