Record the experiences of debugging serial port device bugs and debugging serial port bugs

Source: Internet
Author: User

Record the experiences of debugging serial port device bugs and debugging serial port bugs

It took almost one day to get a Bug. The Bug is as follows:

This Bug is especially unique. It is reported every time the end user uses the software without any prompt on the development computer. I carefully thought about the newly added features in the source code and did not find any obvious problems. Therefore, the literal meaning is "runtime error", so the idea to solve this problem at first is to copy the runtime of all applications to the application directory. This exception is still reported after the attempt. The analysis may have nothing to do with the dynamic link library at runtime. Therefore, adjust the solution to the problem and consider splitting the newly added code in the project. Some of the newly added code has some problems. Finally, an exception occurs in the function. The code for the exception function is as follows:

void CleanSerialPort(){    if(g_hEvent != NULL)    {        CloseHandle(g_hEvent);        g_hEvent = NULL;    }    if(g_SerialPort.IsOpen())    {        COMMPROP properties;        memset(&properties, 0, sizeof(properties));        g_SerialPort.GetProperties();        g_SerialPort.ClearWriteBuffer();        g_SerialPort.ClearReadBuffer();        g_SerialPort.Flush();        g_SerialPort.CancelIo();        g_SerialPort.Close();    }}

I carefully read the code several times at the beginning and did not find any exception. At this time, the problem can only be identified by debugging the compiler. The method of debugging the compiler is more traditional. It is implemented through MessageBox messages. Finally, locate the Close function of g_SerialPort. The function is prototype as follows:

void CSerialPort::Close(){    if(IsOpen())    {        CloseHandle(m_hComm);        m_hComm = INVALID_HANDLE_VALUE;    }}

I read it carefully several times and still did not find the problem there. However, after a period of time, I figured out the problem clearly. When the serial port data cable is pulled out, the serial port device does not exist in the operating system. In this case, the serial port device must be forcibly disabled. Of course, an exception occurs, it is abnormal if no exception occurs. Therefore, consider adding a try... Catch ..., Sure not, an exception is caught successfully. The value of the Exception Code is 0x05, which indicates ERROR_ACCESS_DENIED, indicating that access is denied. Currently, the serial port device does not exist in the operating system. Therefore, access denied is normal. The final code is as follows:

void CleanSerialPort(){    try    {        if(g_hEvent != NULL)        {            CloseHandle(g_hEvent);            g_hEvent = NULL;        }        if(g_SerialPort.IsOpen())        {            COMMPROP properties;            memset(&properties, 0, sizeof(properties));            g_SerialPort.GetProperties();            g_SerialPort.ClearWriteBuffer();            g_SerialPort.ClearReadBuffer();            g_SerialPort.Flush();            g_SerialPort.CancelIo();            g_SerialPort.Close();        }    }    catch(CSerialException &e)    {        ATLTRACE("Unexpected CSerialPort exception, Error:%u\n", e.m_dwError);        UNREFERENCED_PARAMETER(e);    }}

After this exception is caught, if the application is not closed, the initialization of the serial device is not affected, and the application does not report a start Runtime error, therefore, the current solution is feasible by default.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.