Run-Time Check Failure #3 - The variable 'comstat' is being used without being initialized

博客介绍了串口通信编译问题的三种解决方案。一是改变项目配置属性,将基本运行时检查设为默认;二是改变comstat变量属性,将其改为static;三是添加代码memset(&comstat, 0, sizeof(COMSTAT))。还提及debug和Release编译状态下的不同情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方案1: 改变项目配置属性
一种解决方案是改变基本运行时检查(changing the runtime checks in project settings):在菜单Project->Project properties-> C/C++ -> Code generation-> Basic Runtime checks –> change to ‘Default’,在中文版中是:项目-〉属性-〉配置属性-〉C/C++代码生成-〉基本运行时检查-〉设置为默认,当将基本运行时检查改为默认之后,编译自然顺利通过了,向串口调试助手发送消息正常了,再从串口调试助手向SerialPortTest发送消息时,也正常了。

方案2:改变comstat变量属性
另一种解决方案是将CSerialPort.CPP中的COMSTAT comstat;改为static COMSTAT comstat;这样改了之后,debug顺利通过,然后调试,与串口调试助手相互发消息都OK了。为什么这样就能解决呢?其实当你使用debug解决方案时,它的基本运行时检查初始设置为:两者(/RTC1,等同于 /RTCsu),这个两者是指:堆栈帧(/RTCs),未初始化的变量(/RTCu)。由于它要检查未初始化的变量,所以将SerialPort.cpp中的COMSTAT comstat;改为static COMSTAT comstat;就可以正常使用CSerialPort类了。 
以上是在debug下的解决方案,当在Release编译状态时,你会发现并不需要将COMSTAT comstat;改为static COMSTAT comstat;就能编译成功并且发送接收消息正常。其实,在Release下,它的基本运行时检查初始设置已为默认。这应该也算是第一种解决方案之内。 
而当你将它设置为两者(/RTC1,等同于 /RTCsu)时,不论是否将COMSTAT comstat;改为static COMSTAT comstat;,编译都不能通过,报错““cl.exe”返回的结果有误。”,这个应该是属于Release的问题了,另当别论了。

方案3:
添加代码:memset(&comstat, 0, sizeof(COMSTAT)); //VC6不用这句也可以用,2008就要加

https://blog.youkuaiyun.com/humanking7/article/details/48110543转自[MFC]使用强大的第三方串口类 CSerialPort

auto start_time1 = std::chrono::steady_clock::now(); if (all_serial[index].isconnected() == -1) { log_error("Please open the come port!"); return -1; } int transmission_times = 1; memset(&parsered_message[index], 0, 128); memset(&packed_message[index], 0, 128); uint8_t head = heads[slave]; uint8_t packed_length = 0; package_protocol_message(slave, send, (uint8_t*)(packed_command+index), sendLength, &packed_length, mode); RETRY: all_serial[index].getBufferBytes(); //清除发送缓冲区和接收缓冲区 int32_t result = (int32_t)all_serial[index].sendArray(packed_command[index], packed_length);//发送相关指令 if (result != 0) { log_error("%s:Failed to send command to sensor.", send); return result; } //倘若是修改baud率,则不用接受数据 if (sendLength > 6 && (memcmp(send,"baud",4)==0)) { return 0; } //倘若是修改load,则超时 int timeout_multi = 1; if (memcmp(send,"load",4) == 0 || memcmp(send,"frmt",4) == 0) { timeout_multi = 50; } //开始接收协议头数据 _Bool timeout = true;//是否会超时 uint32_t error; COMSTAT comstat;//串口状态 uint32_t actual_length;//串口接收数据的实际字计数 auto start_time = std::chrono::steady_clock::now(); while (ELAPSED_TIME(start_time) <= TIME_OUT_MS*timeout_multi) { result = (int32_t)all_serial[index].clearcom(&error, &comstat); if (comstat.cbInQue < PACKET_EXTRA_HEAD_SIZE) { continue; } result = (int32_t)all_serial[index].getArray(receive, PACKET_EXTRA_HEAD_SIZE, &actual_length);//接收数据 if (actual_length == PACKET_EXTRA_HEAD_SIZE) { timeout = false; break; } } //超时 if (timeout) { log_error(" %s:receive header and length time out!", send); if (transmission_times <= RETRANSMISSION_TIMES) { transmission_times++; goto RETRY; } else { return -1; } } //crc校验错误 if (receive[0] == 1 && receive[1]==0) { log_error(" %s:crc error!", send); if (transmission_times < RETRANSMISSION_TIMES) { transmission_times++; goto RETRY; } else { return -1; } }这一段主要耗时操作在哪
最新发布
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值