ESP8266解析S-BUS协议

Talk is cheap, show you code!
编译平台:Arduino IDE

/**
 *  功能:软件串口读取S-BUS数据;解析;串口打印
 */
#include <SoftwareSerial.h>
#define None 5//D1(空)
#define DataPort 14//D5

uint8_t Data_packet[26];
uint16_t CH[16];  // 通道值

//声明一个软件串口
SoftwareSerial swSerial;

void setup() 
{
  Serial.begin(115200);   
  //设置软件串口波特率  
  //begin(uint32_t baud, SoftwareSerialConfig config,int8_t rxPin, int8_t txPin, bool invert,int Data_packetCapacity = 64, int isrData_packetCapacity = 0);
  swSerial.begin(100000,SWSERIAL_8E2,DataPort,None,true);
}

void loop() 
{
  readSerial();
  Sbus_Data_Count();
  DebugPrint();
}

void DebugPrint()
{
  //Serial.printf("%4d ",Data_packet[1]);//0x0F
  for(int i=0;i<6;i++)//mc6c遥控器只有6个通道
  {
    Serial.printf("%4d ",CH[i]);
  }
  Serial.println();
  //Serial.printf("%4d\n",Data_packet[25]);//0x00

  /*for(int i=1;i<=25;i++)
  {
    Serial.printf("%4d ",Data_packet[i]);
  }
  Serial.println("");*/
}

void readSerial()
{
  for(int i=1;i<=25;i++)
  {
    if(swSerial.available())
    {
      Data_packet[i] = swSerial.read();
      //Serial.printf("Byte%d:%d\n",i,Data_packet[i]);
    }
  }
}

void Sbus_Data_Count()
{
  CH[ 0] = ((int16_t)Data_packet[ 2] >> 0 | ((int16_t)Data_packet[ 3] << 8 )) & 0x07FF;
  CH[ 1] = ((int16_t)Data_packet[ 3] >> 3 | ((int16_t)Data_packet[ 4] << 5 )) & 0x07FF;
  CH[ 2] = ((int16_t)Data_packet[ 4] >> 6 | ((int16_t)Data_packet[ 5] << 2 )  | (int16_t)Data_packet[ 6] << 10 ) & 0x07FF;
  CH[ 3] = ((int16_t)Data_packet[ 6] >> 1 | ((int16_t)Data_packet[ 7] << 7 )) & 0x07FF;
  CH[ 4] = ((int16_t)Data_packet[ 7] >> 4 | ((int16_t)Data_packet[ 8] << 4 )) & 0x07FF;
  CH[ 5] = ((int16_t)Data_packet[ 8] >> 7 | ((int16_t)Data_packet[ 9] << 1 )  | (int16_t)Data_packet[10] <<  9 ) & 0x07FF;
  CH[ 6] = ((int16_t)Data_packet[10] >> 2 | ((int16_t)Data_packet[11] << 6 )) & 0x07FF;
  CH[ 7] = ((int16_t)Data_packet[11] >> 5 | ((int16_t)Data_packet[12] << 3 )) & 0x07FF;
  
  CH[ 8] = ((int16_t)Data_packet[13] << 0 | ((int16_t)Data_packet[14] << 8 )) & 0x07FF;
  CH[ 9] = ((int16_t)Data_packet[14] >> 3 | ((int16_t)Data_packet[15] << 5 )) & 0x07FF;
  CH[10] = ((int16_t)Data_packet[15] >> 6 | ((int16_t)Data_packet[16] << 2 )  | (int16_t)Data_packet[17] << 10 ) & 0x07FF;
  CH[11] = ((int16_t)Data_packet[17] >> 1 | ((int16_t)Data_packet[18] << 7 )) & 0x07FF;
  CH[12] = ((int16_t)Data_packet[18] >> 4 | ((int16_t)Data_packet[19] << 4 )) & 0x07FF;
  CH[13] = ((int16_t)Data_packet[19] >> 7 | ((int16_t)Data_packet[20] << 1 )  | (int16_t)Data_packet[21] <<  9 ) & 0x07FF;
  CH[14] = ((int16_t)Data_packet[21] >> 2 | ((int16_t)Data_packet[22] << 6 )) & 0x07FF;
  CH[15] = ((int16_t)Data_packet[22] >> 5 | ((int16_t)Data_packet[23] << 3 )) & 0x07FF;
}
  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值