dqef7931 2018-05-13 16:06
浏览 149
已采纳

使用go-serial从arduino串行端口读取

I have arduino uno with simple firmware which provides simple API over serial port:

  • Command "read" returns the current state
  • Command "on" sets the state to "on"
  • Command "off" sets the state to "off"

Now I want to implement a client for this device. If I use Arduino IDE serial monitor, this API works as expected. If I use python with pySerial library, API works.

But whenever I try to read data from the serial port using golang and go-serial, my read calls hangs (but works fine with /dev/pts/X created by socat, for example)

Python client

import serial
s = serial.Serial("/dev/ttyACM0")
s.write("read
")
resp = []
char = None
while char != "":
    char = s.read()
    resp.append(char)
print "".join(resp)

Go client (hangs on Read call forever): package main

import "fmt"
import "github.com/jacobsa/go-serial/serial"

func check(err error) {
    if err != nil {
        panic(err.Error())
    }
}

func main() {
    options := serial.OpenOptions{
        PortName:        "/dev/ttyACM0",
        BaudRate:        19200,
        DataBits:        8,
        StopBits:        1,
        MinimumReadSize: 4,
    }
    port, err := serial.Open(options)
    check(err)
    n, err := port.Write([]byte("read
"))
    check(err)
    fmt.Println("Written", n)
    buf := make([]byte, 100)
    n, err = port.Read(buf)
    check(err)
    fmt.Println("Readen", n)
    fmt.Println(string(buf))
}

Firmware code:

String inputString = "";         // a String to hold incoming data
boolean stringComplete = false;  // whether the string is complete
String state = "off";

void setup() {
    // initialize serial:
    Serial.begin(9600);
    // reserve 200 bytes for the inputString:
    inputString.reserve(200);
    pinMode(13, OUTPUT);
}

void loop() {
    // print the string when a newline arrives:
    if (stringComplete) {
        blink();
        if (inputString == "on
") {
        state = "on";  
        } else if (inputString == "off
") {
        state = "off";  
        } else if (inputString == "read
") {
        Serial.println(state  );  
        }
        // clear the string:
        inputString = "";
        stringComplete = false;
    }
}


void blink() {
    digitalWrite(13, HIGH);   // set the LED on
    delay(1000);              // wait for a second
    digitalWrite(13, LOW);    // set the LED off
    delay(1000);              // wait for a second
}

void serialEvent() {
    while (Serial.available()) {
        // get the new byte:
        char inChar = (char)Serial.read();
        // add it to the inputString:
        inputString += inChar;
        // if the incoming character is a newline, set a flag so the main loop can
        // do something about it:
        if (inChar == '
') {
        stringComplete = true;
        }
    }
}

Python code

  • 写回答

1条回答 默认 最新

  • dongxian3852 2018-05-14 12:46
    关注

    You have set the baud rate for the Go lang function to 19200, but in the arduino you have used 9600.

    In the python code, the baud rate is not set, so it takes the default of 9600.

    Just set the right baud rate in your go lang program, and it should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog