c - 在 C 中使用 popen() 和 minicom 会产生垃圾值

标签 c arduino popen raspberry-pi

我正在制作一个脚本来获取arduino通过USB串行在树莓派(debian wheezy)上的串行(serial.write)发送的值。这些值由接近电容传感器(capsense 库)发送。当我使用此命令打开终端窗口时

minicom -b 9600 -o -D /dev/ttyACM0

我(几乎)得到了我想要的,随着我的手的接近而使用react的字符行;所以发送部分工作得很好,树莓派的接收部分也工作得很好。 因此,我在我的 C 代码中使用 popen() 以 4 个 block 为单位获取这些值(因为有 2 个传感器值,以空格分隔),但是当我执行该程序时,我只得到垃圾值,这些值似乎在任何情况下都没有响应靠近我的手。 此外,当我启动程序时,我无法通过使用 ctrl+c 或 ctrl+z 来停止它。

这是我的 C 程序:

#include <stdio.h>
#include <stdlib.h>

int main (void) {

    // open a stream with popen with the (working) command    
    FILE *stream = popen("minicom -b 9600 -o -D /dev/ttyACM0", "r");

    while (!feof(stream) && !ferror(stream)) {

        // declare a buffer, read the stream
        char buf[4];
        int bytesRead = fread(buf, 1, 4, stream);

        // print stuff to see what has been read
        int i;
        for(i = 0; i < sizeof(buf); i++) {
            printf("%i", (int)buf[i]);
        }

        printf("\n");
    }

    pclose(stream);
    return 0;
}

这是我的 arduino 上的程序:

#include <CapacitiveSensor.h>

// sensors
CapacitiveSensor vibeCS = CapacitiveSensor(4,2);
CapacitiveSensor gongCS = CapacitiveSensor(8,7);

// note values
long vibe;
long gong;

void setup() {

  // pin modes
  pinMode(12, OUTPUT);
  pinMode(13, INPUT);

  Serial.begin(9600);
}

void loop() {

    vibe = vibeCS.capacitiveSensor(150);
    gong = gongCS.capacitiveSensor(150);

    // communication
    Serial.write(vibe);
    Serial.write(' ');
    Serial.write(gong);
    Serial.write('\n');

    delay(10);
}

知道为什么会发生这种情况吗?

我还一直在研究另一种看待问题的方法,如下所示:

#include <stdio.h>
#include <stdlib.h>
#include "rs232.h"

#define COMPORT         0x0000      // see explanation below
#define BAUDRATE        9600
#define RECEIVE_CHARS   4

int main (void) {

    // declare a buffer
    unsigned char receive_buffer[RECEIVE_CHARS];

    // open the serial communication
    RS232_OpenComport(COMPORT, BAUDRATE);

    while(1) {

        // poll data from arduino
        RS232_PollComport(COMPORT, receive_buffer, RECEIVE_CHARS);

        // print stuff 
        int i;
        for(i = 0; i < sizeof(receive_buffer); i++) {
            printf("%i", (int)receive_buffer[i]);
        }
        printf("\n");
    }

   RS232_CloseComport(COMPORT);
   return 0;
}

在这样做的过程中,我使用了 0x0000 作为comport,但我认为这不是正确的。我这样设置是因为

setserial -g /dev/ttyACM0

给我

/dev/ttyACM0, UART: unknown, Port: 0x0000, IRQ: 0, Flags: low_latency

因此,如果这在终端中为我提供了漂亮的数据,即使我的手靠近传感器,它也只给出 0000,所以它似乎没有接收到数据。

谢谢

最佳答案

vibeCS.capacitiveSensor(  ) 

返回 long(4 字节)而不是 int(2 字节)。然而它必须被重载,因为它可以让你编译。

http://playground.arduino.cc//Main/CapacitiveSensor?from=Main.CapSense

2 您可能需要检查您的处理器,看看它是大端还是小端。

编辑: 我怀疑下面的代码没有执行您想要的操作。

    int i;
    for(i = 0; i < sizeof(receive_buffer); i++) {
         i = 
         printf("%i", (int)receive_buffer[i]);
    }

例如,假设 buff 中有一个 int。 int 的值为 4242; 4242 的十六进制值为 [00 00 10 92]

所以 buf[0] = 0x92 = 147 ,

buf[1] = 0x10 = 16,

buf[2] = 0x0 = 0,

buf[3] = 0x0 = 0;

因此,您的代码将打印 1471600 作为值 4242。下面的代码显示了我正在谈论的内容。

1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 int main()
5 {
6         unsigned char buf[4];
7         unsigned int *fill = buf;
8         *fill = 4242;
9 
10         int i;
11         for(i = 0; i < sizeof(buf); i++) {
12             printf("%i", (unsigned int)buf[i]);
13         }
14 
15 }

printed value is [1461600]

现在来一些有用的东西。下面的代码可能会解决您的所有问题。

#include <stdio.h>
#include <stdlib.h>

int main (void) {

    // open a stream with popen with the (working) command    
    FILE *stream = popen("minicom -b 9600 -o -D /dev/ttyACM0", "r");

    while (!feof(stream) && !ferror(stream)) {

        // declare a buffer, read the stream
        char buf[4];
        int bytesRead = fread(buf, 1, 4, stream);

        // print stuff to see what has been read
        int *i = buf;
        printf("%i", *i);
        //for(i = 0; i < sizeof(buf); i++) {
        //   printf("%i", (int)buf[i]);
        //}

        printf("\n");
    }

    pclose(stream);
    return 0;
}

关于c - 在 C 中使用 popen() 和 minicom 会产生垃圾值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17271738/

相关文章:

c - 如何修复此代码(使用 c 中的 char 将二进制转换为十进制)?

python - 如何从 python (2.5) 中的 subprocess.Popen 获取 'real-time' 信息

Python:一个线程中的进程阻止另一个线程中的进程完成

c - 如何计算在 FOR 循环中检查条件的次数

c - 从代码中断程序中删除 printf 语句

c - 如何允许使用 scanf 输入空格?

arduino - arduino可以使用sim900通过语音通话告诉室温吗?

android - 使用 MIT App Inventor 2 扫描蓝牙设备

arduino - 在arduino中将十六进制字符串转换为十进制

javascript - 如何防止子 Node 进程被父 Node 进程杀死?