title: Reading data from RadioLink R12DS receiver by S-BUS protocol
body: <p>My goal is pretty simple: I wanna read information from my RadioLink R12DS receiver by S-BUS protocol using desktop console application, written on C#.</p>
  
  <p>I use AT9S transmitter blinded together with receiver in 12 channel mode. I tested it on Pixhawk flight controller. Everything was fine there, no any problem with retrieving data.</p>
  
  <p>I designed a console application based on investigated articles. Here is a couple most valuable of them:</p>
  
  <p><a href="http://forum.fpv.kz/topic/303-frsky-x8r-sbus-v-cppm-konverter-na-arduino/" rel="nofollow noreferrer">http://forum.fpv.kz/topic/303-frsky-x8r-sbus-v-cppm-konverter-na-arduino/</a> <a href="https://github.com/bolderflight/SBUS" rel="nofollow noreferrer">https://github.com/bolderflight/SBUS</a></p>
  
  <p>My application receiving byte stream from a COM Port, one by one, and tries catch message header "0x0F", but it doesn't appear.</p>
  
  <p>The SBUS protocol uses inverted serial logic with a baud rate of 100000, 8 data bits, even parity bit, and 2 stop bits. The SBUS packet is 25 bytes long consisting of:</p>
  
  <ul>
  <li>Byte[0]: SBUS Header, 0x0F</li>
  <li>Byte[1-22]: 16 servo channels, 11 bits per servo channel</li>
  <li>Byte[23]:</li>
  <li>Bit 7: digital channel 17 (0x80)</li>
  <li>Bit 6: digital channel 18 (0x40)</li>
  <li>Bit 5: frame lost (0x20)</li>
  <li>Bit 4: failsafe activated (0x10)</li>
  <li>Bit 0 - 3: n/a</li>
  <li>Byte[24]: SBUS End Byte, 0x00</li>
  </ul>
  
  <p>A table mapping bytes[1-22] to servo channels is <a href="https://github.com/bolderflight/SBUS/blob/master/extras/bit-mapping.pdf" rel="nofollow noreferrer">included</a>.</p>
  
  <p>Here is listing of my code:</p>
  
  <pre><code>static void Main(String[] args)
      {
          var availablePorts = SerialPort.GetPortNames();
          using(var port = new SerialPort(availablePorts[0], 100000, Parity.Even, 8, StopBits.Two))
          {
              port.DataReceived += PortOnDataReceived;
              while(true)
              {
                  if(!port.IsOpen)
                      TryReconnect(port);
  
                  Thread.Sleep(1000);
              }
          }
      }
  
  
      // HANDLERS ///////////////////////////////////////////////////////////////////////////////
      private static void PortOnDataReceived(Object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
      {
          var serialPort = (SerialPort)sender;
          if(SbusConverter.TryReadMessage(serialPort, out var messageBuffer))
          {
              var message = SbusConverter.Convert(messageBuffer);
  
              Console.WriteLine(message.ServoChannels[0]);
          }
      }
  
  public static Boolean TryReadMessage(SerialPort serialPort, out Byte[] messageBuffer)
      {
          const Int32 messageLength = 25;
          const Int32 endOfStream = -1;
          const Byte sBusMessageHeader = 0x0f;
          const Byte sBusMessageEndByte = 0x00;
  
          messageBuffer = new Byte[messageLength];
  
          if(serialPort.BytesToRead &lt; messageLength)
              return false;
  
          do
          {
              var value = serialPort.ReadByte();
              if(value == endOfStream)
                  return false;
  
              if(value == sBusMessageHeader)
              {
                  messageBuffer[0] = (Byte)value;
                  for(var i = 1; i &lt; messageLength; i++)
                  {
                      messageBuffer[i] = (Byte)serialPort.ReadByte();
                  }
  
                  if(messageBuffer[0] == sBusMessageHeader &amp;&amp;
                     messageBuffer[24] == sBusMessageEndByte)
                      return true;
              }
  
          } while(serialPort.BytesToRead &gt; 0);
  
          return false;
      }
  </code></pre>
  
  <p>I have thoughts in my head and I want ask one question here. </p>
  
  <p>It's possible, that RadioLink use different, modified or their own S-BUS implementation, than Futaba and I found no proper documentation yet.</p>
  
  <p>Anybody, who experienced in that field, any suggestions please. It seems, I am stuck.</p>
  
  <p>Thank you!</p>