Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何只检查一次输入,而不挂断等待输入_C_Arrays_Input_Uart_Mbed - Fatal编程技术网

C 如何只检查一次输入,而不挂断等待输入

C 如何只检查一次输入,而不挂断等待输入,c,arrays,input,uart,mbed,C,Arrays,Input,Uart,Mbed,所以我用mbed编译器编程,这是我目前的代码 pc.baud(9600); dev1.baud(19200); char command[5]; //pc.puts("Initialized1X\n"); //while(1) { if (1) { dev1.gets(command, 6); //this is causing my error...just waiting for command, must fix if (command[0] != 0x68){

所以我用mbed编译器编程,这是我目前的代码

pc.baud(9600);
dev1.baud(19200);
char command[5];
//pc.puts("Initialized1X\n");
//while(1) {
if (1) {
    dev1.gets(command, 6); //this is causing my error...just waiting for command, must fix
    if (command[0] != 0x68){
        for (int x=0; x<4; x++){
            command[x] = command[x+1];
        }
        command[4] = 0x16;
    }
    for (int i=0; i<5; i++) {
            pc.printf("%x ", command[i]);
    }
        pc.printf("\n");
    sampleRates = command[2];    
    if (command[1] == 0x01){    
        switch (sampleRates){
            case 0x01: pc.printf("44.1k\n");
            return; 
            case 0x02: pc.printf("48k\n");
            return;
            case 0x03: pc.printf("88.2k\n");
            return;
            case 0x04: pc.printf("96k\n");
            return;
            case 0x05: pc.printf("176.4k\n");
            return;
            case 0x06: pc.printf("192k\n");
            return;
            case 0x07: pc.printf("352k\n");
            return;
            case 0x08: pc.printf("384k\n");
            return;
            case 0x09: pc.printf("705.6k\n");
            return;
            case 0x10: pc.printf("768k\n");
            return;

        }
    }
}
//}

使用dev1.getscommand,6;我正在从另一个设备获取输入。除了需要向程序中添加其他函数外,代码工作正常。其他函数不会运行,因为它只是等待dev.1gets命令的输入。有没有办法让它快速检查,接受输入,然后继续?我在dev1.readable中使用了if语句;但是它在我再次执行之前不会将命令放入数组,因为它只进入循环。也许有一种方法可以同时检查和保存

通过中断将数据输入缓冲区

使用双缓冲并保留一个标志,指示特定缓冲区是否包含“new”“complete”命令,这可能是一个好主意 正在等待处理

中断处理程序维护循环缓冲区并处理实际的I/O

让中断处理程序足够智能,以识别何时输入了完整的“命令”

当输入完整的“命令”时,中断设置特定缓冲区的标志,主程序可以查看/重置该标志,以确定是否有“命令”可供处理

需要保护上述标志,以便在读取/重置标志时使用互斥或让主功能禁用中断