本文主要是介绍HISI3519上的RS485 C++ 读写接口/linux中RS485读写接口文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
1.猜测可能的接线方式
2.dataType.h
3.rs485Service.h
4.rs485Service.cpp
5.接口调用demo
以下为HISI3519上面的RS485读写接口文件
1.猜测可能的接线方式
在rs485Service.cpp里面的init函数里面可以看到有设置引脚的方向和高低电平的代码,猜测这个设置引脚高低电平的代码是485芯片的使能引脚,用来控制485芯片的,可以看到0是read enable,1是发送使能。
2.dataType.h
#ifndef DATE_TYPE_H
#define DATE_TYPE_H#include <stdio.h>typedef unsigned char ubyte;
typedef char BYTE;
typedef unsigned short ushort;
typedef unsigned int uint32;#endif
3.rs485Service.h
#ifndef __RS485_SERVER_H__
#define __RS485_SERVER_H__#include "dataType.h"typedef enum
{OPEN_485_SUCCESS = 0,OPEN_485_FAIL,
}Open485State;class Rs485Service
{private:Rs485Service();~Rs485Service();public:static Rs485Service& Get();public:int Rs485Read(ubyte *buf, uint32 size);int Rs485Write(const ubyte *data, uint32 len);int InitRs485Dev(uint32 bound);void UninitRs485Dev();private:int m_devFd;int m_bound;int32_t m_485WriteState; //true 485处于写状态
};#endif
4.rs485Service.cpp
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <netinet/in.h>
#include "rs485Service.h"#define RS485COM "/dev/ttyAMA1"Rs485Service::Rs485Service()
{m_devFd = -1;
}Rs485Service::~Rs485Service()
{
}Rs485Service& Rs485Service::Get()
{static Rs485Service Rs485server;return Rs485server;
}int Rs485Service::InitRs485Dev(uint32 bound)
{system("himm 0x1204008C 1");system("himm 0x12040094 1");system("himm 0x12040090 0");system("echo 42 > /sys/class/gpio/export");system("echo out > /sys/class/gpio/gpio42/direction");system("echo 0 > /sys/class/gpio/gpio42/value");//如果是NVIDIA Jetson上面直接用下面的命令,不用himm了,himm是海思的。//int error = system("echo su");//error = system("echo 421 >/sys/class/gpio/export");//这个命令只执行一次就好了(只执行一次是从开机开始算),第二次会提示I/O error,但是不影响,还是能正常读写。//error = system("echo out >/sys/class/gpio/gpio421/direction");//error = system("echo 0 >/sys/class/gpio/gpio421/value");//将管脚拉高后才能发送数据。m_devFd = open(RS485COM, O_RDWR | O_NOCTTY | O_NDELAY); //加上O_NDELAY,就变为了非阻塞if (m_devFd != -1){struct termios cfg;memset(&cfg, 0, sizeof(cfg));tcgetattr(0, &cfg);switch(bound){case 19200:cfsetispeed(&cfg, B19200);cfsetospeed(&cfg, B19200);m_bound = 19200;break;case 9600:cfsetispeed(&cfg, B9600);cfsetospeed(&cfg, B9600);m_bound = 9600;break;default:cfsetispeed(&cfg, B19200);cfsetospeed(&cfg, B19200);m_bound = 19200;break;}cfg.c_oflag &= ~(ONLCR);cfg.c_oflag &= (OCRNL);cfg.c_iflag &= (INLCR);cfg.c_cflag |= CLOCAL | CREAD; //使能串口输入//cfg.c_lflag |= ICANON; //标准模式cfg.c_lflag &= ~ICANON;//原始模式cfg.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);//8bit数据cfg.c_cflag &= ~CSIZE;cfg.c_cflag |= CS8;//1bit停止位cfg.c_cflag &= ~CSTOPB;//无校验cfg.c_cflag &= ~PARENB;//禁用硬件流控制:cfg.c_cflag &= ~CRTSCTS;// cfg.c_cc[VTIME] = 1; //设置超时时间,如果采用非阻塞模式则不设置cfg.c_cc[VMIN] = 1; //设置最小接收的数据长度//清楚输入输出缓冲区tcflush(m_devFd, TCIOFLUSH);tcsetattr(m_devFd, TCSANOW, &cfg);return OPEN_485_SUCCESS;}else{printf("open dev err.\n");return OPEN_485_FAIL;}
}void Rs485Service::UninitRs485Dev()
{if (m_devFd != -1){struct termios cfg;memset(&cfg, 0, sizeof(cfg));tcgetattr(0, &cfg);cfg.c_cc[VTIME] = 1;tcflush(m_devFd, TCIOFLUSH);tcsetattr(m_devFd, TCSANOW, &cfg);close(m_devFd);}
}int Rs485Service::Rs485Read(ubyte* buf, uint32 size)
{int rlen = -1;if (m_devFd != -1){rlen = read(m_devFd, buf, size);tcflush(m_devFd, TCIOFLUSH);}return rlen;
}int Rs485Service::Rs485Write(const ubyte* data, uint32 len)
{int wlen = -1;//485是半双工,置为发送状态system("echo 1 > /sys/class/gpio/gpio42/value"); if (m_devFd != -1){ wlen = write(m_devFd, data, len);}//等待数据输出完毕tcdrain(m_devFd);//清空输入输出缓冲区tcflush(m_devFd, TCIOFLUSH);//485是半双工,置为接收状态system("echo 0 > /sys/class/gpio/gpio42/value");return wlen;
}
5.接口调用demo
这篇关于HISI3519上的RS485 C++ 读写接口/linux中RS485读写接口文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!