modbus代码移植
modbus三方库代码:
"libmodbus-ok.tar.gz"
将modbus三方库代码移植到3568t代码里,路径为:/MH_1107/third_party
修改json文件,将modbus三方库代码编进3568t代码里
路径:/MH_1107/build/subsystem_config.json
"modbus": {
"path": "third_party/libmodbus",
"name": "modbus"
}
路径:/MH_1107/productdefine/common/products/rk3568.json
"modbus:libmodbus":{}
附:测试代码
#include<stdio.h> /*标准输入输出定义*/
#include<stdlib.h> /*标准函数库定义*/
#include<unistd.h> /*Unix 标准函数定义*/
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h> /*文件控制定义*/
#include<termios.h> /*PPSIX 终端控制定义*/
#include<errno.h> /*错误号定义*/
#include<string.h>
//#include <wiringPi.h>
//宏定义
#define FALSE -1
#define TRUE 0
int UART0_Open(int fd,char* port){
fd = open( port, O_RDWR|O_NOCTTY|O_NDELAY);
if (FALSE == fd){
perror("Can't Open Serial Port");
return(FALSE);
}
//恢复串口为阻塞状态
if(fcntl(fd, F_SETFL, 0) < 0){
printf("fcntl failed!\n");
return(FALSE);
}else{
printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));
}
//测试是否为终端设备
if(0 == isatty(STDIN_FILENO)){
printf("standard input is not a terminal device\n");
return(FALSE);
}else{
printf("isatty success!\n");
}
printf("fd->open=%d\n",fd);
return fd;
}
void UART0_Close(int fd){
close(fd);
}
int UART0_Set(int fd,int speed,int flow_ctrl,int databits,int stopbits,int parity){
int i;
int speed_arr[] = { B115200, B38400,B19200, B9600, B4800, B2400, B1200, B300};
int name_arr[] = {115200, 38400,19200, 9600, 4800, 2400, 1200, 300};
struct termios options;
/*tcgetattr(fd,&options)得到与fd指向对象的相关参数,并将它们保存于options,该函数还可以测试配置是否正确,该串口是否可用等。若调用成功,函数返回值为0,若调用失败,函数返回值为1.
*/
if( tcgetattr( fd,&options) != 0){
perror("SetupSerial 1");
return(FALSE);
}
//设置串口输入波特率和输出波特率
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++){
if (speed == name_arr[i]){
cfsetispeed(&options, speed_arr[i]);
cfsetospeed(&options, speed_arr[i]);
}
}
//修改控制模式,保证程序不会占用串口
options.c_cflag |= CLOCAL;
//修改控制模式,使得能够从串口中读取输入数据
options.c_cflag |= CREAD;
//设置数据流控制
switch(flow_ctrl){
case 0 ://不使用流控制
options.c_cflag &= ~CRTSCTS;
break;
case 1 ://使用硬件流控制
options.c_cflag |= CRTSCTS;
break;
case 2 ://使用软件流控制
options.c_cflag |= IXON | IXOFF | IXANY;
break;
}
//设置数据位
//屏蔽其他标志位
options.c_cflag &= ~CSIZE;
switch (databits){
case 5:
options.c_cflag |= CS5;
break;
case 6:
options.c_cflag |= CS6;
break;
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return (FALSE);
}
//设置校验位
switch (parity){
case 'n':
case 'N': //无奇偶校验位。
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
case 'O'://设置为奇校验
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E'://设置为偶校验
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 's':
case 'S': //设置为空格
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return (FALSE);
}
// 设置停止位
switch (stopbits){
case 1:
options.c_cflag &= ~CSTOPB; break;
case 2:
options.c_cflag |= CSTOPB; break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (FALSE);
}
//修改输出模式,原始数据输出
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);//我加的
//options.c_lflag &= ~(ISIG | ICANON);
//设置等待时间和最小接收字符
options.c_cc[VTIME] = 1; /* 读取一个字符等待1*(1/10)s */
options.c_cc[VMIN] = 1; /* 读取字符的最少个数为1 */
//如果发生数据溢出,接收数据,但是不再读取 刷新收到的数据但是不读
tcflush(fd,TCIFLUSH);
//激活配置 (将修改后的termios数据设置到串口中)
if (tcsetattr(fd,TCSANOW,&options) != 0){
perror("com set error!\n");
return (FALSE);
}
return (TRUE);
}
int UART0_Init(int fd, int speed,int flow_ctrl,int databits,int stopbits,int parity)
{
//设置串口数据帧格式
if (UART0_Set(fd,speed,0,8,1,'N') == FALSE){
return FALSE;
}else{
return TRUE;
}
}
int modbus_test(int delay, char* rcv_buf, int rcv_size, unsigned char* send_buf, int send_size, int speed){
int fd;
int fd_1; //文件描述符
int err; //返回调用函数的状态
int len;
int i;
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); //打开串口
do{
err = UART0_Init(fd,speed,0,8,1,'N');
printf("Set Port Exactly!\n");
}while(FALSE == err || FALSE == fd);
fd_1 = open("/sys/class/gpio/gpio88/value", O_RDWR);
write(fd_1, "1", 1);
close(fd_1);
len = write(fd,send_buf,send_size);
if(len > 0){
printf("send data successful\n");
printf("len = %d\n",len);
}else{
printf("send data failed!\n");
UART0_Close(fd);
return -2;
}
usleep(delay);
fd_1 = open("/sys/class/gpio/gpio88/value", O_RDWR);
write(fd_1, "0", 1);
close(fd_1);
while(1){
len = read(fd,rcv_buf,rcv_size);
if(len < 0){
continue;
}
printf("len = %d\n",len);
for (i=0; i<rcv_size; i++) {
printf("%02x ", rcv_buf[i]);
}
printf("%s:%d\n", __func__, __LINE__);
break;
}
UART0_Close(fd);
return 0;
}
int main(int argc, char **argv){
int delay = 0;
delay = atoi(argv[1]);
int rcv_size= 16;
int speed= 9600;
unsigned char send_buf[]= {0x01, 0x04, 0x00, 0x00, 0x00, 0x01, 0x31, 0xCA};
char rcv_buf[rcv_size];
memset(rcv_buf,0,sizeof(char) * rcv_size);
int send_size=sizeof(send_buf);
printf("send_size = %d\n",send_size);
while (1){
int res = modbus_test(delay,rcv_buf,rcv_size,send_buf,send_size,speed);
printf("res = %d\n",res);
sleep(1);
}
return 0;
}