usb2can连接golang

时间:2018-10-03 15:13:45

标签: go exec can-bus socketcan

我正在尝试设置一个读取canbus数据输出的车载PC。我希望计算机自动设置usb2serial设备。

我的问题是来自SocketCAN的命令: sudo ip link set up can0 type can bitrate 125000

我无法使连接正常工作。 在终端中执行命令时,它会提示链接成功,但是当go代码运行时,它仅返回can't find script fork/exec

func Can_connect() error {
    var err error

    if helpers.Can_bitrate == 0 {
        log.Println("can bitrate = 0")
        err = errors.New("can bitrate = 0")
        return err
    }

    //sudo ip link set up can0 type can bitrate 125000
    obd_connect := exec.Command("bash", "-c", "sudo ip link set up can0 type can bitrate "+string(helpers.Can_bitrate))
    obd_connect.Run()

    obd_output, err := obd_connect.Output()
    if err != nil {
        log.Println("Error reading output from connecting to obd", err)
        return err
    }

    i, err := strconv.Atoi(string(obd_output[:len(obd_output)-1]))
    if err != nil {
        log.Println(err)
    }

    if i == 0 {
        log.Println("successful confection to obd")
    } else {
        log.Println("unable to setup obd")
    }

    return nil
}

0 个答案:

没有答案