0

たとえば、このコード ブロック全体をコマンドに入力したいとします。

int k = 0;

for (k = 0; k < 50; k++) 
{
    sprintf(buf, "M LR 10 -10\n");                     //We put the string "M L 10" into the string buffer.
    write(sock, buf, strlen(buf));                     //We send the buffer into the socket.
    memset(buf, 0, 80);                                //Clear the buffer, set buffer to value 0.
    read(sock, buf, 80);                               //Read from the socket to get the results.
    int lme, rme;
    sprintf(buf, "S MELR\n");                          //sensor command to find ME values
    write(sock, buf, strlen(buf));                     //sends the buffer to the socket
    memset(buf, 0, 80);                                //Clear the buffer, set buffer to value 0.
    read(sock, buf, 80);                               //read from socket to get results.
    sscanf(buf, "S MELR %i %i\n", &lme, &rme);         //takes lme and rme values from results
    printf(buf, "%3i   %-4i\n", lme, rme);
    //distance = 2 * (22/7) * r
}

for (k = 50; k < 51; k++) 
{
    sprintf(buf, "C RME\n");                           //We put the string "C RME" into the string buffer to reset.
    write(sock, buf, strlen(buf));                     //We send the buffer into the socket.
    memset(buf, 0, 80);                                //Clear the buffer, set buffer to value 0.
    read(sock, buf, 80);                               //Read from the socket to get the results.
}

{sprintf(buf, "M LR 10 -10\n");}これにより、 ie10との文字列の値を変更するだけで-10、残りのプロセスは自動的に実行されます。

たとえば、set_motor_speed(10 -10\n)メイン コードで関数全体を実行する場合、どうすればよいでしょうか?

4

1 に答える 1

5

他の人が言ったように: C 言語の本でこの種のことを読むことができますが、私たちは親切です:

set_motor_speed(int a, int b) {
    ...
    for(k = 0; k < 50; k++) {
        sprintf(buf, "M LR %i %i\n", a, b);
        ...
    }
    ...
}

set_motor_speed(10, -10);
set_motor_speed(5, -5);
于 2013-02-14T14:41:32.467 に答える