#include #include #include #include "test.h" #include "kit_logger.h" #include "drv_di.h" #include "drv_do.h" #include "drv_uart.h" #include "drv_tcp.h" void runTest() { printf("runTest...\n"); // testDO(); // testDI(); // testUart(); testCreatThreadTask(); } // 创建线程入口 void testCreatThreadTask() { printf("testThreadTask\n"); logger_init("./log"); pthread_t tTestLogger, tTestDIDetect, tTestUart, tTestTcp; // 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); pthread_create(&tTestTcp, NULL, testTcpThread, "testTcpThread"); pthread_join(tTestTcp, NULL); logger_destroy(); } // Logger测试线程 void *testLoggerThread(void *arg) { 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, 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); } void *testTcpThread(void *arg) { logger_level_printf(LOGGER_DEBUG_LEVEL, arg); printf("TCP Serve Start\n"); // getInterfacesAndIPs(); tcpServe(8888); }