diff --git a/CMakeLists.txt b/CMakeLists.txt index 52666ef..2e1b0d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,8 @@ set(DRV_SOURCE ${PROJECT_SOURCE_DIR}/drv/drv_di.c # DO驱动源文件 ${PROJECT_SOURCE_DIR}/drv/drv_do.c + # UART驱动源文件 + ${PROJECT_SOURCE_DIR}/drv/drv_uart.c ) # 添加KIT源文件 diff --git a/app/app_di_detect.h b/app/app_di_detect.h new file mode 100644 index 0000000..e69de29 diff --git a/drv/drv_di.c b/drv/drv_di.c index da1c2c2..02f792e 100644 --- a/drv/drv_di.c +++ b/drv/drv_di.c @@ -1,4 +1,13 @@ #include "drv_di.h" -#include -#include -#include + +void drv_di_read(char *dev, char *value) +{ + int devFd = open(dev, O_RDONLY); + if(devFd == -1) { + printf("open device failed[%s]\n", dev); + } + else { + read(devFd, value, sizeof(value)); + } + close(devFd); +} diff --git a/drv/drv_di.h b/drv/drv_di.h index 4d07625..93c03f9 100644 --- a/drv/drv_di.h +++ b/drv/drv_di.h @@ -1,6 +1,13 @@ #ifndef __DRV_DI_H_ #define __DRV_DI_H_ +#include +#include +#include + +#define DRV_DI_H 1 +#define DRV_DI_L 0 + #define DRV_DI1 "/sys/class/gpio-input/DIN1/state" #define DRV_DI2 "/sys/class/gpio-input/DIN2/state" #define DRV_DI3 "/sys/class/gpio-input/DIN3/state" @@ -12,4 +19,6 @@ #define DRV_KEY "sys/class/gpio-input/FWn/state" +void drv_di_read(char *dev, char *value); + #endif \ No newline at end of file diff --git a/drv/drv_do.c b/drv/drv_do.c index 6e8ee92..d1fa285 100644 --- a/drv/drv_do.c +++ b/drv/drv_do.c @@ -1,7 +1,4 @@ #include"drv_do.h" -#include -#include -#include void drv_do_write(char *dev, char *value) { diff --git a/drv/drv_do.h b/drv/drv_do.h index f95206e..d4ba2cc 100644 --- a/drv/drv_do.h +++ b/drv/drv_do.h @@ -1,6 +1,10 @@ #ifndef __DRV_DO_H_ #define __DRV_DO_H_ +#include +#include +#include + #define DRV_DO1 "/sys/class/leds/do1/brightness" #define DRV_DO2 "/sys/class/leds/do2/brightness" #define DRV_DO3 "/sys/class/leds/do3/brightness" @@ -12,8 +16,8 @@ #define DRV_LED3 "/sys/class/leds/led1/brightness" #define DRV_LED4 "/sys/class/leds/led2/brightness" -#define DO_H "1" -#define DO_L "0" +#define DRV_DO_H "1" +#define DRV_DO_L "0" void drv_do_write(char *dev, char *value); diff --git a/drv/drv_uart.c b/drv/drv_uart.c index e69de29..84e02ba 100644 --- a/drv/drv_uart.c +++ b/drv/drv_uart.c @@ -0,0 +1,86 @@ +#include "drv_uart.h" + +int drv_uart_open(char *dev, int32_t baudrate, int32_t databit, char parity, char stopbit) +{ + int devFd = open(dev, O_RDWR | O_NONBLOCK| O_NOCTTY | O_NDELAY); + if(devFd == -1) { + printf("open uart failed\n"); + } + else { + struct termios oldtio, newtio; + memset(&newtio, 0, sizeof(newtio)); + speed_t speed; + switch (baudrate) + { + case 9600: + speed = B9600; + break; + case 38400: + speed = B38400; + break; + case 115200: + speed = B115200; + break; + default: + speed = B115200; + break; + } + + switch (databit) + { + case 8: + newtio.c_cflag |= CS8; + break; + case 7: + newtio.c_cflag |= CS7; + break; + case 6: + newtio.c_cflag |= CS6; + break; + case 5: + newtio.c_cflag |= CS5; + break; + default: + newtio.c_cflag |= CS8; + break; + } + + switch (parity) + { + case 'N': + newtio.c_cflag &= ~PARENB; + break; + case 'E': + newtio.c_cflag |= PARENB; + newtio.c_cflag &= ~PARODD; + break; + case 'O': + newtio.c_cflag |= (PARENB | PARODD); + break; + default: + newtio.c_cflag &= ~PARENB; + break; + } + + switch (stopbit) + { + case '1': + newtio.c_cflag &= ~CSTOPB; + break; + case '2': + newtio.c_cflag |= CSTOPB; + default: + newtio.c_cflag &= ~CSTOPB; + break; + } + + newtio.c_cflag |= (CLOCAL | CREAD); /* | CRTSCTS */ + newtio.c_oflag &= ~OPOST; + newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); + newtio.c_cc[VTIME] = 1; /* unit: 1/10 second. */ + newtio.c_cc[VMIN] = 255; /* minimal characters for reading */ + tcflush(devFd, TCIFLUSH); + tcsetattr(devFd, TCSANOW, &newtio); + } + return devFd; +} \ No newline at end of file diff --git a/drv/drv_uart.h b/drv/drv_uart.h index e69de29..bcb824c 100644 --- a/drv/drv_uart.h +++ b/drv/drv_uart.h @@ -0,0 +1,44 @@ +#ifndef __DRV_UART_H_ +#define __DRV_UART_H_ + +#include +#include +#include +#include +#include +#include + +#define RS485_1 "/dev/ttyS9" +#define RS485_2 "/dev/ttyS5" +#define RS485_3 "/dev/ttyS3" +#define RS485_4 "/dev/ttyS4" +#define RS485_5 "/dev/ttyS7" +#define RS485_6 "/dev/ttyS8" +#define RS485_7 "/dev/ttyS0" +#define RS485_8 "/dev/ttyXRUSB1" +#define RS485_9 "/dev/ttyXRUSB2" +#define RS485_10 "/dev/ttyXRUSB3" +#define RS485_11 "/dev/ttyXRUSB0" + +#define MAX_UART_NUM 6 +#define UART_DATA_LEN 256 + +static struct termios termios_old, termios_new; + +//串口驱动结构体设计 +typedef struct{ + int32_t fd; //串口对应的标识符 + int32_t uartId; //第几个串口 + int32_t baudrate; + int32_t databit; + int32_t sendDataLen; + int32_t receiveDataLen; + const char *stopbit; + char parity; + uint8_t sendBuffer[UART_DATA_LEN]; //point to send buffer + uint8_t receiveBuffer[UART_DATA_LEN]; //point to receive buffer +}UartLib_T; + +int drv_uart_open(char *dev, int32_t baudrate, int32_t databit, char parity, char stopbit); + +#endif \ No newline at end of file diff --git a/test/test.c b/test/test.c index 9e12c33..a6384f1 100644 --- a/test/test.c +++ b/test/test.c @@ -1,53 +1,115 @@ #include +#include #include #include "test.h" #include "kit_logger.h" #include "drv_di.h" #include "drv_do.h" +#include "drv_uart.h" void runTest() { printf("runTest...\n"); - testDO(); - testDI(); + // testDO(); + // testDI(); + // testUart(); testCreatThreadTask(); } +// 创建线程入口 void testCreatThreadTask() { printf("testThreadTask\n"); logger_init("./log"); - pthread_t tTestLogger; - pthread_create(&tTestLogger, NULL, testLoggerThread, "testLoggerThread"); - pthread_join(tTestLogger, NULL); + pthread_t tTestLogger, tTestDIDetect, tTestUart; + // pthread_create(&tTestLogger, NULL, testLoggerThread, "testLoggerThread"); + // pthread_join(tTestLogger, NULL); + // int ret = pthread_create(&tTestDIDetect, NULL, testDIDetectThread, "testDIDetectThread"); + // printf("testDIDetectThread ret = %d\n", ret); + // pthread_join(tTestDIDetect, NULL); + pthread_create(&tTestUart, NULL, testUartThread, "testUartThread"); + pthread_join(tTestUart, NULL); logger_destroy(); } +// Logger测试线程 void *testLoggerThread(void *arg) { - logger_level_printf(LOGGER_DEBUG_LEVEL, "this an debug"); - logger_level_printf(LOGGER_INFO_LEVEL, "this an info"); - logger_level_printf(LOGGER_WARN_LEVEL, "this an warn"); - logger_level_printf(LOGGER_ERROR_LEVEL, "this an error"); - logger_level_printf(LOGGER_FATAL_LEVEL, "this an fatal"); + logger_level_printf(LOGGER_DEBUG_LEVEL, arg); } +// DI状态检测测试线程 +void *testDIDetectThread(void *arg) +{ + logger_level_printf(LOGGER_DEBUG_LEVEL, arg); + while(1) + { + logger_level_printf(LOGGER_DEBUG_LEVEL, "DI Detect..."); + char DIState[8]; + drv_di_read(DRV_DI1, &DIState[0]); + drv_di_read(DRV_DI2, &DIState[1]); + drv_di_read(DRV_DI3, &DIState[2]); + drv_di_read(DRV_DI4, &DIState[3]); + drv_di_read(DRV_DI5, &DIState[4]); + drv_di_read(DRV_DI6, &DIState[5]); + drv_di_read(DRV_DI7, &DIState[6]); + drv_di_read(DRV_DI8, &DIState[7]); + + for(int i = 0; i < 8; i++) { + char tempDI[20]; + sprintf(tempDI, "DI[%d] [%c]\n", i, DIState[i]); + logger_level_printf(LOGGER_DEBUG_LEVEL, tempDI); + } + sleep(1); + } +} + +// Uart测试线程 +void *testUartThread(void *arg) +{ + logger_level_printf(LOGGER_DEBUG_LEVEL, arg); + char buffer[256]; + char sendBuffer[100]="uart send buff\n"; + int devFd = drv_uart_open(RS485_1, 9600, 8, 'N', '1'); + if(devFd == -1) { + logger_level_printf(LOGGER_DEBUG_LEVEL, "open RS485_1 failed"); + } + memset(buffer, 0, sizeof(buffer)); + while(1) + { + // write(devFd, sendBuffer, strlen(sendBuffer)); + int recvLen = read(devFd, buffer, sizeof(buffer) - 1); + if (recvLen > 0) { + buffer[recvLen] = '\0'; // 确保字符串以null结尾 + printf("Received: %s\n", buffer); + } + // sleep(1); + } + close(devFd); +} + +// DI驱动测试 void testDI() { - + printf("test di\n"); + char value; + drv_di_read(DRV_DI1, &value); + printf("device[%s] value[%c]\n", DRV_DI1, value); } +// DO驱动测试 void testDO() { printf("test do\n"); - drv_do_write(DRV_LED1, DO_L); - drv_do_write(DRV_LED2, DO_L); - drv_do_write(DRV_LED3, DO_L); - drv_do_write(DRV_LED4, DO_L); - drv_do_write(DRV_DO1, DO_L); - drv_do_write(DRV_DO2, DO_L); - drv_do_write(DRV_DO3, DO_L); - drv_do_write(DRV_DO4, DO_L); - drv_do_write(DRV_DO5, DO_L); - drv_do_write(DRV_DO6, DO_L); + drv_do_write(DRV_LED1, DRV_DO_H); +} + +// Uart驱动测试 +void testUart() +{ + printf("test uart\n"); + int devFd = drv_uart_open(RS485_1, 9600, 8, 'N', '1'); + char test[100]="forlinx_uart_test.1234567890..."; + printf("Send test data:\n%s\n", test); + write(devFd, test, strlen(test) + 1); } \ No newline at end of file diff --git a/test/test.h b/test/test.h index a87a1df..b42cd7d 100644 --- a/test/test.h +++ b/test/test.h @@ -2,9 +2,11 @@ #define __TEST_H_ void runTest(); -void testCreatThreadTask(); -void *testLoggerThread(void *arg); void testDI(); void testDO(); - +void testUart(); +void testCreatThreadTask(); +void *testLoggerThread(void *arg); +void *testDIDetectThread(void *arg); +void *testUartThread(void *arg); #endif \ No newline at end of file