博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# winform读取及发送串口信号
阅读量:6159 次
发布时间:2019-06-21

本文共 14357 字,大约阅读时间需要 47 分钟。

请参考C#的API文档:

 

通过串口读写数据,非常简单,只需要将控制拉到窗口位置即可,就可以在窗口的下方查看到串口控件的图标,点击图标,按下F4即可查看该串品的详细属性

也可以通过在代码中创建一个串口类,并在代码中直接操作串口收发数据

其中包括比较基础的端口名称、波特率、数据位、停止位、检验位等五个参数

也有只需要指定端口名称即可,其他参数保持默认

 

知识点:向串口发送数据并获取数据
只需要将serialPort加入至窗口中,并且指定其中的端口名及波特率,指定串口数据回应事件即可(DataReceived事件) 然后在窗口加载事件中,将serialPort打开即可1,直接使用serialPort.writeLine方法即可将数据发送至端口2,已为serialPort指定了数据回应事件,当有数据从串口中返回,事件即会响应

 

 


 

示例项目

系统启动三个后台线程,

一个线程对应到更新当前时间

一个线程定时发送AA1086至串口7,并从串口7获得返回的数据

一个线程定时获取串口1的数据(修改为不需要后台线程)

 

示例代码

两个基础类,线程及定时,存放在Common文件夹中

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;namespace MES_MonitoringClient.Common{    ///     /// 多线程任务    ///     public class ThreadHandler    {        //        Thread _TThread = null;        //默认后台运行        private const bool default_isBackground = true;        //默认不自动运行        private const bool default_autoRun = false;        public ThreadHandler(System.Threading.ThreadStart startFunction) : this(startFunction, default_isBackground, default_autoRun)        { }        public ThreadHandler(System.Threading.ThreadStart startFunction, bool isBackground) : this(startFunction, isBackground, default_autoRun)        { }                ///         /// 创建线程        ///         /// 运行方法        /// 是否后台运行        /// 是否自动运行        public ThreadHandler(System.Threading.ThreadStart startFunction, bool isBackground, bool autoRun)        {            _TThread = new Thread(startFunction);            //是否后台运行            _TThread.IsBackground = isBackground;            //自动运行线程            if (autoRun)            {                _TThread.Start();            }        }        ///         /// 开始线程        ///         public void ThreadStart()        {            _TThread.Start();        }        ///         /// 查看线程执行状态        ///         /// 
public bool ThreadIsAlive() { return _TThread.IsAlive; } /// /// 查看线程 /// 前后台执行 /// ///
public string ThreadState() { return _TThread.ThreadState.ToString(); } /// /// 当前线程唯一托管标识符 /// ///
public int ThreadManagedThreadId() { return _TThread.ManagedThreadId; } /// /// 等待线程结束 /// public void ThreadJoin() { _TThread.Join(); } }}
ThreadHandler
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace MES_MonitoringClient.Common{    ///     /// 定时器帮助类    /// 创建定时器=》开始执行任务    ///     public class TimmerHandler    {        //定时器        System.Timers.Timer _TTimer = null;        //定时器默认执行一次        private const bool default_autoReset = false;        //定时器默认一秒执行一次        private const int default_interval = 1000;        //定时器创建后即刻运行        private const bool default_autoRun = false;        public TimmerHandler(System.Timers.ElapsedEventHandler elapseEvent) : this(default_interval, elapseEvent, default_autoRun)        { }        public TimmerHandler(System.Timers.ElapsedEventHandler elapseEvent, bool autoRun) : this(default_interval, elapseEvent, autoRun)        { }        public TimmerHandler(int interval, System.Timers.ElapsedEventHandler elapseEvent, bool autoRun) : this(interval, default_autoReset, elapseEvent, autoRun)        { }        ///         /// 创建定时器        ///         /// 间隔时间(ms)        /// 是否重复执行        /// 定时事件        /// 是否自动运行        public TimmerHandler(int interval, bool autoReset, System.Timers.ElapsedEventHandler elapseEvent, bool autoRun)        {            _TTimer = new System.Timers.Timer();            //时间间隔                        _TTimer.Interval = interval;            //是否重复执行            _TTimer.AutoReset = autoReset;            //定时器处理事件            _TTimer.Elapsed += elapseEvent;            //定时器自动运行            if (autoRun)            {                _TTimer.Start();            }        }        ///         /// 定时器是否可用        ///         /// 
public bool GetTimmerEnable() { return _TTimer.Enabled; } /// /// 开始定时器任务 /// public void StartTimmer() { _TTimer.Start(); } /// /// 关闭定时器任务 /// public void StopTimmer() { _TTimer.Stop(); } /// /// 删除定时器 /// public void RemoveTimmer() { _TTimer.Stop(); _TTimer.Dispose(); } }}
TimmerHandler

 

最后是窗体的代码

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Threading;using System.Diagnostics;namespace MES_MonitoringClient{    public partial class frmMain : Form    {        /*---------------------------------------------------------------------------------------*/        //发送信号数量        static int sendDataCount = 0;        //接收信号数量        static int receviedDataCount = 0;        //发送信号错误数量        static int sendDataErrorCount = 0;        //接收信号错误数量        static int receviedDataErrorCount = 0;        //向串口7发送的默认信号        static string mc_DefaultSignal = "AA1086";        //必须的串口端口        static string[] mc_DefaultRequiredSerialPortName = new string[] { "COM1", "COM7" };        //三个后台线程        static Common.ThreadHandler DateTimeThreadHandler = null;        static Common.ThreadHandler SerialPort1ThreadHandler = null;        static Common.ThreadHandler SerialPort7ThreadHandler = null;        /*---------------------------------------------------------------------------------------*/        //后台线程变量        Thread timerThread = null;        //定时器变量        System.Timers.Timer TTimer;        /*主窗口方法*/        /*---------------------------------------------------------------------------------------*/        public frmMain()        {            InitializeComponent();        }        ///         /// 主窗口加载事件        ///         ///         ///         private void frmMain_Load(object sender, EventArgs e)        {            try            {                var sw = Stopwatch.StartNew();                //检测端口                //打开端口                serialPort7.Open();                serialPort1.Open();                //开始后台进程                DateTimeThreadHandler = new Common.ThreadHandler(new ThreadStart(DateTimeTimer), true, true);                //SerialPort1ThreadHandler = new Common.ThreadHandler(new ThreadStart(GetRFIDDataTimer), true, true);                SerialPort7ThreadHandler = new Common.ThreadHandler(new ThreadStart(SendDataToSerialPortTimer), true, true);                label2.Text = DateTimeThreadHandler.ThreadState();                //MessageBox.Show("初始化共使用" + sw.ElapsedMilliseconds.ToString() + "毫秒");                                sw.Stop();            }            catch (Exception ex)            {                ShowErrorMessage(ex.Message, "系统初始化");            }        }        ///         /// 主窗口关闭中事件        ///         ///         ///         private void frmMain_FormClosing(object sender, FormClosingEventArgs e)        {            DateTimeThreadHandler.ThreadJoin();            //SerialPort1ThreadHandler.ThreadJoin();            SerialPort7ThreadHandler.ThreadJoin();            if (serialPort1.IsOpen)            {                serialPort1.Close();            }            if (serialPort7.IsOpen)            {                serialPort7.Close();            }        }        /*定时器方法*/        /*---------------------------------------------------------------------------------------*/        ///         /// 显示时间定时器        ///         private void DateTimeTimer()        {                        Common.TimmerHandler TTimerClass = new Common.TimmerHandler(1000, true, (o, a) => {                SetDateTime();            }, true);        }        ///         /// 获取串口1RFID定时器        ///         private void GetRFIDDataTimer()        {            Common.TimmerHandler TTimerClass = new Common.TimmerHandler(1000, true, (o, a) => {                GetRFIDData(mc_DefaultSignal);            }, true);        }        ///         /// 发送AA1086至串口7定时器        ///         private void SendDataToSerialPortTimer()        {            Common.TimmerHandler TTimerClass = new Common.TimmerHandler(1000, true, (o, a) => {                SendDataToSerialPort(mc_DefaultSignal);            }, true);        }        /*定时器委托*/        /*---------------------------------------------------------------------------------------*/        ///         /// 声明显示当前时间委托        ///         private delegate void SetDateTimeDelegate();        private void SetDateTime()        {            if (this.InvokeRequired)            {                try                {                    this.Invoke(new SetDateTimeDelegate(SetDateTime));                }                catch (Exception ex)                {                    //响铃并显示异常给用户                    System.Media.SystemSounds.Beep.Play();                }            }            else            {                try                {                    label1.Text = string.Format("当前时间:" + DateTime.Now);                }                catch (Exception ex)                {                    //响铃并显示异常给用户                    System.Media.SystemSounds.Beep.Play();                }            }        }        ///         /// 声明发送数据至串口委托        ///         ///         private delegate void SendDataToSerialPortDelegate(string defaultSignal);        private void SendDataToSerialPort(string defaultSignal)        {            if (this.InvokeRequired)            {                SendDataToSerialPortDelegate sendDataToSerialPortDelegate = SendDataToSerialPort;                try                {                    this.Invoke(sendDataToSerialPortDelegate, new object[] { defaultSignal });                }                catch (Exception ex)                {                    //响铃并显示异常给用户                    System.Media.SystemSounds.Beep.Play();                }            }            else            {                try                {                    if (serialPort7.IsOpen)                    {                        serialPort7.WriteLine(defaultSignal);                        sendDataCount += 1;                        //lab_SendDataCount.Text = "发送成功:" + sendDataCount;                    }                }                catch (Exception ex)                {                    sendDataErrorCount += 1;                    //lab_SendDataErrorCount.Text = "发送错误:" + sendDataErrorCount;                    //响铃并显示异常给用户                    System.Media.SystemSounds.Beep.Play();                }            }        }        ///         /// 声明获取RFID串口委托        ///         ///         private delegate void GetRFIDDataDelegate(string defaultSignal);        private void GetRFIDData(string defaultSignal)        {            if (this.InvokeRequired)            {                GetRFIDDataDelegate getRFIDDataDelegate = GetRFIDData;                try                {                    this.Invoke(getRFIDDataDelegate, new object[] { defaultSignal });                }                catch (Exception ex)                {                    //响铃并显示异常给用户                    System.Media.SystemSounds.Beep.Play();                }            }            else            {                try                {                    if (serialPort7.IsOpen)                    {                        serialPort7.WriteLine(defaultSignal);                        sendDataCount += 1;                        //lab_SendDataCount.Text = "发送成功:" + sendDataCount;                    }                }                catch (Exception ex)                {                    sendDataErrorCount += 1;                    //lab_SendDataErrorCount.Text = "发送错误:" + sendDataErrorCount;                    //响铃并显示异常给用户                    System.Media.SystemSounds.Beep.Play();                }            }        }        /*窗口公共方法*/        /*---------------------------------------------------------------------------------------*/        ///         /// 显示系统错误信息        ///         /// 错误标题        /// 错误        private void ShowErrorMessage(string errorTitle,string errorMessage)        {            MessageBox.Show(errorMessage, errorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);        }        /*获取串口数据事件*/        /*---------------------------------------------------------------------------------------*/        ///         /// 串口7获取数据        ///         ///         ///         private void serialPort7_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)        {            try            {                System.IO.Ports.SerialPort COM = (System.IO.Ports.SerialPort)sender;                //因为要访问UI资源,所以需要使用invoke方式同步ui                this.Invoke((EventHandler)(delegate                {                    receviedDataCount += 1;                    //lab_ReceviedDataCount.Text = "接收成功:" + receviedDataCount;                    richTextBox1.AppendText(COM.ReadLine() + "\r");                }                   )                );            }            catch (Exception ex)            {                //响铃并显示异常给用户                System.Media.SystemSounds.Beep.Play();            }        }               ///         /// 串口1获取数据        ///         ///         ///         private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)        {            try            {                System.IO.Ports.SerialPort COM = (System.IO.Ports.SerialPort)sender;                //因为要访问UI资源,所以需要使用invoke方式同步ui                this.Invoke((EventHandler)(delegate                {                    richTextBox2.AppendText(COM.ReadLine() + "\r");                }                   )                );            }            catch (Exception ex)            {                ShowErrorMessage("serialPort1_DataReceived", "RFID串口获取数据时出错");                //响铃并显示异常给用户                System.Media.SystemSounds.Beep.Play();            }        }        private void serialPort1_ErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)        {            ShowErrorMessage("serialPort1_ErrorReceived", "RFID串口获取数据出错");        }    }}
frmMain

 

 

 

 

 

 

转载于:https://www.cnblogs.com/weschen/p/10120277.html

你可能感兴趣的文章
BZOJ3239Discrete Logging——BSGS
查看>>
SpringMVC权限管理
查看>>
spring 整合 redis 配置
查看>>
cacti分组发飞信模块开发
查看>>
浅析LUA中游戏脚本语言之魔兽世界
查看>>
飞翔的秘密
查看>>
Red Hat 安装源包出错 Package xxx.rpm is not signed
查看>>
编译安装mysql-5.6.16.tar.gz
查看>>
活在当下
查看>>
每天进步一点----- MediaPlayer
查看>>
PowerDesigner中CDM和PDM如何定义外键关系
查看>>
跨域-学习笔记
查看>>
the assignment of reading paper
查看>>
android apk 逆向中常用工具一览
查看>>
MyEclipse 报错 Errors running builder 'JavaScript Validator' on project......
查看>>
Skip List——跳表,一个高效的索引技术
查看>>
Yii2单元测试初探
查看>>
五、字典
查看>>
前端js之JavaScript
查看>>
Log4J日志配置详解
查看>>