串口传输速率 [英] Transmission rate over the serial port

查看:79
本文介绍了串口传输速率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我一直在研究需要通过串口发送信息的多线程MFC应用程序。除了我需要每100毫秒发送一次信息之外,一切似乎都能正常工作。我一直在尝试根据滴答计数发送信息,以确定何时确切地发送以下给出的信息但到目前为止没有运气。关于我哪里出错的任何建议?任何帮助都会非常感激。



代码:



主题(...)
{
CDlg * appPtr =(CDlg *)lpVoid;

while true
{
< span class =code-keyword> if (appPtr-> m_StartSending == true
{
dInitialCount = GetTickCount ();

if (dPRCount == 0
dPRCount = GetTickCount ();

dCurrentCount = GetTickCount();
差值=(dCurrentCount - dPRCount);
if (差异> = 100
{
appPtr - > SendPR(ⅰ);
// 使用WriteFile()每隔100毫秒通过串口发送消息
< span class =code-comment> // 写文件后1秒休眠以确保收到数据
dPRCount = 0 ;
dCurrentCount = 0 ;
}
}
}





 SendPR(i) 
{
// 传输我的信息
写(.. );
// 延迟1毫秒以确保收到数据
睡眠(< span class =code-digit> 1 );
}

解决方案

请参阅我对该问题的评论。整个想法是错误的:你永远不会在代码中等待任何东西。你想尝试旋转等待吗?这总是邪恶的。使用计时器事件或单独的线程(更好);在发送周期之间休眠。但即使这可能是错的,因为我在对这个问题的评论中提到了这个问题。只有当您提供有关为什么要这样做的完整信息时,您才有机会获得更好的建议。



-SA


试图做什么?

如果您的通信确实需要准确的同步(准确度必须达到100毫秒),您将会失败( Windows OS 无法提供它)。

Hello,

I have been working on a multi-threaded MFC application that needs to send information via the serial port. Everything seems to work fine apart from the fact that I need to send the information every 100ms. I have been trying to send the information based on the tick counts to know when exactly to send the information as given below but no luck so far. Any suggestions on where I am going wrong? Any help would be really appreciated.

Code:

Thread(...)
{
CDlg* appPtr = (CDlg*)lpVoid;

while(true)
{
	if(appPtr->m_StartSending == true)
      	{        
         dInitialCount = GetTickCount();

         if(dPRCount == 0)
            dPRCount = GetTickCount();

         dCurrentCount = GetTickCount();
         Difference = (dCurrentCount - dPRCount);
         if(Difference >= 100)
         {
            appPtr->SendPR(i);   
             //Send message via serial port every 100ms using WriteFile()
            // Sleep of 1 sec after writefile to ensure data is received
            dPRCount = 0;
            dCurrentCount = 0;
         }
}
}



SendPR(i)
{
// transmitting my information
Write(...);
// Delay of 1 millisec to ensure data is received
Sleep(1);
}

解决方案

Please see my comment to the question. The whole idea is wrong: you never wait for anything in your code. Are you trying to do a spin wait? This is always an evil. Either use a timer events or a separate thread (better); sleep between sending cycles. But even this could be wrong, due to the problem I mentioned in my comment to the question. You can get a chance for a better advice only if you provide complete information on why are you doing all that.

—SA


What are trying to do?
If your communuication really needs an accurate synchronization (how accurate must be the 100 milliseconds period), you are going to fail (Windows OS cannot provide it).


这篇关于串口传输速率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆