找回密码
 立即注册
查看: 1393|回复: 0

stc15w4k56s4会偶尔算错crc

[复制链接]

该用户从未签到

1

主题

0

回帖

11

积分

新手上路

积分
11
发表于 2022-11-29 15:22:28 | 显示全部楼层 |阅读模式
使用芯片stc15w4k56s4,串口1接收命令,串口2,3依次下发命令。发送偶尔就来一次crc算错了,下一帧crc又好了。计算错误在串口发送之前,很神奇,是指针用的有问题吗?
  1. unsigned char motor_s3_long[13]={0x7f,0x10,0x06,0x06,0x00,0x02,0x04,0x0b,0xb1,0x00,0x00,0x94,0x0f};
  2. void Message_Composed2_Long(uint address,uint cmds_1,uint cmds_2)
  3. {
  4.   motor_s3_long[2] =address/256;
  5.   motor_s3_long[3] =address%256;
  6.   motor_s3_long[7] =cmds_1/256;
  7.   motor_s3_long[8] =cmds_1%256;
  8.   motor_s3_long[9] =cmds_2/256;
  9.   motor_s3_long[10]=cmds_2%256;
  10. }
复制代码
  1. void S3_Send_RS485(unsigned char *sendline,unsigned char length)
  2. {
  3.   unsigned char i=0, j=0,n=0;
  4.   unsigned short reg_crc=0xffff;
  5.          //crc计算-----------------------------//
  6.   while((i<(length-2)))
  7.   {
  8.     reg_crc^= sendline[i];       
  9.     for (j=0; j<8; j++ )
  10.    {
  11.       if( reg_crc & 0x01 )
  12.      {
  13.        reg_crc = (reg_crc >> 1)^0xA001;
  14.      }
  15.      else
  16.     {
  17.          reg_crc = (reg_crc>>1);
  18.     }
  19.    }
  20.    i++;
  21.                 }         
  22.   sendline[length-1]=(reg_crc/256)%256;
  23.   sendline[length-2]=reg_crc%256;
  24.                  
  25.     RS485_2 = 1;
  26.     for(n=0;n<length;n++)
  27.     {
  28.         S3_SendByte(sendline[n]);
  29.      }  
  30.      RS485_2 = 0;
  31. }
复制代码
  1. Message_Composed2_Long(ASD6003,by_speed2,0x0000);
  2. S3_Send_RS485(motor_s3_long,13);
复制代码

回复 送花

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

GMT+8, 2024-4-20 14:17 , Processed in 0.054784 second(s), 28 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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