Talk is cheap, show you code! 编译平台:Arduino IDE
#include <SoftwareSerial.h>
#define None 5
#define DataPort 14
uint8_t Data_packet[26];
uint16_t CH[16];
SoftwareSerial swSerial;
void setup()
{
Serial.begin(115200);
swSerial.begin(100000,SWSERIAL_8E2,DataPort,None,true);
}
void loop()
{
readSerial();
Sbus_Data_Count();
DebugPrint();
}
void DebugPrint()
{
for(int i=0;i<6;i++)
{
Serial.printf("%4d ",CH[i]);
}
Serial.println();
}
void readSerial()
{
for(int i=1;i<=25;i++)
{
if(swSerial.available())
{
Data_packet[i] = swSerial.read();
}
}
}
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;
}
|