#include #include #include #include "test.h" #include "kit_logger.h" #include "drv_di.h" #include "drv_do.h" #include "drv_uart.h" #include "drv_can.h" #include "drv_tcp.h" #include "drv_4g.h" // #include "libmodbus/include/modbus-rtu.h" #include "kit_mqtt.h" #include "kit_mqtt_sub.h" // #include "MQTTAsync.h" void runTest() { printf("runTest...\n"); // testDO(); // testDI(); // testUart(); testCreatThreadTask(); } // 创建线程入口 void testCreatThreadTask() { printf("testThreadTask\n"); logger_init("./log"); pthread_t tTestLogger, tTestDIDetect, tTestUart, tTestTcp, tTest4G, tTestCanSend, tTestCanRecv, tTestModbus, tTestMQTT; // 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); // pthread_create(&tTest4G, NULL, test4GThread, "test4GThread"); // pthread_join(tTest4G, NULL); // pthread_create(&tTestCanSend, NULL, testCanSendThread, "testCanSendThread"); // pthread_join(tTestCanSend, NULL); // pthread_create(&tTestCanRecv, NULL, testCanRecvThread, "testCanRecvThread"); // pthread_join(tTestCanRecv, NULL); // pthread_create(&tTestModbus, NULL, testModbusThread, "testModbusThread"); // pthread_join(tTestModbus, NULL); pthread_create(&tTestMQTT, NULL, testMQTTThread, "testMQTTThread"); pthread_join(tTestMQTT, NULL); logger_destroy(); } // 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); } // 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); } //服务端功能测试线程 void *testTcpThread(void *arg) { logger_level_printf(LOGGER_DEBUG_LEVEL, arg); printf("TCP Serve Start\n"); // getInterfacesAndIPs(); tcpServe(8888); } //客户端功能测试线程 void *test4GThread(void *arg) { logger_level_printf(LOGGER_DEBUG_LEVEL, arg); printf("client test!\n"); client(SERVER_ADDR, PORT); } //Can接收功能测试线程 void *testCanRecvThread(void *arg) { logger_level_printf(LOGGER_DEBUG_LEVEL, arg); printf("Can Recv test!\n"); unsigned int canIDs[] = {0x123, 0x456, 0x789}; while(1) { printf("enter the can recv loop\n"); can_recv(CANNUM_RECV, canIDs, CANIDNUM, CANDLC_RECV); sleep(1); } } //Can发送功能测试线程 void *testCanSendThread(void *arg) { logger_level_printf(LOGGER_DEBUG_LEVEL, arg); printf("Can Send test!\n"); unsigned int canID_send = 0x112233; char * can_send_data = "01 02 03 04 05 06 07 08 09 10"; while(1) { printf("enter the can send loop\n"); can_send(CANNUM_SEND, canID_send, can_send_data, CANDLC_SEND); sleep(1); } } //Modbus测试线程 void *testModbusThread(void *arg) { logger_level_printf(LOGGER_DEBUG_LEVEL, arg); // MQTTAsync_malloc(12); // modbus_new_rtu("dev", 9600, '1', '8', 1); } //MQTT测试线程 void *testMQTTThread(void *arg) { logger_level_printf(LOGGER_DEBUG_LEVEL, arg); printf("MQTT test!\n"); int rc = pubClient(); if (rc < 0) { printf("MQTT wrong\n"); } // int rc = subClient(); // if (rc < 0) // { // printf("MQTT wrong\n"); // } }