Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一个莫名其妙的问题 #22

Closed
kerwinxu opened this issue Apr 5, 2020 · 5 comments
Closed

一个莫名其妙的问题 #22

kerwinxu opened this issue Apr 5, 2020 · 5 comments

Comments

@kerwinxu
Copy link

kerwinxu commented Apr 5, 2020

Describe the bug
A clear and concise description of what the bug is.
我的的WIN10系统没有这个问题,我朋友的WIN7系统出现一个莫名其妙的问题,MFC程序,COM1串口,在用writedata发送后,断点执行时也能看到说发送了多少字节,这个字节数也是正确的,问题是,硬件没有收到数据,我之后会启动一个mfc的计时器settimer,作用是休息若干时间去执行接收数据的操作,我是设置串口同步的,程序会在这个settimer处卡死,卡死期间,在任务管理器中显示unknown,更奇怪的是,解决方式其实很简单,比如用Demo中的例子打开这个串口,然后关闭这个串口,程序就可以正常了。

我很奇怪,理论上,writedata是调用winapi发送的数据吧,收到发送字节后,应该是发送了才是吧,但为什么没有发送?这个计时器为什么会被串口卡死?我调用的是MFC的实例方法,而不是winapi,理论上应该跟串口无关。请问有解决方向吗?

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. DeepIn 15.11 64bit/Win7 Ultimate 64bit]
  • Compiler: [e.g. QT 5.6.2 GCC 6.3.0/VS2013 update 5]
  • Version: [e.g. 4.0.2.200108]

Additional context
Add any other context about the problem here.

@kerwinxu
Copy link
Author

kerwinxu commented Apr 5, 2020

我好像找到原因了,在我的初始化串口中有如下代码
//先设置串口名和波特率
m_SerialPort.init(portName,this->m_SerialPortBaudRate, itas109::Parity(0), itas109::DataBits(8), itas109::StopBits(1));
//我这里设置同步吧。
m_SerialPort.setOperateMode(itas109::SynchronousOperate);//既然是同步了,就不设置消息事件了吧。
//错误信息
CString s_err;
try
{
if (m_SerialPort.open())
{
return 1;
}
else
{
s_err.Format(_T("串口打开错误:%d"), m_SerialPort.getLastError());
TRACE(s_err);
return 0;
}
}
catch (const std::exception&)
{
s_err.Format(_T("串口打开错误:%d"), m_SerialPort.getLastError());
TRACE(s_err);
return 0;
}

open返回false,然后,getLastError返回0 .
问题是为什么会造成这种情况?怎么解决?

@itas109
Copy link
Owner

itas109 commented Apr 5, 2020

open函数的getLastError返回值有问题 能调试一下定位具体问题吗

@kerwinxu
Copy link
Author

kerwinxu commented Apr 7, 2020

2个问题:
1、在我刚电脑,刚启动程序,我先init串口,然后open串口,我这里的代码都是在SerialPortWinBase.cpp中的openPort函数调试,然后在isOpened函数是返回True的,然后整个函数返回False,
2、如上是第一次,然后如下几次调试,isOpened函数返回False,但是在148行SetCommConfig出现问题了,我在191行的// set com configure error 添加了代码 int err = GetLastError(); //得到错误代码 = 87 .
第一个问题,我在后来的调试中都没有重现,我是远程别人电脑调试,我后面调试的时候跟进isOpened函数,m_handle都是一堆的0xffff,一堆的f。这应该是没问题吧。

//我的初始化串口部分,串口号是控件变量CString portName , 而波特率是控件变量 int m_SerialPortBaudRate
char CMFCApplication1Dlg::init_serialport()
{
// TODO: 在此处添加实现代码.
//这个函数只是初始化串口的
this->UpdateData(TRUE);
string portName;
#ifdef UNICODE
portName = CW2A(this->m_SerialPortName.GetString());
#else
portName = temp.GetBuffer();
#endif

//if (m_SerialPort.isOpened())
//{
//	m_SerialPort.close();
//}


//先设置串口名和波特率
m_SerialPort.init(portName,this->m_SerialPortBaudRate, itas109::Parity(0), itas109::DataBits(8), itas109::StopBits(1));
//我这里设置同步吧。
m_SerialPort.setOperateMode(itas109::SynchronousOperate);//既然是同步了,就不设置消息事件了吧。
//错误信息
CString s_err;
try
{
	if (m_SerialPort.open())
	{
		return 1;
	}
	else
	{
		s_err.Format(_T("串口打开错误:%d"), m_SerialPort.getLastError());
		TRACE(s_err);
		return 0;
	}
}
catch (const std::exception&)
{
	s_err.Format(_T("串口打开错误:%d"), m_SerialPort.getLastError());
	TRACE(s_err);
	return 0;
}

}

@itas109
Copy link
Owner

itas109 commented Apr 7, 2020

好的 我测试一下

@itas109
Copy link
Owner

itas109 commented Apr 8, 2020

//先设置串口名和波特率
m_SerialPort.init(portName,this->m_SerialPortBaudRate, itas109::Parity(0), itas109::DataBits(8), itas109::StopBits(1));

最后一个参数停止位应该是itas109::StopBits(0)。一般情况下停止位为1.5不会用到

enum StopBits
{
	StopOne = 0,            ///< 1 stop bit 1位停止位
	StopOneAndHalf = 1,     ///< 1.5 stop bit 1.5位停止位 - This is only for the Windows platform
	StopTwo = 2             ///< 2 stop bit 2位停止位
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants