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
}
Line 37... Line 36...
37
 
36
 
38
 
37
 
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
}
Line 131... Line 130...
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) {
Line 134... Line 133...
134
    int out = (data[start]);
133
    int out = (data[start]);
135
 
134
 
Line 149... Line 148...
149
}
148
}
Line 150... Line 149...
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
{
Line 156... Line 155...
156
    int Out = (Data[Start+1]<<8) | (Data[Start+0]);
155
    int Out = (Data[Start+1]<<8) | (Data[Start+0]);
157
 
156
 
Line 163... Line 162...
163
}
162
}
Line 164... Line 163...
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
{
Line 170... Line 169...
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
 
Line 172... Line 171...
172
    if ((Out > 32767) && (is_signed))
171
    if ((Out > 32767) && (is_signed))
173
      Out = Out;
172
      Out = Out;
Line -... Line 173...
-
 
173
 
-
 
174
    return Out;
-
 
175
}
-
 
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++)
174
 
181
        tmp[i-start] = data[i];
175
    return Out;
182
    return std::string(tmp);
176
}
183
}
Line 177... Line 184...
177
 
184
 
Line 228... Line 235...
228
        tmpCRC += tx[i];
235
        tmpCRC += tx[i];
229
    }
236
    }
Line 230... Line 237...
230
 
237
 
Line 231... Line 238...
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;