搜索
bottom↓
回复: 0

zstack串口助手如何发送数据给协调器?

[复制链接]

出0入0汤圆

发表于 2015-6-11 09:31:36 | 显示全部楼层 |阅读模式
本帖最后由 yodrome 于 2015-6-11 09:38 编辑

根据weebee的例子改了一下。我每次发送 FE 02 41 80 01 01 00.(这里的CM0 CM1是在另一个例子里看来的,不知道是什么意思) 1S发一次,协调器接到串口时间之后,把数据广播出去。发送数据一多,协调器就再也收不到数据了。
不知道是什么原因。求大神指教。

  1. void MT_UartProcessZToolData ( uint8 port, uint8 event )
  2. {
  3.   uint8  ch;
  4.   uint8  bytesInRxBuffer;
  5.   
  6.   (void)event;  // Intentionally unreferenced parameter

  7.   while (Hal_UART_RxBufLen(port))
  8.   {
  9.    
  10.     HalUARTRead (port, &ch, 1);
  11.     //HalLedSet(HAL_LED_1,HAL_LED_MODE_TOGGLE);
  12.     switch (state)
  13.     {
  14.       case SOP_STATE:
  15.         if (ch == MT_UART_SOF)
  16.           state = LEN_STATE;
  17.         break;

  18.       case LEN_STATE:
  19.         LEN_Token = ch;

  20.         tempDataLen = 0;

  21.         /* Allocate memory for the data */
  22.         pMsg = (mtOSALSerialData_t *)osal_msg_allocate( sizeof ( mtOSALSerialData_t ) +
  23.                                                         MT_RPC_FRAME_HDR_SZ + LEN_Token );

  24.         if (pMsg)
  25.         {
  26.           /* Fill up what we can */
  27.           pMsg->hdr.event = CMD_SERIAL_MSG;
  28.           pMsg->msg = (uint8*)(pMsg+1);
  29.           pMsg->msg[MT_RPC_POS_LEN] = LEN_Token;
  30.           state = CMD_STATE1;
  31.         }
  32.         else
  33.         {
  34.           state = SOP_STATE;
  35.           return;
  36.         }
  37.         break;

  38.       case CMD_STATE1:
  39.         pMsg->msg[MT_RPC_POS_CMD0] = ch;
  40.         state = CMD_STATE2;
  41.         break;

  42.       case CMD_STATE2:
  43.         pMsg->msg[MT_RPC_POS_CMD1] = ch;
  44.         /* If there is no data, skip to FCS state */
  45.         if (LEN_Token)
  46.         {
  47.           state = DATA_STATE;
  48.         }
  49.         else
  50.         {
  51.           state = FCS_STATE;
  52.         }
  53.         break;

  54.       case DATA_STATE:

  55.         /* Fill in the buffer the first byte of the data */
  56.         pMsg->msg[MT_RPC_FRAME_HDR_SZ + tempDataLen++] = ch;

  57.         /* Check number of bytes left in the Rx buffer */
  58.         bytesInRxBuffer = Hal_UART_RxBufLen(port);

  59.         /* If the remain of the data is there, read them all, otherwise, just read enough */
  60.         if (bytesInRxBuffer <= LEN_Token - tempDataLen)
  61.         {
  62.           HalUARTRead (port, &pMsg->msg[MT_RPC_FRAME_HDR_SZ + tempDataLen], bytesInRxBuffer);
  63.           tempDataLen += bytesInRxBuffer;
  64.         }
  65.         else
  66.         {
  67.           HalUARTRead (port, &pMsg->msg[MT_RPC_FRAME_HDR_SZ + tempDataLen], LEN_Token - tempDataLen);
  68.           tempDataLen += (LEN_Token - tempDataLen);
  69.         }

  70.         /* If number of bytes read is equal to data length, time to move on to FCS */
  71.         if ( tempDataLen == LEN_Token )
  72.             state = FCS_STATE;

  73.         break;

  74.       case FCS_STATE:

  75.         FSC_Token = ch;

  76.         /* Make sure it's correct */
  77.         //if ((MT_UartCalcFCS ((uint8*)&pMsg->msg[0], MT_RPC_FRAME_HDR_SZ + LEN_Token) == FSC_Token))
  78.        // {
  79.           osal_msg_send( App_TaskID, (byte *)pMsg );
  80.        // }
  81.        // else
  82.        // {
  83.           /* deallocate the msg */
  84.        //   osal_msg_deallocate ( (uint8 *)pMsg );
  85.        // }

  86.         /* Reset the state, send or discard the buffers at this point */
  87.         state = SOP_STATE;

  88.         break;

  89.       default:
  90.        break;
  91.     }
  92.   }
  93. }
复制代码



调试发现下面的提示串口输出代码,只要都注释掉,就很容易就死掉了。
放着就能正常工作挺久。
  1. void SampleApp_SerialCMD(mtOSALSerialData_t *cmdMsg)
  2. {
  3.   uint8 i,len,*str=NULL;     //len有用数据长度
  4.   str=cmdMsg->msg;          //指向数据开头
  5.   len=*str;                 //msg里的第1个字节代表后面的数据长度

  6.   /********打印出串口接收到的数据,用于提示*********/
  7.   //Onboard_wait(300);
  8.   //for(i=0;i<=len+2;i++)
  9.   //HalUARTWrite(0,str+i,1 );
  10.   HalUARTWrite(0,"\n",1 );//换行  
  11.   HalLedSet(HAL_LED_1,HAL_LED_MODE_TOGGLE);
  12.   /*******发送出去***参考网蜂 1小时无线数据传输教程*********/

  13.   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
  14.                             SAMPLEAPP_PERIODIC_CLUSTERID,//自己定义一个
  15.                             len+3,                  // 数据长度         
  16.                             str,                     //数据内容
  17.                             &SampleApp_TransID,
  18.                             AF_DISCV_ROUTE,
  19.                             AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  20.                              {
  21.                              }
  22.   else
  23.   {
  24.   // Error occurred in request to send.
  25.   }
  26. }
复制代码
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-9 20:01

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表