最近在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,编译成功。