+++ /dev/null
-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 )
+++ /dev/null
-#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
+++ /dev/null
-#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;
- }
-}
add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/tof )
add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/als )
add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/buzzer )
-#add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/485 )
+add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/eia485 )
add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/tuya-iot-core-sdk )
#include "led.h"
#include "button.h"
#include "cQueue.h"
+#include "cJSON.h"
#ifdef __x86_64__
//#warning "PC Version Debug"
#define TUYA_AUTH_PATH "tuya_auth"
#define MG_ACTIVE_PATH "mg.active"
#define READ_UUID_CMD "cat cpuinfo | grep Serial"
+#define ALS_CFG_PATH "als.cfg"
#else
#define DEV_CFG_PATH "/appconfigs/dev.cfg"
+#define ALS_CFG_PATH "/appconfigs/als.cfg"
#define TUYA_AUTH_PATH "/customer/tuya_auth"
#define MG_ACTIVE_PATH "/customer/mg.active"
#define READ_UUID_CMD "cat /proc/cpuinfo | grep Serial"
#endif
+//环境光传感器默认阈值
+#define ALS_THRESHOLD_HIGH_DEFAULT 1000
+#define ALS_THRESHOLD_LOW_DEFAULT 400
+
+
#define KEY_LED_MAX_DEF 100 //(0-100%)
#define ATMOS_LED_MAX_DEF 100 //(0-100%)
//#define RECOGNITION_MODE_DEF 0 //enum 0:"light_mode", 1:"dark_mode"
void mg_dev_lock( mg_dev_t *dev );
void mg_dev_unlock( mg_dev_t *dev );
+//
+int32_t load_als_threshold( uint32_t *ts_high, uint32_t *ts_low);
#endif //__MG_DEVICE_H__
return 0;
}
+int32_t load_als_threshold( uint32_t *ts_high, uint32_t *ts_low)
+{
+ FILE *fd;
+ char json_data[200];
+ size_t n;
+ cJSON *root = NULL;
+ cJSON *high = NULL;
+ cJSON *low = NULL;
+
+ //printf( "load als thresold...\r\n" );
+ //get als.cfg : { "high":1000, "low":400 }
+ //1. open file
+ fd = fopen( ALS_CFG_PATH, "r" );
+ if( !fd ){
+ MG_LOGD( "open als_cfg file[%s] fail!\r\n", ALS_CFG_PATH );
+ goto err_out;
+ }
+ //2. read json data
+ n = fread( json_data, 1, sizeof(json_data)-1, fd );
+ if( n == 0 ){
+ MG_LOGD( "read als config file fail!\r\n" );
+ goto err_read;
+ }
+ json_data[n] = 0;
+ //( "get json data[%ld]:%s\r\n", n, json_data );
+
+ //3. parse json
+ root = cJSON_Parse((const char *)json_data);
+ if( !root ){
+ MG_LOGD( "parser json data fail!\r\n" );
+ goto err_json;
+ }
+
+ high =cJSON_GetObjectItem(root, "high");
+ low =cJSON_GetObjectItem(root, "low");
+
+ if( !cJSON_IsNumber(high) || !cJSON_IsNumber(low) ){
+ MG_LOGD( "get threshold high:fail\r\n" );
+ goto err_json;
+ }
+
+ if( high )
+ *ts_high = high->valueint;
+ if( ts_low )
+ *ts_low = low->valueint;
+
+
+ cJSON_Delete( root );
+ return 0;
+
+err_json:
+ if( root ){
+ cJSON_Delete( root );
+ }
+
+err_read:
+ if( fd )
+ fclose(fd);
+err_out:
+ if( ts_high )
+ *ts_high = ALS_THRESHOLD_HIGH_DEFAULT;
+ if( ts_low )
+ *ts_low = ALS_THRESHOLD_LOW_DEFAULT;
+ return -1;
+}
+
--- /dev/null
+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 )
--- /dev/null
+#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
--- /dev/null
+#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;
+ }
+}