使用VS2015用C#最简单代码实现串口通讯

使用VS2015用C#最简单代码实现串口通讯

上位机界面如图:

使用VS2015用C#最简单代码实现串口通讯_第1张图片

具体代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace Test3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//设置该属性 为false
}

    private void Form1_Load(object sender, EventArgs e)
    {
        //RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
        //if (keyCom != null)
        //{
        //    string[] sSubKeys = keyCom.GetValueNames();
        //    comPort.Items.Clear();
        //    foreach (string sName in sSubKeys)
        //    {
        //        string sValue = (string)keyCom.GetValue(sName);
        //        comPort.Items.Add(sValue);
        //    }
        //    if (comPort.Items.Count > 0)
        //         comPort.SelectedIndex = 0;
        //}
        comPort.Text = "COM1";
        comBaud.Text = "9600";
    }

    bool isOpened = false;//串口状态标志

    private void button1_Click(object sender, EventArgs e)
    {
        if (!isOpened)
        {
            serialPort1.PortName = comPort.Text;
            serialPort1.BaudRate = Convert.ToInt32(comBaud.Text, 10);
            try
            {
                serialPort1.Open();     //打开串口
                button1.Text = "关闭串口";
                comPort.Enabled = false;//关闭使能
                comBaud.Enabled = false;
                isOpened = true;
                serialPort1.DataReceived += new SerialDataReceivedEventHandler(post_DataReceived);//串口接收处理函数
            }
            catch
            {
                MessageBox.Show("串口打开失败!");
            }
        }
        else
        {
            try
            {
                serialPort1.Close();     //关闭串口
                button1.Text = "打开串口";
                comPort.Enabled = true;//打开使能
                comBaud.Enabled = true;
                isOpened = false;
            }
            catch
            {
                MessageBox.Show("串口关闭失败!");
            }
        }
    }

    private void post_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        string str = serialPort1.ReadExisting();//字符串方式读
        ReceiveTbox.Text = "";//先清除上一次的数据
        ReceiveTbox.Text += str;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //发送数据
        if (serialPort1.IsOpen)
        {//如果串口开启
            if (SendTbox.Text.Trim() != "")//如果框内不为空则
            {
                serialPort1.Write(SendTbox.Text.Trim());//写数据
            }
            else
            {
                MessageBox.Show("发送框没有数据");
            }
        }
        else
        {
            MessageBox.Show("串口未打开");
        }
    }

    private void groupBox2_Enter(object sender, EventArgs e)
    {

    }

    private void comPort_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void comBaud_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void SendTbox_TextChanged(object sender, EventArgs e)
    {

    }

    private void ReceiveTbox_TextChanged(object sender, EventArgs e)
    {

    }

}

}

实际运行效果:
使用VS2015用C#最简单代码实现串口通讯_第2张图片

你可能感兴趣的:(上位机串口通讯,c#)