打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[转载]C#中串口通信编程
#region Using directives

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

#endregion

namespace Serialexpample
{
    partial class Form1 : Form
    {
        //create instance of property page
        //property page is used to set values for stop bits and
        //baud rate

        PropertyPage pp = new PropertyPage();

        //create an Serial Port object
        SerialPort sp = new SerialPort();

        public Form1()
        {
            InitializeComponent();
        }
            
        private void propertyButton_Click(object sender, EventArgs e)
        {
            //show property dialog
            pp.ShowDialog();

            propertyButton.Hide();
        }

        private void sendButton_Click(object sender, EventArgs e)
        {
            try
            {
                //write line to serial port
                sp.WriteLine(textBox.Text);
                //clear the text box
                textBox.Text = "";
            }
            catch (System.Exception ex)
            {
                baudRatelLabel.Text = ex.Message;
            }

        }

        private void ReadButton_Click(object sender, EventArgs e)
        {
            try
            {
                //clear the text box
                textBox.Text = "";
                //read serial port and displayed the data in text box
                textBox.Text = sp.ReadLine();
            }
            catch(System.Exception ex)
            {
                baudRatelLabel.Text = ex.Message;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("Do u want to Close the App");
            sp.Close();
        }

        private void startCommButton_Click(object sender, EventArgs e)
        {
            startCommButton.Hide();
            sendButton.Show();
            readButton.Show();
            textBox.Show();
        }

        //when we want to save the status(value)
        private void saveStatusButton_Click_1(object sender, EventArgs e)
        {
            //display values
            //if no property is set the default values
            if (pp.bRate == "" && pp.sBits == "")
            {
                dataBitLabel.Text = "BaudRate = " + sp.BaudRate.ToString();
                readTimeOutLabel.Text = "StopBits = " + sp.StopBits.ToString();
            }
            else
            {
                dataBitLabel.Text = "BaudRate = " + pp.bRate;
                readTimeOutLabel.Text = "StopBits = " + pp.sBits;
            }

            parityLabel.Text = "DataBits = " + sp.DataBits.ToString();
            stopBitLabel.Text = "Parity = " + sp.Parity.ToString();
            readTimeOutLabel.Text = "ReadTimeout = " +
                      sp.ReadTimeout.ToString();

            if (propertyButton.Visible == true)
                propertyButton.Hide();
            saveStatusButton.Hide();
            startCommButton.Show();

            try
            {
                //open serial port
                sp.Open();
                //set read time out to 500 ms
                sp.ReadTimeout = 500;
            }
            catch (System.Exception ex)
            {
                baudRatelLabel.Text = ex.Message;
            }
        }
    }
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
转:C#中跨线程访问控件问题解决方案
<转载>数组之外另一个选择:List<T>类
如何:响应 Calendar Web 服务器控件中的日期选择
《计算机基础与程序设计》试题
V9.A10ProgressBar控件(进度条)
C#窗体——100以内加法做题程序
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服