//检查连接线RXD跟TXD是否短路
uint32 Device_HasShortBetweenTxdRxd(uint8* acCheckSelfQueryPacket, uint32 nPacketLen)
{
//自身连接线检查
uint8 acBuffer[4];
uint32 cAnswerReceived = 0;
uint32 nCount;
uint32 nCheckTimes = 3;
while(nCheckTimes > 0)
{
nCheckTimes--;
OS_ENTER_CRITICAL();
nCount = 1;
while(nCount > 0)
{
nCount = UartEx_Receive(UART_DEVICE, acBuffer, 1);
}
UartEx_Send(UART_DEVICE, acCheckSelfQueryPacket, nPacketLen);
OS_EXIT_CRITICAL();
OSTimeDly(OS_TICKS_PER_SEC / 30);
OS_ENTER_CRITICAL();
nCount = UartEx_Receive(UART_DEVICE, acBuffer, nPacketLen);
OS_EXIT_CRITICAL();
if(nCount > 0)
{
if(memcmp(acCheckSelfQueryPacket, acBuffer, nPacketLen) == 0)
cAnswerReceived++;
}
else if(nCount == 0)
{
break;
}
}
return (cAnswerReceived > 0)?DEVICE_TROUBLE_HARDWARE:DEVICE_TROUBLE_NONE;
}
我来说说我对这个程序的理解和问题,首先我明白的变量是nCheckTimes = 3,这是要检测三次,acBuffer[4]是接收缓冲区,但是不明白为什么要设为一个有4个元素的数组,cAnswerReceived和nCount不明白是干什么的?尤其是为什么首先要设nCount=1,然后执行while(nCount > 0){nCount = UartEx_Receive(UART_DEVICE, acBuffer, 1);},这又是干什么呢??而且后面还要用函数memcmp比较发送缓冲区和接收缓冲区的字符数是否一样多,只有一样多才会返回DEVICE_TROUBLE_HARDWARE,如果不一样多呢不也发过去一部分吗这不是也说明RXD和TXD连着呢吗为什么返回是DEVICE_TROUBLE_NONE呢?
请高手给与指导!
uint32 Device_HasShortBetweenTxdRxd(uint8* acCheckSelfQueryPacket, uint32 nPacketLen)
{
//自身连接线检查
uint8 acBuffer[4];
uint32 cAnswerReceived = 0;
uint32 nCount;
uint32 nCheckTimes = 3;
while(nCheckTimes > 0) // 检测3次,分别为3,2,1,0时退出
{
nCheckTimes--;
OS_ENTER_CRITICAL();
nCount = 1; // 强制进入下面循环,丢弃当前串口数据
while(nCount > 0)
{
nCount = UartEx_Receive(UART_DEVICE, acBuffer, 1); // 从串口读入数据并丢弃,当无数据或超时后退出
}
UartEx_Send(UART_DEVICE, acCheckSelfQueryPacket, nPacketLen); // 向串口写入测试数据, nPacketLen 应该小于等于 4
OS_EXIT_CRITICAL();
OSTimeDly(OS_TICKS_PER_SEC / 30);
OS_ENTER_CRITICAL();
nCount = UartEx_Receive(UART_DEVICE, acBuffer, nPacketLen); // 从串口读入 nPacketLen 长度的数据
OS_EXIT_CRITICAL();
if(nCount > 0) // 从串口读入数据成功
{
if(memcmp(acCheckSelfQueryPacket, acBuffer, nPacketLen) == 0) // 比较读入数据和写入数据,如果相同则cAnserReceived递增
cAnswerReceived++;
}
else if(nCount == 0) // 串口中无数据
{
break;
}
}
return (cAnswerReceived > 0)?DEVICE_TROUBLE_HARDWARE:DEVICE_TROUBLE_NONE; // 如果读入与写入串口数据曾相同,那么Rx与Tx交叉,否则没有
}