Custom Controller?

What I mean by an ASCII protocol is that everything is sent using printing characters, to send the number 1023, four characters are sent. in hex this would be

0x31 0x30 0x32 0x33

which if sent to a terminal program like hyperterminal would print out as “1023”

if you were to use with a binary protocol you would send perhaps the binary representation of the number 1023 as two bytes

0x03 0xFF

or

0xFF 0x03

(google big endian and little endian to explain why the two possible ways)

It should be possible… you will need to remove all arudino dependencies… it also might be overkill…

Maybe I shouldn’t have started this project, the whole thing is just way to confusing…

So in ascii, would be 17 bytes, right?
and in binary, it would be how many? Why can’t all these serial communications be easy and actually work with other systems:(? I don’t think I will ever get the system working.

So should I use ascii or binary to send commands and variables to vex? All I am doing is using Serial.write to send variables to vex.

Looks like sending ascii and recieving ascii works, like it will actually show letters and numbers. Problem is, it will only show it every so many characters… Still, I don’t know how to put it back into variables once I recieve it on the vex.

Either could work for you, but you have to have a way to synchronize the receiver with the transmitter from the stand point of messages.

Take a look at scanf, the opposite of printf.

That is really confusing. Are you sure scanf can read and put them into their own variables?

I found this on the old Arduino forums:

I think I can get a clue how they did it. Most of it is on that forum post.

My interpretation:
string is the input.
stuff in the “” is what you are searching for
&temp1, &temp2 are int variables

// everything.c : implementation file
#include "API.h"

void stuff ( void )
{
    int input;
    int input2;

    int x;
    int y;
   
    OpenSerialPortTwo ( BAUD_115200 ) ;
    //OpenSerialPortOne ( BAUD_115200 ) ;

    while(1){
        //input2=input;
        while(GetSerialPort2ByteCount()){
            input=ReadSerialPortTwo();
            scanf(input, "<%d %d>", &x, &y);
        }
        //if (input2!=input){
        PrintToScreen ( "%d" , (int)x ) ; 
        PrintToScreen ( " %d\n" , (int)y ) ; 
       // }
        Wait ( 16 ) ; 
    }
}

Errors:

Hey Soapy29,

I don’t have a Vex development system in front of me at the moment, but if scanf() isn’t in the runtime library, then it will be a bit harder to use ASCII messages. You can still pick the string apart one character at a time and calculate the values, but it requires some tedious code.

For instance, if you have your packet in the string called “input”, and you want to get the value of the 7th character in the string, you can write:


unsigned int ValueOfSeventhChar = input[6] - '0';

This will convert that single character into a binary quantity from 0 to 9 and assign it to ValueOfSeventhChar. Note that this assumes the character really is an ASCII code from ‘0’ to ‘9’. If it is some other non-digit character, you will get a bogus value.

The index value “[6]” is the 7th character of the input string because arrays start at index 0. (e.g., input[0] is the 1st character).

If you want to convert a multi-digit number, you can pick out each digit and then sum them back together like so:

unsigned int Thousands = input [2] - '0';
unsigned int Hundreds = input[3] - '0';
unsigned int Tens = input[4] - '0';
unsigned int Ones = input [5] - '0';

unsigned int Number = Thousands*1000 + Hundreds*100 + Tens*10 + Ones;

Of course, you can wrap this in loops and write functions to help make this cleaner, but that is exactly what scanf() and other related functions like atoi() do for you. If you can find them, I’d recommend using them.

Hope that helps…

  • Dean
// everything.c : implementation file
#include "API.h"

void stuff(){
    int input;
    int input2;

    int x;
    int y=0;
   
    OpenSerialPortTwo ( BAUD_115200 ) ;
    //OpenSerialPortOne ( BAUD_115200 ) ;

    while(1){
        //input2=input;
        while(GetSerialPort2ByteCount()){
            input=ReadSerialPortTwo();
            //scanf(input, "<%d %d>", &x, &y);
            unsigned int Thousands = input[1] - '0';
            unsigned int Hundreds = input[2] - '0';
            unsigned int Tens = input[3] - '0';
            unsigned int Ones = input[4] - '0';

            unsigned int x = Thousands*1000 + Hundreds*100 + Tens*10 + Ones;
        }
        //if (input2!=input){
        PrintToScreen ( "%d" , (int)x ) ; 
        PrintToScreen ( " %d\n" , (int)y ) ; 
       // }
        Wait ( 16 ) ; 
    }
}

line#19 syntax error

This is line 19:
unsigned int Thousands = input[1] - ‘0’;

You will probably need to split up the variable declarations from their point of use. I just presented it the way I did for compactness:

// Variable declarations
            unsigned int Thousands;
            unsigned int Hundreds;
            unsigned int Tens;
            unsigned int Ones;
            unsigned int x;

// ...

            Thousands = input[1] - '0';
            Hundreds = input[2] - '0';
            Tens = input[3] - '0';
            Ones = input[4] - '0';

            x = Thousands*1000 + Hundreds*100 + Tens*10 + Ones;

You can also do this more compactly using a single variable as an accumulator, though it isn’t quite as easy to understand if you aren’t familiar with the concept:

// Variable declaration
            unsigned int x;  // Accumulator

// ...

            x = (input[1]-'0'); // Thousands Place
            x = (input[2]-'0') + (10*x) // Hundreds Place
            x = (input[3]-'0') + (10*x) // Tens Place
            x = (input[4]-'0') + (10*x) // Ones Place

Cheers,

  • Dean

EasyC Pro needs the feature to copy the output…

line# 23-26: ] operator requires a pointer and an integer as operands

23: Thousands = input[1]-‘0’;
24: Hungreds = input[2]-‘0’;
25: Tens = input[3]-‘0’;
26: Ones = input[4]-‘0’;

The above assumes that input is an array, where as input is just a single Integer.

You want an array of Characters to use the above Code.

input=ReadSerialPortTwo(); will read a Single Byte, into the variable input, (which is an Integer right now).

You will need to Loop through reading ReadSerialPortTwo(), until you get a New Line, SAVING all the characters you read into the Array mentioned above… then follow Quazar’s example code above…

We are teaching at an accelerated rate here. It is a little hard. Quazar, jpearman and I do this kind of thing for a Day Job… Easy systems have many failure points… why teach you the wrong way, when we can teach the better way.

17, if you include the <>. Binary, could be reduced to 3 or 5 bytes.

How can I tell if there is a new line? What I am going to try and do is have it read all the characters until the newline thing is read, then it will move on and set it to variables. I have a good idea on how to do this, but I don’t know the newline thing. I’ll look it up :smiley:

Here is what I have. It still says that Thousands=input[1]-‘0’; has a syntax error.

// everything.c : implementation file
#include "API.h"

void stuff(){
    char input[12];
    int input2;

    int x;
    int y=0;
    int Thousands;
    int Hundreds;
    int Tens;
    int Ones;
    //char input[5];
    int spot=0;
    int thing;
   
    OpenSerialPortTwo ( BAUD_115200 ) ;
    //OpenSerialPortOne ( BAUD_115200 ) ;

    while(1){
        //input2=input;
        while(GetSerialPort2ByteCount()){
            thing=ReadSerialPortTwo();
            if (thing=='\'){
                return;
            }
            input[spot]=thing;
            spot++;
            //scanf(input, "<%d %d>", &x, &y);
        }

        Thousands = input[1]-'0';
        Hundreds = input[2] - '0';
        Tens = input[3] - '0';
        Ones = input[4] - '0';

        x = Thousands*1000 + Hundreds*100 + Tens*10 + Ones;
        //if (input2!=input){
        PrintToScreen ( "%d" , (int)x ) ; 
        PrintToScreen ( " %d\n" , (int)y ) ; 
       // }
        Wait ( 16 ) ; 
    }
}

I am working on a response to that, and a few other issues…

I couldn’t figure this out with EasyC Pro either, so I complained, and was told how to do it… EasyC 1.x and 2.x work differently… :wink:

At the Bottom of the Left Pane, click on the Project Tab, then Right Click on the Main Function ( or the Function Name you want to Copy ), then Left Click the “Open C File” or “Open H File” to use the Internal Editor. Then Click “Select All” or Click and Drag to Highlight Text, and then Right Click on Highlighted Text and Left Click Copy.

I was talking about the output, the pane even lower than that one. You can’t just highlight the outputs and copy them, I think Arduino can do that. It is a nice feature for debugging though, or just copying on to the forum :slight_smile:

Look for the .ERR file in the Directory that your Project is in…

I just compiled:


#include "Main.h"

void main ( void )
{
      while ( 1 )
      {
            Tank2 ( 0 , 3 , 2 , 3 , 2 , 0 , 1 ) ;
            if ( &&  )
            {
            }
      }
}


And got this .ERR file:


Error[yy] Main.c line# 8 : syntax error