使用芯片stc15w4k56s4,串口1接收命令,串口2,3依次下发命令。发送偶尔就来一次crc算错了,下一帧crc又好了。计算错误在串口发送之前,很神奇,是指针用的有问题吗?
- unsigned char motor_s3_long[13]={0x7f,0x10,0x06,0x06,0x00,0x02,0x04,0x0b,0xb1,0x00,0x00,0x94,0x0f};
- void Message_Composed2_Long(uint address,uint cmds_1,uint cmds_2)
- {
- motor_s3_long[2] =address/256;
- motor_s3_long[3] =address%256;
- motor_s3_long[7] =cmds_1/256;
- motor_s3_long[8] =cmds_1%256;
- motor_s3_long[9] =cmds_2/256;
- motor_s3_long[10]=cmds_2%256;
- }
复制代码
- void S3_Send_RS485(unsigned char *sendline,unsigned char length)
- {
- unsigned char i=0, j=0,n=0;
- unsigned short reg_crc=0xffff;
- //crc计算-----------------------------//
- while((i<(length-2)))
- {
- reg_crc^= sendline[i];
- for (j=0; j<8; j++ )
- {
- if( reg_crc & 0x01 )
- {
- reg_crc = (reg_crc >> 1)^0xA001;
- }
- else
- {
- reg_crc = (reg_crc>>1);
- }
- }
- i++;
- }
- sendline[length-1]=(reg_crc/256)%256;
- sendline[length-2]=reg_crc%256;
-
- RS485_2 = 1;
- for(n=0;n<length;n++)
- {
- S3_SendByte(sendline[n]);
- }
- RS485_2 = 0;
- }
复制代码
- Message_Composed2_Long(ASD6003,by_speed2,0x0000);
- S3_Send_RS485(motor_s3_long,13);
复制代码
|