go在arm上读取串口数据

转:https://blog.csdn.net/xysq/article/details/51451769

最近在imx6上开程序使用go作为后台服务程序,需要用到读取串口数据,通过cgo用c来读取取串口数据

package main
/*
#cgo LDFLAGS: lib/testrs232.a
#include "lib/testrs232.h"

int opendev(char *buf)
{
    int ret = openDevice(buf);
    return ret;
}

int writedev(char *buf,int writesize)
{ int ret = writeDevice(buf,writesize);
return ret;
}

int setPara(enum EBAUD baud, enum EDATABIT databit, enum EPARITYBIT  paritybit
                 ,enum ESTOPBIT stopbit)
{
setParameter(baud,databit,paritybit,stopbit);
}

int readData(char *buf,int bufsize)
{
return read_port(buf,bufsize);
}
*/

import "C"
在上面调用中,/**/内的是c代码,后面紧跟着import "C"
例如:go里要使用上面的函数 C.opendev(C.CString("/dev/ttymxc2"))
在linux下成功编译,并能读取串口的值,但想编译到arm平台的板子上,总是显示错误,后来在liteIDE里,编辑当前环境,将 CGO_ENABLED=1,默认 CGO_ENABLED=0,再编译,还是出现错误,找不到编译器,后来设置环境变量CC,设置为板子的工具链 CC=arm-fsl-linux-gnueabi-gcc,编译成功

猜你喜欢

转载自blog.csdn.net/ztx01001/article/details/88980193