Subversion Repositories Projects

Rev

Rev 449 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 449 Rev 450
Line 8... Line 8...
8
 * see http://www.mikrokopter.com/ucwiki/en/SerialCommands
8
 * see http://www.mikrokopter.com/ucwiki/en/SerialCommands
9
 * to look at the possible commands that are already coded
9
 * to look at the possible commands that are already coded
10
 * in data
10
 * in data
11
 */
11
 */
12
void Parser::create_frame(char cmd, int address, char * data, unsigned int length) {
12
void Parser::create_frame(char cmd, int address, char * data, unsigned int length) {
13
        //if # is cmd we do not touch anything, because
13
    //if # is cmd we do not touch anything, because
14
        // the command is already encoded in data
14
    // the command is already encoded in data
15
        if (cmd != '#') {
15
    if (cmd != '#') {
16
            /*
16
        /*
17
            //calculate buffer length
17
        //calculate buffer length
18
            //(4 Bytes for 1-byte '#', 1-byte address, and 2-byte crc)
18
        //(4 Bytes for 1-byte '#', 1-byte address, and 2-byte crc)
19
            //(the rest for the data length - see encode how this is calculated)
19
        //(the rest for the data length - see encode how this is calculated)
20
            int buff_len = 4+(length/3 + (length%3==0?0:1) )*4;
20
        int buff_len = 4+(length/3 + (length%3==0?0:1) )*4;
21
 
21
 
22
            //allociate memory for the data we want to send
22
        //allociate memory for the data we want to send
23
            char * send_data = (char *)malloc(buf_len);
23
        char * send_data = (char *)malloc(buf_len);
24
            */
24
        */
-
 
25
        Parser::encode64(data, length);
25
            char send_data[150];
26
        char send_data[150];
26
            send_data[0]='#';
27
        send_data[0]='#';
27
            send_data[1]=(char)address;
28
        send_data[1]=(char)address+'a';
28
            send_data[2]=cmd;
29
        send_data[2]=cmd;
29
            for (int i = 0; i < length; i++)
30
        for (int i = 0; i < length; i++)
30
                send_data[i+3] = data[i];
31
            send_data[i+3] = data[i];
31
            //TODO: abgleich mit MKCommunication::send_command
-
 
32
            Parser::encode64(send_data, length);
32
        add_CRC(send_data, length+3);
33
            address = 'a' + address;
33
        data = send_data;
34
           
-
 
35
        }
34
    }
36
}
35
}
37
 
36
 
38
 
37
 
39
/**
38
/**
40
 * Base64 Decoder
39
 * Base64 Decoder
Line 128... Line 127...
128
    //move pointer of tx_buff to data
127
    //move pointer of tx_buff to data
129
    data = tx_buff;
128
    data = tx_buff;
130
}
129
}
131
 
130
 
132
// Datensatz nach 8bit Integer
131
// Datensatz nach 8bit Integer
133
int Parser::dataToChar(int *data , int start, bool is_signed) {
132
int Parser::dataToChar(char *data , int start, bool is_signed) {
134
    int out = (data[start]);
133
    int out = (data[start]);
135
 
134
 
136
    if ((out > 128) && (is_signed))
135
    if ((out > 128) && (is_signed))
137
      out = out - 256;
136
      out = out - 256;
138
 
137
 
Line 149... Line 148...
149
}
148
}
150
 
149
 
151
/**
150
/**
152
 * convert data to 16bit Integer
151
 * convert data to 16bit Integer
153
 */
152
 */
154
int Parser::dataToInt(int *Data , int Start, bool is_signed)
153
int Parser::dataToInt(char *Data , int Start, bool is_signed)
155
{
154
{
156
    int Out = (Data[Start+1]<<8) | (Data[Start+0]);
155
    int Out = (Data[Start+1]<<8) | (Data[Start+0]);
157
 
156
 
158
    if ((Out > 32767) && (is_signed))
157
    if ((Out > 32767) && (is_signed))
159
      Out = Out - 65536;
158
      Out = Out - 65536;
Line 163... Line 162...
163
}
162
}
164
 
163
 
165
/**
164
/**
166
 * convert data to 32bit Long
165
 * convert data to 32bit Long
167
 */
166
 */
168
long Parser::dataToLong(int *Data , int Start, bool is_signed)
167
long Parser::dataToLong(char *Data , int Start, bool is_signed)
169
{
168
{
170
    long Out = (Data[Start+3]<<24) | (Data[Start+2]<<16) | (Data[Start+1]<<8) | (Data[Start+0]);
169
    long Out = (Data[Start+3]<<24) | (Data[Start+2]<<16) | (Data[Start+1]<<8) | (Data[Start+0]);
171
 
170
 
172
    if ((Out > 32767) && (is_signed))
171
    if ((Out > 32767) && (is_signed))
173
      Out = Out;
172
      Out = Out;
174
 
173
 
175
    return Out;
174
    return Out;
176
}
175
}
177
 
176
 
-
 
177
std::string Parser::dataToString(char * data, int start, int end)
-
 
178
{
-
 
179
    char tmp[MAX_DATA_SIZE];
-
 
180
    for (int i = start; i < end; i++)
-
 
181
        tmp[i-start] = data[i];
-
 
182
    return std::string(tmp);
-
 
183
}
-
 
184
 
178
float Parser::getFloat(long value, int count)
185
float Parser::getFloat(long value, int count)
179
{
186
{
180
    long num = pow(10, count);
187
    long num = pow(10, count);
181
 
188
 
182
    float temp = value;
189
    float temp = value;
Line 228... Line 235...
228
        tmpCRC += tx[i];
235
        tmpCRC += tx[i];
229
    }
236
    }
230
 
237
 
231
    tmpCRC %= 4096;
238
    tmpCRC %= 4096;
232
 
239
 
233
    tx[length-2] = '=' + tmpCRC / 64;
240
    tx[length] = '=' + tmpCRC / 64;
234
    tx[length-1] = '=' + tmpCRC % 64;
241
    tx[length+1] = '=' + tmpCRC % 64;
235
    tx[length] = '\0';
242
    tx[length+2] = '\0';
236
}
243
}
237
 
244