MultiWii Serial Protocol Testing

This forum is dedicated to software development related to MultiWii.
It is not the right place to submit a setup problem.
Software download

MultiWii Serial Protocol Testing

Postby Memarnejad » Sun Aug 07, 2016 8:50 am

Hi,
I would like to test the Multiwii Serial Protocol (MSP) individually.
The way I am doing this is that I have written a simple program in processing as bellow:

Code: Select all
import processing.serial.*; // serial library
import java.util.*;
import java.lang.*;

String MSP_HEADER = "$M<";
int
  MSP_IDENT                =100,
  MSP_STATUS               =101,
  MSP_RAW_IMU              =102;
 
Serial myPort;
//flag variable is used to send the request just once
boolean flag;
long delay = 0;

//This function has been copied from the multiwii conf source code 
List<Byte> requestMSP (int msp, Character[] payload) {
      if(msp < 0) {
       return null;
      }
      List<Byte> bf = new LinkedList<Byte>();
      for (byte c : MSP_HEADER.getBytes()) {
        bf.add( c );
      }
     
      byte checksum=0;
      byte pl_size = (byte)((payload != null ? int(payload.length) : 0)&0xFF);
      bf.add(pl_size);
      checksum ^= (pl_size&0xFF);
     
      bf.add((byte)(msp & 0xFF));
      checksum ^= (msp&0xFF);
     
      if (payload != null) {
        for (char c :payload){
          bf.add((byte)(c&0xFF));
          checksum ^= (c&0xFF);
        }
      }
      bf.add(checksum);
      return (bf);
}
 
//This function has been copied from the multiwii conf source code 
void sendRequestMSP(List<Byte> msp) {
    byte[] arr = new byte[msp.size()];
    println("Message is : ");
    println(msp);
    int i = 0;
    for (byte b: msp) {
      arr[i++] = b;
    }
    myPort.write(arr); // send the complete byte sequence in one go
}
 


void setup(){

  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 115200);
 
  flag = true;
}

void draw(){
   
    myPort.buffer(256);
    //myPort.clear();
    int c;
    int i = 0;
   
    //Send request to multiwii
    if (flag){
      sendRequestMSP(requestMSP (MSP_STATUS,null));
      flag = false;
   
      println("Waiting to receive...");
   
      //make delay
      for (i=0; i<2; i++){
          delay = 0;
       
        while (delay < 10000){
           delay += 1;
          }
      }
     
      //get response from multiwii
      while (myPort.available()>0) {
          c = (myPort.read());
          println(c);
   
      }
    }
}


what I am doing is that I am making a MSP_STATUS request and the send it toward the arduino UNO which has been programed with the multiwii program in advance.
I expect to get the response from the arduino back but no response is returned and printed in my computer.

What is wrong with it ?? How can I create a MSP message myself and send it to arduino and get the response back ??
Any alternative way OR Any program has been written previously ?
Thanks.
Memarnejad
 
Posts: 3
Joined: Sat Jul 23, 2016 10:53 am

Re: MultiWii Serial Protocol Testing

Postby gregd72002 » Sun Aug 07, 2016 7:02 pm

Here is a service-client code that does exactly what you are trying to do
https://github.com/rpicopter/mw-service
gregd72002
 
Posts: 103
Joined: Fri Dec 12, 2014 5:16 pm

Re: MultiWii Serial Protocol Testing

Postby Memarnejad » Mon Aug 08, 2016 9:10 am

Whta is wrong with my Code??

I have written another program in python doing the same thing but this also does not work.
here is the code:

Code: Select all
import serial
import time
   
if __name__ == "__main__":

  PORT = 'COM3'
  BAUD = 115200

  s = serial.Serial(PORT, BAUD, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1)

  #Sending toward Arduino
  #1111111111111111111111
  s.write(chr(36))  #ASCII CODE for $
  s.write(chr(77))  #ASCII CODE for M
  s.write(chr(60))  #ASCII CODE for <
  s.write(chr(0))   #ASCII CODE for null because we have no payload
  s.write(chr(100)) #MSP Message 100 which is MSP_IDENT
  s.write(chr(100)) #Check sum
  time.sleep(1)     #make a delay for 1 sec and then send another packet
 
  #22222222222222222222222
  s.write('$')
  s.write('M')
  s.write('<')
  s.write(chr(0))
  s.write(chr(116))
  s.write(chr(116))
  time.sleep(1)
 
  #33333333333333333333333
  s.write('$')
  s.write('M')
  s.write('<')
  s.write(chr(0))
  s.write(chr(111))
  s.write(chr(111))
  time.sleep(1)
 
  #44444444444444444444444
  s.write('$')
  s.write('M')
  s.write('<')
  s.write(chr(0))
  s.write(chr(112))
  s.write(chr(112))
  time.sleep(1)
 
  #55555555555555555555555
  s.write('$')
  s.write('M')
  s.write('<')
  s.write(chr(0))
  s.write(chr(115))
  s.write(chr(115))
  time.sleep(1)
 
  #66666666666666666666666
  s.write('$')
  s.write('M')
  s.write('<')
  s.write(chr(0))
  s.write(chr(113))
  s.write(chr(113))
  time.sleep(1)
 
  #777777777777777777777777
  s.write('$')
  s.write('M')
  s.write('<')
  s.write(chr(0))
  s.write(chr(114))
  s.write(chr(114))
  time.sleep(1)
 
  #888888888888888888888888
  s.write('$')
  s.write('M')
  s.write('<')
  s.write(chr(0))
  s.write(chr(120))
  s.write(chr(120))
  time.sleep(1)
  #time.sleep(5)
 
 
  #s.flush()

  #Getting inforamtion from Arduino
  response = s.read(100)

  print response
  s.close()


What is wrong with this one(python code) ?? Any solution for any of the two code (processing code or python code) would be fine.
I really need help to fix the problem.
I have tried may things but non of them solved the problem.
can any one run the program in his computer and find the problem and then give me back the solution to the problem.
THANKS.
Memarnejad
 
Posts: 3
Joined: Sat Jul 23, 2016 10:53 am

Re: MultiWii Serial Protocol Testing

Postby gregd72002 » Tue Aug 09, 2016 8:38 pm

are you sure your wiring is correct (tx to rx, rx to tx and common ground), you are not getting overflow on the uart (you are sending many request and reading only once at the end), your uart configuration is ok?

You never mentioned what TTL levels you are using on both ends...
gregd72002
 
Posts: 103
Joined: Fri Dec 12, 2014 5:16 pm


Return to Software development

Who is online

Users browsing this forum: No registered users and 4 guests