【CC3200 评测】CC3200-LAUNCHXL开发板使用心得分享+温度传感器测温+UDP_Socket传输

基于UDP_Socket的代码,增加温度传感器测温,并转换成摄氏温度,然后通过UDP_Socket ,发送到PC上。

PC接收到的数据,单位F是华氏温度,C是摄氏温度。

串口log输出:

串口这边先进入发送,然后MCU建立UDP socket, 循环发送,但是有个问题,就是发送过程会出错,然后退出发送过程,原因还未找到。

看看代码:

// sending packet
iStatus = sl_SendTo(iSockID, g_cBsdBuf, sTestBufLen, 0,
(SlSockAddr_t *)&sAddr, iAddrSize);
if( iStatus <= 0 )
{
// error
sl_Close(iSockID);
ASSERT_ON_ERROR(SEND_ERROR);
}

就是这个sl_SendTo会自己出错退出。

我修改的代码:

增加了每个循环温度采样,并且UDP发送出去。

测试结果与代码一致。

遗留问题会继续找原因,也请有经验的大师指导,谢谢。。。。

函数源码:

int BsdUdpClient(unsigned short usPort)
{
int iCounter;
short sTestBufLen;
SlSockAddrIn_t sAddr;
int iAddrSize;
int iSockID;
int iStatus;
unsigned long lLoopCount = 0;

// filling the buffer
for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++)
{
g_cBsdBuf[iCounter] = (char)(iCounter % 10);
}

//filling the UDP server socket address
sAddr.sin_family = SL_AF_INET;
sAddr.sin_port = sl_Htons((unsigned short)usPort);
sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)g_ulDestinationIp);

iAddrSize = sizeof(SlSockAddrIn_t);

// creating a UDP socket
iSockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0);
if( iSockID < 0 )
{
// error
ASSERT_ON_ERROR(SOCKET_CREATE_ERROR);
}

// for a UDP connection connect is not required
// sending 1000 packets to the UDP server
//while (lLoopCount < g_ulPacketCount)
while(1)
{
float fCurrentTemp;
char cTemp,cTempc;
unsigned char *ptr;
short sTempLen,sTempLenc;
int idx;
for(idx=0x50000;idx>0;idx--);
TMP006DrvGetTemp(&fCurrentTemp);
cTemp = (char)fCurrentTemp;
cTempc = (cTemp-32)*5/9;
Report("GetTemp=%d F, %d C.\n\r",cTemp, cTempc);
ptr = g_cBsdBuf;
sTempLen = itoa(cTemp,(char*)ptr);
ptr[sTempLen++] = ' ';
ptr[sTempLen++] = 'F';
ptr[sTempLen++] = ',';
ptr = g_cBsdBuf+sTempLen;
sTempLenc = itoa(cTempc,(char*)ptr);
ptr[sTempLenc++] = ' ';
ptr[sTempLenc++] = 'C';
ptr[sTempLenc++] = '!';
sTestBufLen = sTempLen+sTempLenc;//BUF_SIZE;
g_ulPacketCount = 1;
// sending packet
iStatus = sl_SendTo(iSockID, g_cBsdBuf, sTestBufLen, 0,
(SlSockAddr_t *)&sAddr, iAddrSize);
if( iStatus <= 0 )
{
// error
sl_Close(iSockID);
ASSERT_ON_ERROR(SEND_ERROR);
}
lLoopCount++;
}

UART_PRINT("Sent %u packets successfully\n\r",g_ulPacketCount);

//closing the socket after sending 1000 packets
sl_Close(iSockID);


return SUCCESS;
}

1 个回复