EMS/test/test.c

128 lines
3.3 KiB
C
Raw Normal View History

#include <stdio.h>
2024-11-12 08:21:37 +08:00
#include <string.h>
#include <pthread.h>
#include "test.h"
#include "kit_logger.h"
#include "drv_di.h"
#include "drv_do.h"
2024-11-12 08:21:37 +08:00
#include "drv_uart.h"
2024-11-13 14:46:59 +08:00
#include "drv_tcp.h"
void runTest()
{
printf("runTest...\n");
2024-11-12 08:21:37 +08:00
// testDO();
// testDI();
// testUart();
testCreatThreadTask();
}
2024-11-12 08:21:37 +08:00
// 创建线程入口
void testCreatThreadTask()
{
printf("testThreadTask\n");
logger_init("./log");
2024-11-13 14:46:59 +08:00
pthread_t tTestLogger, tTestDIDetect, tTestUart, tTestTcp;
2024-11-12 08:21:37 +08:00
// 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);
2024-11-13 14:46:59 +08:00
// pthread_create(&tTestUart, NULL, testUartThread, "testUartThread");
// pthread_join(tTestUart, NULL);
pthread_create(&tTestTcp, NULL, testTcpThread, "testTcpThread");
pthread_join(tTestTcp, NULL);
logger_destroy();
}
2024-11-12 08:21:37 +08:00
// Logger测试线程
void *testLoggerThread(void *arg)
{
2024-11-12 08:21:37 +08:00
logger_level_printf(LOGGER_DEBUG_LEVEL, arg);
}
2024-11-12 08:21:37 +08:00
// DI状态检测测试线程
void *testDIDetectThread(void *arg)
{
2024-11-12 08:21:37 +08:00
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);
}
}
2024-11-12 08:21:37 +08:00
// 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);
}
2024-11-12 08:21:37 +08:00
// 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");
2024-11-12 08:21:37 +08:00
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);
2024-11-13 14:46:59 +08:00
}
void *testTcpThread(void *arg)
{
logger_level_printf(LOGGER_DEBUG_LEVEL, arg);
printf("TCP Serve Start\n");
// getInterfacesAndIPs();
tcpServe(8888);
}