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

使用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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化