最近做了一个小系统,麻雀虽小五脏俱全呀,用到各种线程控制,串口控制等技术。其中串口控制最麻烦,因为继电器的响应很快,根据不同的转接口,返回的数据质量是不一样的,所以不能直接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实现串口控制继电器的更多相关文章

  1. C# SerialPort自定义串口DCB

    C# SerialPort自定义串口DCBChange DCB fields from SerialPort instance CPS:中文DCB结构详解表 译自Change DCB fields f ...

  2. C#用SerialPort实现串口通讯

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. Nodejs 使用 SerialPort 调用串口

    工作经常使用串口读写数据,electron 想要替代原来的客户端,串口成了必须要突破的障碍. get -->  https://github.com/EmergingTechnologyAdvi ...

  4. C#SerialPort如何读取串口数据并显示在TextBox上

    SerialPort中串口数据的读取与写入有较大的不同.由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取.一.线程实时读串口:二.事件触发方式实现. 由于线程实时读串口的效率不是十分高 ...

  5. c#实现串口操作 SerialPort

    命名空间:using System.IO.Ports;该类提供了同步 I/O 和事件驱动的 I/O.对管脚和中断状态的访问以及对串行驱动程序属性的访问. 操作类声明: SerialPort sp = ...

  6. System.IO.Ports.SerialPort串口通信接收完整数据

    C#中使用System.IO.Ports.SerialPort进行串口通信网上资料也很多,但都没有提及一些细节: 比如 串口有时候并不会一次性把你想要的数据全部传输给你,可能会分为1次,2次,3次分别 ...

  7. 树莓派4B 串口通信

    提前下载安装Glade图形编辑器 参考 树莓派4B安装netcore 环境部署.发布.执行操作 准备串口设备本文使用串口控制继电器设备 如图 1.发现串口 void GetSerialPort() { ...

  8. C#上位机制作之串口接受数据(利用接受事件)

    前面设计好了界面,现在就开始写代码了,首先定义一个串口对象.. SerialPort serialport = new SerialPort();//定义串口对象 添加串口扫描函数,扫描出来所有可用串 ...

  9. C#串口通讯实例

    本文参考<C#网络通信程序设计>(张晓明  编著) 程序界面如下图: 参数设置界面代码如下: using System; using System.Collections.Generic; ...

随机推荐

  1. swagger学习2

    转:http://blog.csdn.net/fansunion/article/details/51923720 写的非常好,非常详细,推荐!!!! 最常用的5个注解 @Api:修饰整个类,描述Co ...

  2. Install the AWS Command Line Interface on Linux

    Install the AWS Command Line Interface on Linux You can install the AWS Command Line Interface and i ...

  3. java JDK动态代理的机制

    一:前言 自己在稳固spring的一些特性的时候在网上看到了遮掩的一句话“利用接口的方式,spring aop将默认通过JDK的动态代理来实现代理类,不适用接口时spring aop将使用通过cgli ...

  4. php windows rename 中文出错

    php windows rename 中文出错 rename()函数可以重命名文件.目录等,但是要注意目的地和起始地址的编码. 比如:我的PHP文件编码是UTF-8,但是在WINDOW系统中中文默认编 ...

  5. 【Atcoder】AGC 016 C - +/- Rectangle

    [题意]给定大矩阵的边长H和W,给每格填数(<=|10^9|),要求大矩形总和为正数,而每个h*w的小矩形总和为负数,求构造方式. [算法]数学 [题解]结论题. ★当h|H&& ...

  6. BZOJ 100题纪念

  7. bzoj 1500 修改区间 splay

    内个我也不知道哪儿不对,TLE了,说说思路吧 其实思路也没什么说的,就是裸的splay,对于最后一个操作 我们记下每个区间的最长前缀,最长后缀,那么最长子序列就是 前缀,后缀,左子树的后缀+右子树的前 ...

  8. LCD实验学习笔记(三):WATCH DOG

    看门狗是为了能够防止程序跑飞用的.程序应该定时的去喂狗.如果程序跑飞了,那么就不会去喂狗了.如果超过了喂狗的时间,那么狗就会生成一个信号来reset CPU.一般程序不需要,特殊情况下需要这种机制. ...

  9. Django-urls路由分发

      例如:127.0.0.0:8000/blog/lucaq.html,当有多个应用时,需要在blog应用下进行路由,我们在blog应用下做一个urls路由分发,就需要include模块实现.   导 ...

  10. web前端 html基础2

    表单标签<form></form> input系列标签 text 文本输入框 placeholder 默认的属性,输入时消失 password 密码输入框 radio 单选框 ...