linux - 我无法通过 USB 串行连接向我的 Arduino 发送数据

标签 linux arduino arduino-uno serial-communication usbserial

所以,我有一个 arduino uno R3 SMD 版本,我想在它运行时向它发送命令。它有一个连接到它的 adafruit v2 电机屏蔽,它与 arduino 分开供电。 arduino 通过 USB 电缆连接到我的笔记本电脑。

现在,电机护罩可以工作了,我可以将代码发送到 arduino 以使其执行操作。但是,当它从串行连接运行时,我无法让 arduino 接收我发送给它的任何东西。不过,我可以从 arduino 代码打印到 arduino ide 中的显示器。我在 debian linux stretch 顺便说一句。我的 arduino ide 来自 debian 仓库。

这是我尝试使用的所有代码:

Arduino代码

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

void setup()
{
        Serial.begin(115200);
        AFMS.begin();
        myMotor->setSpeed(300);
}

char input;
int dir;

void loop()
{
        dir = 0;
        if (Serial.available() > 0) {
                input = Serial.read();
                Serial.print(input);

                if (input == 1) {
                        dir = FORWARD;
                }
                if (input == 2) {
                        dir = BACKWARD;
                }
        }

        if (dir != 0) {
                myMotor->step(360, dir, DOUBLE);
                delay(1000);
        }
}

笔记本电脑上的 Controller 代码

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

int
main(int argc, char **argv)
{
        FILE *arduino;
        int c;

        arduino = fopen("/dev/ttyACM0", "w");

        if (arduino == NULL) {
                fprintf(stdout, "NOOOOOOOOOOOO\n");
        }

        while (1) {
                c = fgetc(stdin);

                if (c == 'f') {
                        fprintf(arduino, "%d", 1); 
                }
                if (c == 'b') {
                        fprintf(arduino, "%d", 2); 
                }

                fflush(arduino);

                if (c == 'q') {
                        break;
                }
        }

        return 0;
}

我很确定这不是权限问题,我已经从 root 运行 Controller 代码并且 tty 设备打开正常。此外,我已经尝试了 9600 和 115200 的波特率,但没有骰子。有人有想法吗?通过谷歌搜索,似乎其他人都在这样做。

最佳答案

您的 Controller 正在发送字符 '1''2'。您的 Arduino 正在检查字符代码 12 - 换句话说,字符 CtrlA CtrlB。您可以在任一端进行更改,它们只需要匹配即可。

关于linux - 我无法通过 USB 串行连接向我的 Arduino 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43360068/

相关文章:

android - 当从 Arduino 收到数据时,Textview 值不会更新

linux - 使用awk和grep相加

php - lighttpd php5 socket_connect() 权限被拒绝

python - GTK 状态图标 : Coordinates of left-click?

linux - Solaris -> Linux 迁移上的 Solaris 特定功能

c++ - 用 <> 传递参数?

java - 将串行通信数据添加到实时 Java 图中

PHP未定义索引使用arduino到mySQL

c - LED 8x8 的程序在 Arduino Uno 中不起作用。我没有使用 pinMode() 方法。我正在使用另一种方法进行规范

arduino - Serial.print() 使用什么类型的变量?