Problem Reading FrSky Smart Port (aka S.Port, SPORT)

This forum is dedicated to software development related to MultiWii.
It is not the right place to submit a setup problem.
Software download
Post Reply
riskable
Posts: 6
Joined: Mon Jan 06, 2014 3:52 am

Problem Reading FrSky Smart Port (aka S.Port, SPORT)

Post by riskable »

I'm having difficulty working with MultiWii's serial library. I've got code that works to read and write to FrSky's smart port using Arduino's regular serial libraries but I cannot get it working inside of MultiWii. Here's the relevant bits:

In MultiWii.cpp I've added:

Code: Select all

#include "FrSkySport.h"

and at the end of the annexCode() function I've added:

Code: Select all

frsky_sport();


The frsky_sport() function is just this:

Code: Select all

void frsky_sport() {
    if (FrSkySport_timeToSend()) {
        FrSkySport_sendData();
    }
}


...with the important function being FrSkySport_timeToSend():

Code: Select all

// Note that config.h has: #define FRSKY_SPORT_SERIAL 3
bool FrSkySport_timeToSend() {
  static uint8_t lastRx = 0;
// This commented-out way of looping doesn't work either:
//  uint8_t c = SerialAvailable(FRSKY_SPORT_SERIAL);
//  if (c != 0) { debug[0] = c; }
//  while (c--) {
  while (SerialAvailable(FRSKY_SPORT_SERIAL)) {
    int rx = SerialRead(FRSKY_SPORT_SERIAL);
    if (lastRx == 0x7e) {
      debug[1] = rx; // This is just here to provide visibility on what MultiWii is seeing
    }
    if (lastRx == 0x7e && rx == FRSKY_SPORT_SENSOR_ID) { // FRSKY_SPORT_SENSOR_ID == 0x98
      // On a regular Arduino (using the default serial lib) this conditional matches *constantly* (once a second maybe?)
      // With MultiWii's serial library this conditional only matches about once every 30 seconds or so.  No idea why.
      lastRx = 0;
      debug[2]++; // So I can see how often it matches
      return true;
    }
    lastRx = rx;
  }
  return false;
}


FrSkySport_sendData() just sends a simulated A2 voltage (full code will be posted once I've got the serial Rx working) but I can have it send the full gamut of FrSky telemetry. In fact, I've got a lot of that ready-to-go except for the issue that I cannot get FrSkySport_timeToSend() working!

Notes about the hardware: The FrSky Smart Port uses an inverted serial signal so I've implemented a simple UART signal inverter using a P2222 NPN transistor with 10k and 4.7k resistors:

sport_ttl_inverter.png
Simple FrSky Smart Port UART/TTL Serial Logic Inverter
(6.97 KiB) Not downloaded yet


I have independently verified that the inverter works using a separate Arduino (Duemilanove) running the following code:

Code: Select all

#include <SoftwareSerial.h>

#define rxPin 4
#define txPin 2

SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin, false);
// Note that with SoftwareSerial you can pass 'true' as the 3rd arg above
// to read the Smart Port without a hardware inverter

void setup() {
  Serial.begin(57600);
  Serial.println("Serial port monitor starting..");
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  mySerial.begin(57600); // This is the baud rate of the Smart Port
  mySerial.listen();
}

void loop() {
  if (mySerial.available()>0) {
    int val = mySerial.read();
    if (val != 0) {
      Serial.print(":");
      Serial.print(val);
    }
    else {
      Serial.print("\n");
    }
  }
}


Here's an example of what that outputs while reading the Smart Port:

Code: Select all

:126:161:126:34:126:131:126:228:126:69:126:198:126:103:126:72:126:233:126:106:126:203:126:172:126:13:126:142:126:47:126:208:126:113:126:242:126:83:126:52:126:149:126:22:126:183:126:152:126:57:126:186:126:27:126


The way the protocol works the telemetry data must be sent after detecting "126" (0x7e) immediately followed by the sensor ID that we're simulating which in the code above is "152" (0x98). That would be "126:152" in the output above.

With a separate Ardiuno hooked up to the Smart Port I can successfully send whatever telemetry data I want using this method (detect sensor ID, send payload). It shows up on my Taranis and works great!

Can anyone provide insight as to what's wrong with my FrSkySport_timeToSend() function above? Why can't I read the Smart Port data like I can with a separate Arduino using the regular libraries?

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: Problem Reading FrSky Smart Port (aka S.Port, SPORT)

Post by haydent »

hi, i modded your code with an arduino mega serial 1 and an ttl inverter between it and the x8r s.port

i got a similar if not same stream:

Code: Select all

126:161:126:34:126:131:126:228:126:69:126:198:126:103:126:72:126:233:126:106:126:203:126:172:126:13:126:142:126:47:126:208:126:113:126:242:126:83:126:52:126:149:126:22:126:183:126:152:126:57:126:186:126:27:126


cant find a match of all the sensor ids requested by the rx on the web

wondering if you had found what they are ?

thanks for your post

this is my simplified sketch for mega:

Code: Select all

void setup() {
  Serial.begin(57600);
  Serial.println("Serial port monitor starting..");
  Serial1.begin(57600); // This is the baud rate of the Smart Port
}

void loop() {
  if (Serial1.available()>0) {
    int val = Serial1.read();
    if (val != 0) {
      Serial.print(val);
      Serial.print(":");     
    }
    else {
      Serial.print("\n");
    }
  }
}

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: Problem Reading FrSky Smart Port (aka S.Port, SPORT)

Post by haydent »

Can anyone provide insight as to what's wrong with my FrSkySport_timeToSend() function above? Why can't I read the Smart Port data like I can with a separate Arduino using the regular libraries?


i now have it reading the same stream using mw 2.3 . theres nothing too it, maybe you are not setting your serial port to the right baud rate, as by default for example with mega all ports are set to 115200 in config.h

config.h

Code: Select all

#define SERIAL1_COM_SPEED 57600 //hayden


multiwii.cpp loop

Code: Select all

    //hayden
    while(SerialAvailable(1)){
    int val = SerialRead(1);
    if(val != 0 && val != 126)debug[1] = val;
    else debug[0] = val;
    }

User avatar
grödener
Posts: 7
Joined: Thu Jul 10, 2014 11:19 pm
Location: Southtirol

Re: Problem Reading FrSky Smart Port (aka S.Port, SPORT)

Post by grödener »

hi i have the crius v2 aio, and the problem is wen connecting to the crius rx port serial 3 i have a freeze, i haved flashed the bootloder whit USBASP , nothing the

same problem... i think the problem is the atmega has no the power to converting the inputs and fliing in the same time...

its possible to try a teenys 3.1 to converting ?? i have see this: http://diydrones.com/forum/topics/amp-t ... -converter

the problem is that work with apm, its included a sketch for mavlink.... i not a developer, its possible to convert the sketchcode for MWI?

or better use the mavlinkprotokoll to the mwi 2.3 liftoff?! sry my english is beed..

Post Reply