C#串口收发数据,子线程更新textbox控件内容,一段时间后,界面卡死,请教问题原因所在

jcyhcs jcyhcs 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
1、现象描述:
我自己用C#做了一个模拟流量计软件,下位机大概每100ms会发送一些数据(8字节),我的软件接收后,响应一个9字节的数据。数据的接收和发送都在richtextbox中显示出来。我开了一个子线程,这个线程每100ms去更新textbox控件内容,(就是先读取textbox的内容,在此基础上递增一个常量,再显示在textbox中),程序能够正常运行一段时间,然后就出现了界面卡死的现象,串口能够正常收发,richtextbox中的日志数据能够正常打印,但就是主界面死掉了,点什么都没反应,请教问题出现在什么地方。

为了方便大牛查看问题,现在将我的源程序贴在下面,请指点,谢谢!

关键程序如下:


public void JQHandler()
{
#if true
while (true)
{
//当前气量增加,流速/10
if (chkJQStart.Checked)
{
if (txtCurVol.InvokeRequired)
{
txtCurVol.BeginInvoke(new MethodInvoker(delegate()
{
fCurVol = float.Parse(txtCurVol.Text) + float.Parse(txtFlowRate.Text) / 10.0f;
txtCurVol.Text = fCurVol.ToString("#0.0000");
}));
}
//Thread.Sleep(100);
Thread.CurrentThread.Join(100);
}
}
#endif
}

private void frmFlowMeter_Load(object sender, EventArgs e)
{
//创建一个新的线程
threadTimer = new Thread(JQHandler);
threadTimer.IsBackground = true;
threadTimer.Start();
comport.DataReceived += new SerialDataReceivedEventHandler(ComDataRecv);//串口接到数据会激发这个事件
}

public void ComDataRecv(object sender, SerialDataReceivedEventArgs e)
{

int bytes = comport.BytesToRead;
byte[] buf = new byte[bytes];
comport.Read(buf, 0, bytes);
//1.缓存数据
buffer.AddRange(buf);
//2.完整性判断
while (buffer.Count >= 8)
{
if (buffer[0] > 2) { buffer.RemoveRange(0, buffer.Count); break; } //枪号不对,清除缓冲,重新开始
if (buffer[1] != 3 && buffer[1] != 5) { buffer.RemoveRange(0, buffer.Count); break; } //命令字不是3和5,清除缓冲,重新开始
byte[] ReceiveBytes = new byte[8];
//得到完整的数据,复制到ReceiveBytes中进行校验
buffer.CopyTo(0, ReceiveBytes, 0, 8);
Log(LogMsgType.Incoming, ByteArrayToHexString(ReceiveBytes) + "\n");
cp_ModBusDeed(ReceiveBytes);
buffer.RemoveRange(0, buffer.Count);

}
}
public void cp_ModBusDeed(byte[] RecvBuf)
{
//判断CRC是否正确
ushort usCRC16;
byte CRC16H, CRC16L;
byte[] SendBuf = new byte[9];

usCRC16 = CRC16(0xff, 0xff, RecvBuf, 6);
CRC16H = (byte)(usCRC16 >> 8); CRC16L = (byte)(usCRC16 & 0xff);
if ((RecvBuf[6] != CRC16L) || RecvBuf[7] != CRC16H) return;
if(RecvBuf[1] == 0x03) //读寄存器
{
String strAddr = BitConverter.ToString(RecvBuf,2,2);
strAddr = strAddr.Replace("-", "");
switch(strAddr)
{
case "00F4":
cp_SendComm(SendBuf, "0.0000", true);
break;
case "00F6": //读流速
cp_SendComm(SendBuf, txtFlowRate.Text,true);
break;
case "0102":
cp_SendComm(SendBuf, txtCurVol.Text, true);
break;


}

}
else if (RecvBuf[1] == 0x05) //写寄存器
{
String strAddr = BitConverter.ToString(RecvBuf, 2, 2);
strAddr = strAddr.Replace("-", "");
switch (strAddr)
{
case "0037": //清本次加气量
txtCurVol.BeginInvoke(new EventHandler(delegate { txtCurVol.Text = "0.0000"; }));
//Thread.Sleep(30);
cp_SendComm(RecvBuf,"",false);
break;

}
}



}
public void cp_SendComm(byte[] SendBuf, String strData, bool CountCRCF)
{
float TemSingle;
ushort usCRC16;
if (CountCRCF)
{
SendBuf[0] = 1;
SendBuf[1] = 3;
SendBuf[2] = 4;
TemSingle = float.Parse(strData);
unsafe
{
byte* p = (byte*)&TemSingle;
for (int i = 0; i < 4; i++) SendBuf[i + 3] = p[3 - i];
}
//填写CRC
usCRC16 = CRC16(0xff, 0xff, SendBuf, 7);
SendBuf[7] = (byte)(usCRC16 & 0xff);
SendBuf[8] = (byte)(usCRC16 >> 8);
}
//Thread.Sleep(50);
if(comport.IsOpen)
{
comport.Write(SendBuf, 0, SendBuf.Length);
Log(LogMsgType.Outgoing, ByteArrayToHexString(SendBuf) + "\n");
}


}
private void Log(LogMsgType msgtype, string msg)
{
rtfTerminal.BeginInvoke(new EventHandler(delegate
{
rtfTerminal.SelectedText = string.Empty;//表示空字符串。此字段为只读
rtfTerminal.SelectionFont = new Font(rtfTerminal.SelectionFont, FontStyle.Regular);
rtfTerminal.SelectionColor = LogMsgTypeColor[(int)msgtype];
if (msgtype == LogMsgType.Incoming)
rtfTerminal.AppendText("Recv:[" + DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond + "] ");
else if (msgtype == LogMsgType.Outgoing)
rtfTerminal.AppendText("Send:[" + DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond + "] ");
else
rtfTerminal.AppendText("[" + DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond + "] ");
rtfTerminal.AppendText(msg);
rtfTerminal.ScrollToCaret();
}));
}



...全文

相关推荐

WinForm窗体调用WCF服务窗体卡死问题

在Winform窗体中调用部署在其他服务器中的WCF服务时,由于调用服务需要一定时延,因此窗体在这段时间一直卡住不能进行其他操作

weixin_38663193

GUI_Gruppe1_CTE:对于GUI上载

GUI_Gruppe1_CTE:对于GUI上载

九九长安

Loading-master.zip

winfrom 执行较长的函数时等待期展示loading画面。。详见代码

香煎三文鱼

防止程序假死

当程序中有很多循环,或者运行慢时,界面往往会出现假死不东的现象,这个文档可能会有所帮助

python进击者

科研论文写作+LaTeX公式排版+数学公式与矩阵/多行对齐/编号引用实战案例+毕业论文与期刊投稿速查手册

LaTeX公式排版技巧与实战案例 【解决什么问题】 公式写对了却"看起来不对":函数名排成斜体、长公式冲出右边距、等号错开半行、编号跳号、括号过大、行距被分式撑开——编译不报错,却一眼被导师和审稿人看出来。本资源逐个给出可复制的正解。 【内容结构】 1)手册 50 页(PDF + 可编辑 DOCX):第 1—3 章讲 4 条硬指标、引擎选择、导言区骨架、公式入口与字体语义;第 4—12 章拆角标与微分、分式根式、求和积分、定界符尺寸、矩阵、编号与交叉引用、断行决策树、间距微调、定理体系;第 13 章是十大高频报错对照表(报错原文→根因→修复);第 15 章为投稿前 20 条自检清单;附录含完整模板、符号速查表、宏包清单。 2)12 个独立可编译案例(examples/):求根公式、分段函数、矩阵与方程组、多重积分与雅可比、泰勒展开、条件概率与 MLE、损失函数多行对齐、带约束优化、断行三写法对比、化学式与物理单位、算法伪代码公式、定理—引理—证明闭环,手册第 14 章逐例解读。 3)模板与速查单页(template/):改导言区即可写论文;A4 双栏符号表可打印。 【适用人群】毕业论文、期刊投稿、技术文档作者;会写 \documentclass 即可上手。 【环境】TeX Live 2023+ / MiKTeX 23+,XeLaTeX 编译;amsmath、mathtools、bm、siunitx、amsthm、mhchem、cleveref。 【用法】先跑通第 2 章环境 → 编译 01 号案例确认链路 → 替换模板导言区 → 写作时查附录 B → 交稿前过第 15 章清单。 示例代码可自由用于个人学习与论文写作。

kyyyyyy_
评论 8
收藏 已收藏
分享
返回顶部
jcyhcs
C# 111,129 成员 642,540 内容