添加rs485中间件
authorzwj <645281962@qq.com>
Fri, 2 Jun 2023 05:47:26 +0000 (13:47 +0800)
committerzwj <645281962@qq.com>
Fri, 2 Jun 2023 05:47:26 +0000 (13:47 +0800)
485/CMakeLists.txt [new file with mode: 0644]
485/inc/485.h [new file with mode: 0644]
485/src/485.c [new file with mode: 0644]

diff --git a/485/CMakeLists.txt b/485/CMakeLists.txt
new file mode 100644 (file)
index 0000000..0d6341f
--- /dev/null
@@ -0,0 +1,18 @@
+cmake_minimum_required(VERSION 3.8)
+
+project(mg_485)
+
+include_directories(
+       ${PROJECT_SOURCE_DIR}/./
+       ${PROJECT_SOURCE_DIR}/src/
+       ${PROJECT_SOURCE_DIR}/inc/
+       ${CMAKE_SYSROOT}/usr/include/
+       )
+
+aux_source_directory(./src/ SRCS)
+
+add_library( ${PROJECT_NAME} STATIC  ${SRCS} )
+#add_library( ${PROJECT_NAME}  ${SRCS} )
+#install( TARGETS ${PROJECT_NAME} RUNTIME DESTINATION /app/lib )
+#install( TARGETS ${PROJECT_NAME} LIBRARY DESTINATION /app/lib )
+#install( TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION /app/lib )
diff --git a/485/inc/485.h b/485/inc/485.h
new file mode 100644 (file)
index 0000000..a4e7220
--- /dev/null
@@ -0,0 +1,46 @@
+#ifndef _485_H_
+#define _485_H_
+
+/*数据位长度*/
+enum{
+       DATA_BITS_5 = 5,
+       DATA_BITS_6,
+       DATA_BITS_7,
+       DATA_BITS_8,
+};
+
+/*停止位长度*/
+enum{
+       STOP_BITS_1 = 1,
+       DATA_BITS_2 = 2,
+};
+
+/*校验位*/
+enum{
+       PARITY_NONE=0,
+       PARITY_ODD,
+       PARITY_EVEN,
+};
+
+struct mg_eia485{
+    char *dev_path;
+    int uartFd;
+    char *ctl_path;
+    int ctlFd;
+};
+typedef struct mg_eia485 mg_eia485_t;
+
+/*打开EIA485控制器*/
+mg_eia485_t * mg_eia485_open(uint32_t baud, uint32_t data_bits, uint32_t stop_bits, uint32_t parity );
+
+/*关闭控制器*/
+void mg_eia485_close( mg_eia485_t * eia485_ctrl );
+
+/*发送数据*/
+int32_t mg_eia485_write(mg_eia485_t * eia485_ctrl, uint8_t * tx_data, uint32_t tx_size );
+
+/*接收数据*/
+int32_t mg_eia485_read( mg_eia485_t * eia485_ctrl,uint8_t * rx_data, uint32_t rx_size, uint32_t timeout_ms );
+
+
+#endif
\ No newline at end of file
diff --git a/485/src/485.c b/485/src/485.c
new file mode 100644 (file)
index 0000000..8a331ef
--- /dev/null
@@ -0,0 +1,387 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <termios.h>
+#include <string.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <pthread.h>
+
+#include "485.h"
+
+#define UART_DEVICE "/dev/ttyS5" // UART设备文件路径
+#define CTL_DEVICE "/dev/rs485_txen" // UART设备文件路径
+#define FIFO_SIZE 50            // FIFO队列大小
+
+typedef struct {
+    unsigned char* buffer;
+    int capacity;
+    int head;
+    int tail;
+    int count;
+} FifoQueue;
+
+int time_flag = 0;
+int uartFd = -1;
+int stop = -1;
+FifoQueue receiveQueue;
+
+mg_eia485_t *my_485 = NULL;
+
+
+/*初始化fifo*/
+int initFifoQueue(FifoQueue* queue, int capacity) {
+    queue->buffer = (unsigned char*)malloc(capacity * sizeof(unsigned char));
+    if (queue->buffer == NULL) {
+        perror("Failed to allocate memory for FIFO queue");
+        return 0;
+    }
+
+    queue->capacity = capacity;
+    queue->head = 0;
+    queue->tail = 0;
+    queue->count = 0;
+
+    return 1;
+}
+
+/*销毁fifo*/
+void destroyFifoQueue(FifoQueue* queue) {
+    if (queue->buffer != NULL) {
+        free(queue->buffer);
+        queue->buffer = NULL;
+    }
+    queue->capacity = 0;
+    queue->head = 0;
+    queue->tail = 0;
+    queue->count = 0;
+}
+
+/*判断fifo是否已满*/
+int isFifoQueueFull(const FifoQueue* queue) {
+    return (queue->count == queue->capacity);
+}
+
+/*判断fifo是否为空*/
+int isFifoQueueEmpty(const FifoQueue* queue) {
+    return (queue->count == 0);
+}
+
+/*往fifo中写数据*/
+int writeToFifoQueue(FifoQueue* queue, unsigned char data) {
+    if (isFifoQueueFull(queue)) {
+        return 0;
+    }
+
+    queue->buffer[queue->tail] = data;
+    queue->tail = (queue->tail + 1) % queue->capacity;
+    queue->count++;
+
+    return 1;
+}
+
+/*读取fifo的数据*/
+unsigned char readFromFifoQueue(FifoQueue* queue) {
+    if (isFifoQueueEmpty(queue)) {
+        return 0;
+    }
+
+    unsigned char data = queue->buffer[queue->head];
+    queue->head = (queue->head + 1) % queue->capacity;
+    queue->count--;
+
+    return data;
+}
+
+/*清空fifo*/
+void clearFifoQueue(FifoQueue* queue) {
+    if (queue == NULL) {
+        return;
+    }
+
+    queue->head = 0;
+    queue->tail = 0;
+    queue->count = 0;
+}
+
+
+int Pull_up_ctrl_pin()
+{
+    int ret;
+
+    ret = write(my_485->ctlFd, "1", 1);
+    if(ret<0)
+    {
+        printf("max485 set ctl to low failed!\r\n");
+        
+        return -1;
+    }
+    else
+    {
+        return 0;
+    }
+}
+
+int Pull_down_ctrl_pin()
+{
+    int ret;
+
+    ret = write(my_485->ctlFd, "0", 1);
+    if(ret<0)
+    {
+        printf("max485 set ctl to high failed!\r\n");
+
+        return -1;
+    }
+    else
+    {
+        return 0;
+    }
+}
+
+/*
+发送数据
+参数定义:
+    eia485_ctrl: EIA485控制器
+    tx_data:     发送数据
+    tx_size:     发送数据长度
+返回值:
+    0: 成功
+    -1:失败
+*/
+int32_t mg_eia485_write(mg_eia485_t * eia485_ctrl,uint8_t * tx_data,uint32_t tx_size )
+{
+    int rc = 0;
+    if (my_485->uartFd == -1) {
+        perror("UART device not open");
+        goto error;;
+    }
+
+    Pull_up_ctrl_pin();
+    usleep(1000);
+    int result = write(my_485->uartFd, tx_data, tx_size);
+    if (result < 0) {
+        perror("Failed to write to UART device");
+        goto error;
+    }
+    Pull_down_ctrl_pin();
+    usleep(1000);
+    return 0;
+error:
+    return -1;
+}
+
+/*
+接收数据
+参数定义:
+    eia485_ctrl: EIA485控制器
+    tx_data:     接收buffer
+    tx_size:     最大读取长度
+    timeout_ms:  等待超时时间,单位毫秒,如果值是0则立即返回
+返回值:
+    大于0:读取到的数据长度
+    0: 无数据 
+    -1: 操作失败失败
+*/
+int32_t mg_eia485_read( mg_eia485_t * eia485_ctrl,uint8_t * rx_data, uint32_t rx_size,uint32_t timeout_ms )
+{
+    unsigned char receiveData;
+
+    if (my_485->uartFd == -1) {
+        perror("UART device not open");
+        goto error;
+    }
+    // 接收数据
+    int bytesRead = 0;
+    while (bytesRead < rx_size) {
+       
+        receiveData=readFromFifoQueue(&receiveQueue);
+        rx_data[bytesRead] = receiveData;
+        bytesRead++;
+        printf("rx_data[bytesRead]----------->%c\n",receiveData);
+        // if(time_flag == 1)
+        // {
+        //     clearFifoQueue(&receiveQueue);
+        //     time_flag = 0;
+        // }
+    }
+    return bytesRead;
+error:
+    return -1;
+}
+
+void* uart_task(void* data)
+{
+    int result = 0;
+    fd_set rfds;
+    int ret;
+    FD_ZERO(&rfds);
+    FD_SET(my_485->uartFd, &rfds);  
+    unsigned char receiveData,rData;
+    printf("this is therad\n");
+    while (stop) {
+        struct timeval timeout;
+
+        // 设置超时时间为2秒
+        timeout.tv_sec = 2;
+        timeout.tv_usec = 0;
+        ret = select(my_485->uartFd + 1, &rfds, NULL, NULL,NULL);
+        result = read(my_485->uartFd, &receiveData, 1);
+        if (result > 0) {
+            if (!writeToFifoQueue(&receiveQueue,receiveData))
+                perror("Failed to write to FIFO queue\n");
+            printf("receivedata 1----------->%c\n",receiveData);
+            //rData=readFromFifoQueue(&receiveQueue);
+            //printf("rdata 2----------->%c\n",rData);
+        }else if(result == 0){
+            lseek(my_485->uartFd, 0, SEEK_SET);
+        }
+    }
+}
+
+
+
+
+/*
+打开EIA485控制器
+参数定义:
+    baud: 波特率
+    data_bits:数据位长度
+    stop_bits: 停止位长度
+    parity:   校验位
+返回值: 
+    打开失败,返回 NULL;
+    成功,返回TOF控制器指针。
+*/
+mg_eia485_t * mg_eia485_open(uint32_t baud, uint32_t data_bits, uint32_t stop_bits, uint32_t parity )
+{
+    initFifoQueue(&receiveQueue, FIFO_SIZE);
+
+    pthread_t uart5_thread;
+    my_485 = (mg_eia485_t*)malloc(sizeof(mg_eia485_t));
+    my_485->dev_path = UART_DEVICE;
+    my_485->dev_path = CTL_DEVICE; 
+
+    stop = 1;
+    my_485->ctlFd = open(CTL_DEVICE, O_RDWR | O_NOCTTY | O_NDELAY);
+    if (my_485->uartFd == -1) {
+        perror("Failed to open CTL");
+        goto error;
+    }
+    my_485->uartFd = open(UART_DEVICE, O_RDWR | O_NOCTTY | O_NDELAY);
+    if (my_485->uartFd == -1) {
+        perror("Failed to open UART");
+        goto error;
+    }
+
+    struct termios newtio,oldtio;
+    if  ( tcgetattr(my_485->uartFd,&oldtio)  !=  0) { 
+        perror("SetupSerial 1");
+        goto error;
+    }
+    bzero( &newtio, sizeof( newtio ) );
+    newtio.c_cflag  |=  CLOCAL | CREAD;
+    newtio.c_cflag &= ~CSIZE;
+
+    switch( data_bits )
+    {
+    case 5:
+        newtio.c_cflag |= CS5;
+        break;
+    case 6:
+        newtio.c_cflag |= CS6;
+        break;
+    case 7:
+        newtio.c_cflag |= CS7;
+        break;
+    case 8:
+        newtio.c_cflag |= CS8;
+        break;
+    }
+
+    switch( parity )
+    {
+    case '1':
+        newtio.c_cflag |= PARENB;
+        newtio.c_cflag |= PARODD;
+        newtio.c_iflag |= (INPCK | ISTRIP);
+        break;
+    case '2': 
+        newtio.c_iflag |= (INPCK | ISTRIP);
+        newtio.c_cflag |= PARENB;
+        newtio.c_cflag &= ~PARODD;
+        break;
+    case '0':  
+        newtio.c_cflag &= ~PARENB;
+        break;
+    }
+
+    switch( baud )
+    {
+    case 2400:
+        cfsetispeed(&newtio, B2400);
+        cfsetospeed(&newtio, B2400);
+        break;
+    case 4800:
+        cfsetispeed(&newtio, B4800);
+        cfsetospeed(&newtio, B4800);
+        break;
+    case 9600:
+        cfsetispeed(&newtio, B9600);
+        cfsetospeed(&newtio, B9600);
+        break;
+    case 115200:
+        cfsetispeed(&newtio, B115200);
+        cfsetospeed(&newtio, B115200);
+        break;
+    case 460800:
+        cfsetispeed(&newtio, B460800);
+        cfsetospeed(&newtio, B460800);
+        break;
+    case 921600:
+        printf("Rate:921600\n");
+        cfsetispeed(&newtio, B921600);
+                cfsetospeed(&newtio, B921600);
+                break;
+    default:
+        cfsetispeed(&newtio, B9600);
+        cfsetospeed(&newtio, B9600);
+        break;
+    }
+    if( stop_bits == 1 )
+        newtio.c_cflag &=  ~CSTOPB;
+    else if ( stop_bits == 2 )
+        newtio.c_cflag |=  CSTOPB;
+
+    newtio.c_cc[VTIME]  = 0;
+    newtio.c_cc[VMIN] = 0;
+    tcflush(my_485->uartFd,TCIFLUSH);
+    if((tcsetattr(my_485->uartFd,TCSANOW,&newtio))!=0)
+    {
+        perror("com set error");
+        return -1;
+    }
+    if( pthread_create( &uart5_thread, NULL, uart_task, NULL) != 0 ){
+        perror("create 485 task!");
+        goto error;
+    }
+    return my_485;
+error:
+    return NULL;
+};
+
+/*
+关闭控制器
+参数定义:
+    eia485_ctrl: EIA485控制器
+返回值: 无
+*/
+void mg_eia485_close( mg_eia485_t * eia485_ctrl )
+{
+    stop = 0;
+    if (eia485_ctrl->uartFd != -1) {
+        close(eia485_ctrl->uartFd);
+        eia485_ctrl->uartFd = -1;
+    }
+}