C#SerialPort实现串口控制继电器
最近做了一个小系统,麻雀虽小五脏俱全呀,用到各种线程控制,串口控制等技术。其中串口控制最麻烦,因为继电器的响应很快,根据不同的转接口,返回的数据质量是不一样的,所以不能直接wirte,然后马上read,这样经常得到的效应状态是错误的。因此需要用到backgroundworker不停地read,校验数据成功后再设置成功状态标志,最后让timer定时获取改标志,成功后立即控制程序界面上的按钮等控件。
public void openSerial()
{
try
{
foreach (GlobaConfig config in GlobaConfigInstance.Instance)
{
if ((!config.SerialPort.IsOpen))
{
config.SerialPort.Open();
config.SerialPort.DiscardInBuffer();
config.SerialPort.DiscardOutBuffer();
}
}
}
catch(Exception ex)
{
//MessageBox.Show("打开失败!" + ex.Message);
NotificationManager.Show(this, "打开失败!" + ex.Message,
Color.Gold, );
return;
}
} private void SendOpenDemand(byte port)
{
byte[] buffer = new byte[];
byte[] buffer2 = new byte[];
buffer[] = GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].Addr;
buffer[] = ;
buffer[] = ;
buffer[] = port;
buffer[] = 0xff;
buffer[] = ;
A101.GetCRC(buffer, , buffer2);
buffer[] = buffer2[];
buffer[] = buffer2[];
if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen)
{
this.SerialSendFrame(buffer, , );
}
else
{
//MessageBox.Show("亲,你要先打开串口哦!");
}
} private void SendCloseDemand(byte port)
{
byte[] buffer = new byte[];
byte[] buffer2 = new byte[];
buffer[] = GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].Addr;
buffer[] = ;
buffer[] = ;
buffer[] = port;
buffer[] = ;
buffer[] = ;
A101.GetCRC(buffer, , buffer2);
buffer[] = buffer2[];
buffer[] = buffer2[];
if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen)
{
this.SerialSendFrame(buffer, , );
}
else
{
//MessageBox.Show("亲,你要先打开串口哦!");
}
} private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
byte[] buffer;
while (true)
{
try
{ if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen)
{
int bytesToRead = GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.BytesToRead;
buffer = new byte[0xff];
if (bytesToRead > )
{
int num2;
bytesToRead = 0x45;
GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.Read(buffer, , bytesToRead);
bool isValid = A101.ValidCRC(buffer, (byte)bytesToRead);
System.Diagnostics.Debug.WriteLine("isValid: " + isValid);
if (isValid)
{
for (num2 = ; num2 < bytesToRead; num2++)
{
this.SerialRecBuf[num2] = buffer[num2];
} this.SerialRecFlag = true;
}
else
this.SerialRecFlag = false; }
}
}
catch (Exception ex)
{ Console.WriteLine(ex);
}
Thread.Sleep();
}
} private void timer1_Tick(object sender, EventArgs e)
{
if (this.SerialRecFlag)
{
this.SerialRecFlag = false;
if (((this.SerialRecBuf[] == GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].Addr) && (this.SerialRecBuf[] == )) && (this.SerialRecBuf[] == ))
{
this.BtnSendFlag = false;
if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen)
{
Control[] controls = this.kryptonNavigator1.SelectedPage.Controls.Find("btnRelay" + kryptonNavigator1.SelectedIndex + this.SerialRecBuf[], true);
if (controls.Length > )
{
KryptonCheckButton button = controls[] as KryptonCheckButton;
button.Checked = false;
button.Tag = "断开";
button.Enabled = true;
}
}
}
else if (((this.SerialRecBuf[] == GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].Addr) && (this.SerialRecBuf[] == )) && (this.SerialRecBuf[] == 0xff))
{
this.BtnSendFlag = false; if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen)
{
Control[] controls = this.kryptonNavigator1.SelectedPage.Controls.Find("btnRelay" + kryptonNavigator1.SelectedIndex + this.SerialRecBuf[], true);
if (controls.Length > )
{
KryptonCheckButton button = controls[] as KryptonCheckButton;
button.Checked = true;
button.Tag = "闭合";
button.Enabled = true;
}
}
}
else if (((this.SerialRecBuf[] == GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].Addr) && (this.SerialRecBuf[] == )) && (this.SerialRecBuf[] == ))
{
uint[] numArray = new uint[];
uint resource = ;
numArray[] = this.SerialRecBuf[];
numArray[] = this.SerialRecBuf[];
numArray[] = this.SerialRecBuf[];
numArray[] = this.SerialRecBuf[];
resource = (((numArray[] << 0x18) | (numArray[] << 0x10)) | (numArray[] << )) | numArray[]; if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen)
{
foreach (Control control in this.kryptonNavigator1.SelectedPage.Controls[].Controls)
{
KryptonCheckButton button = control as KryptonCheckButton;
if (this.GetRegBit(resource, (byte)control.TabIndex) == )
{
button.Checked = false;
button.Tag = "断开";
}
else
{
button.Checked = true;
button.Tag = "闭合";
}
button.Enabled = true;
}
}
} }
} private void timer2_Tick(object sender, EventArgs e)
{
if (!this.BtnSendFlag)
{
byte[] buffer = new byte[];
byte[] buffer2 = new byte[];
buffer[] = GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].Addr;
buffer[] = ;
buffer[] = ;
buffer[] = ;
buffer[] = ;
buffer[] = 0x1c;
A101.GetCRC(buffer, , buffer2);
buffer[] = buffer2[];
buffer[] = buffer2[];
System.Diagnostics.Debug.WriteLine(kryptonNavigator1.SelectedIndex + ":" + GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen);
if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen)
{
this.SerialSendFrame(buffer, , );
this.timer1.Start();
}
else
{
if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].IsValid == true)
{
GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].IsValid = false;
try
{
GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.Open();
//修復退出的線程
//this.timer1 = new System.Windows.Forms.Timer();
this.timer1.Start();
}
catch (Exception ex)
{
NotificationManager.Show(this, "主机" + (kryptonNavigator1.SelectedIndex + ) + "出现异常:" + ex.Message,
Color.Gold, );
//MessageBox.Show("主机" + (kryptonNavigator1.SelectedIndex + 1) + "出现异常:" + ex.Message);
}
}
}
}
}
C#SerialPort实现串口控制继电器的更多相关文章
- C# SerialPort自定义串口DCB
C# SerialPort自定义串口DCBChange DCB fields from SerialPort instance CPS:中文DCB结构详解表 译自Change DCB fields f ...
- C#用SerialPort实现串口通讯
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Nodejs 使用 SerialPort 调用串口
工作经常使用串口读写数据,electron 想要替代原来的客户端,串口成了必须要突破的障碍. get --> https://github.com/EmergingTechnologyAdvi ...
- C#SerialPort如何读取串口数据并显示在TextBox上
SerialPort中串口数据的读取与写入有较大的不同.由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取.一.线程实时读串口:二.事件触发方式实现. 由于线程实时读串口的效率不是十分高 ...
- c#实现串口操作 SerialPort
命名空间:using System.IO.Ports;该类提供了同步 I/O 和事件驱动的 I/O.对管脚和中断状态的访问以及对串行驱动程序属性的访问. 操作类声明: SerialPort sp = ...
- System.IO.Ports.SerialPort串口通信接收完整数据
C#中使用System.IO.Ports.SerialPort进行串口通信网上资料也很多,但都没有提及一些细节: 比如 串口有时候并不会一次性把你想要的数据全部传输给你,可能会分为1次,2次,3次分别 ...
- 树莓派4B 串口通信
提前下载安装Glade图形编辑器 参考 树莓派4B安装netcore 环境部署.发布.执行操作 准备串口设备本文使用串口控制继电器设备 如图 1.发现串口 void GetSerialPort() { ...
- C#上位机制作之串口接受数据(利用接受事件)
前面设计好了界面,现在就开始写代码了,首先定义一个串口对象.. SerialPort serialport = new SerialPort();//定义串口对象 添加串口扫描函数,扫描出来所有可用串 ...
- C#串口通讯实例
本文参考<C#网络通信程序设计>(张晓明 编著) 程序界面如下图: 参数设置界面代码如下: using System; using System.Collections.Generic; ...
随机推荐
- Google MapReduce中文版
英文原文链接: Google Map Reduce 译文原文链接: Google MapReduce中文版 Google MapReduce中文版 译者: alex 摘要 MapReduce是一个编程 ...
- Eclipse工具栏太多,自定义工具栏,去掉调试
Window --> Customize Perspective... --> Tool Bar Visibility 去掉勾选debug Tip:最新版本Customize Persp ...
- 【SPOJ-QTREE3】树链剖分
http://www.spoj.com/problems/QTREE3/ 时间限制:2s 代码长度限制:50000B 内存限制:1536MB [题目描述] 给出N个点的一棵树(N-1条边 ...
- noip车站分级 拓扑排序
题目传送门 这道题呢 每次输入一段数就把1~n里面没有在这组数里面的数和他们连一波 表示这些数比他们等级低 然后就搞一搞就好了哇 #include<cstdio> #include< ...
- HDU1267 下沙的沙子有几粒? 基础DP
题目链接 题意:给定m个H和n个D(1<=n,m<=20),问这些字母构成的序列中,对于任意位置,从左开始数H的累积个数总是不比D的累计数少的排列有多少种. 题解:二维DP,画一个正方形, ...
- 【git】git提交忽略不必要的文件或文件夹
对于经常使用Git的朋友来说,.gitignore配置一定不会陌生.废话不说多了,接下来就来说说这个.gitignore的使用. 首先要强调一点,这个文件的完整文件名就是".gitignor ...
- 逐步实现python版wc命令
Python 如何处理管道输入输出 sys.stdin 等于打开了一个文件对象,所有输入的文件都会写入到标准输入文件中(键盘) sys.stdout 等于打来了一个文件对象,使用.write()把信息 ...
- apparmor开启
- django日志的设置
关于django的日志设置详细可以看下官方文档:https://yiyibooks.cn/xx/Django_1.11.6/topics/logging.html 示例: # 日志文件配置 LOGGI ...
- 《Java编程思想》笔记 第十二章 通过异常处理错误
1.异常也是对象 标准异常类都有两个构造器,一个默认,一个接受字符串. 抛异常与方法返回类型不同,但有相似效果使当前方法退出并返回,抛异常可以看作是一种不同的返回机制.(异同点不必深究) Throwa ...