C# 串口助手

本文介绍了一个由作者在项目实践中编写的C#串口助手,该助手具备数据发送、接收以及HEX显示等功能,并支持多种常用编码。是进行串口通信的实用工具。
摘要由CSDN通过智能技术生成

        在做项目的过程中频繁接触串口通信,积累了C#相关知识,业余时间编辑了最基本的串口助手,添加了自己所需要的功能。

主要功能:1.数据的发送、接收

                  2.HEX的发送和显示

                  3.增加常用几种编码

/*****************************************************
 * ProjectName:  SXPSerialPort
 * Author:       Sha Xiaoping
 * CreateTime:   2019/5/12
*****************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SXPSerialPort
{
    public partial class SxpSerialPort : Form
    {
        private SerialPort ComDevice = new SerialPort();

        public SxpSerialPort()
        {
            InitializeComponent();
            this.init();
        }
        public void init()
        {
            btnSend.Enabled = false;
            cbbComList.Items.AddRange(SerialPort.GetPortNames());
            if (cbbComList.Items.Count > 0)
            {
                cbbComList.SelectedIndex = 0;//默认选用第一个端口
            }
            cbbBaudRate.SelectedIndex = 5; //选用波特率9600
            cbbDataBits.SelectedIndex = 0;//默认选用数据位8位 
            cbbStopBits.SelectedIndex = 0;//默认选用停止位1位
            cbbParity.SelectedIndex = 0;//默认选用校验位None
            pictureBox1.BackgroundImage = Properties.Resources.red;
            ComDevice.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);//绑定事件           
        }

        /// 打开串口
        private void btnOpen_Click(object sender, 
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C#串口助手程序的示例代码: ```csharp using System; using System.IO.Ports; namespace SerialPortDemo { class Program { static SerialPort _serialPort; static void Main(string[] args) { try { _serialPort = new SerialPort(); _serialPort.PortName = "COM1"; // 串口名称 _serialPort.BaudRate = 9600; // 波特率 _serialPort.DataBits = 8; // 数据位 _serialPort.Parity = Parity.None; // 校验位 _serialPort.StopBits = StopBits.One; // 停止位 _serialPort.Handshake = Handshake.None; // 握手协议 _serialPort.Open(); // 打开串口 Console.WriteLine("Serial port opened!"); while (true) { string input = Console.ReadLine(); if (input.ToLower() == "exit") break; _serialPort.WriteLine(input); // 发送数据 Console.WriteLine("Sent: " + input); string output = _serialPort.ReadLine(); // 接收数据 Console.WriteLine("Received: " + output); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (_serialPort != null && _serialPort.IsOpen) _serialPort.Close(); // 关闭串口 } } } } ``` 这个程序使用了C#的`SerialPort`类来实现串口通信。在`Main`方法中,它打开了一个名为COM1的串口,并且设置了波特率为9600,数据位为8,校验位为无,停止位为1,握手协议为无。 程序进入一个无限循环,等待用户从控制台输入数据。当用户输入数据后,程序将其发送到串口,并等待接收到响应。用户可以通过输入"exit"来退出程序。 在`try`块中,我们使用`SerialPort.WriteLine`方法将数据发送到串口,并使用`SerialPort.ReadLine`方法等待串口返回响应。在`catch`块中,如果出现异常,程序将打印错误消息。在`finally`块中,我们使用`SerialPort.Close`方法关闭串口。 请注意,在使用`SerialPort`类之前,您需要将`System.IO.Ports`命名空间添加到您的项目中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值