本文是对于串口通讯,普通串口RS232串口通讯分享。
通常电脑可以使用USB转TTL模块进行测试,只需两个USB转TTL模块,将两个模块的TX(输出端)和RX(输入端)交叉接入,在电脑上打开两个串口,就可以互相测试了。此部分如有不懂后期会出详细介绍。
头文件引用
using System.IO.Ports;
主要代码:
此代码为个人封装自用的串口类,可互相借鉴学习交流,不完善地方还有很多,主要用于学习目的。
using System;
using System.Collections.Generic;
using System.Data;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DataCollectionSystem_attach
{
class Port
{
//信息委托
public delegate void TcpCommunicationData(string value);
public event TcpCommunicationData CommunicationTexts;
public delegate void TcpCommunicationData_buff(byte[] value);
public event TcpCommunicationData_buff CommunicationTexts_buff;
public SerialPort serialPort = new SerialPort();
/// <summary>
/// 自动获取串口的端口号
/// </summary>
/// <returns></returns>
public DataTable GetComList(DataTable Port_DataTable)
{
Port_DataTable.Columns.Add("PortName", typeof(string));
string[] names = SerialPort.GetPortNames();
Port_DataTable.Clear();
for (int i = 0; i < names.Length; i++)
{
Da