EC801 CH340驱动,打样

这个提交包含在:
2025-11-29 02:17:31 +08:00
当前提交 82e9eb38d6
共有 5 个文件被更改,包括 283 次插入 和 0 次删除

1
.gitignore vendored 普通文件
查看文件

@ -0,0 +1 @@
build

17
.vscode/c_cpp_properties.json vendored 普通文件
查看文件

@ -0,0 +1,17 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}

19
.vscode/settings.json vendored 普通文件
查看文件

@ -0,0 +1,19 @@
{
"files.associations": {
"*.part": "html",
"*.MD": "markdown",
"*.jsonx": "javascript",
"unistd.h": "c",
"termios.h": "c",
"atomic": "c",
"fstream": "c",
"ostream": "c",
"sstream": "c",
"*.inc": "c",
"ioctl.h": "c",
"features.h": "c",
"fcntl.h": "c",
"ch9329.h": "c",
"stdio.h": "c"
}
}

15
CMakeLists.txt 普通文件
查看文件

@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 4.2)
project(UART)
find_path(READLINE_INCLUDE_DIR readline/readline.h)
find_library(READLINE_LIBRARY readline)
# 检查是否找到库
if (READLINE_INCLUDE_DIR AND READLINE_LIBRARY)
include_directories(${READLINE_INCLUDE_DIR})
add_executable(dtu src/main.c)
target_link_libraries(dtu ${READLINE_LIBRARY})
else()
message(FATAL_ERROR "Readline library not found!")
endif()

231
src/main.c 普通文件
查看文件

@ -0,0 +1,231 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <sys/select.h>
#define SERIAL_PORT "/dev/ttyUSB0"
#define BAUD_RATE B115200
int serial_fd = -1;
// 初始化串口
int init_serial() {
serial_fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_SYNC);
if (serial_fd < 0) {
perror("打开串口失败");
return -1;
}
struct termios tio;
memset(&tio, 0, sizeof(tio));
if (tcgetattr(serial_fd, &tio) != 0) {
perror("获取串口属性失败");
close(serial_fd);
return -1;
}
cfsetispeed(&tio, BAUD_RATE);
cfsetospeed(&tio, BAUD_RATE);
// 配置串口
cfmakeraw(&tio);
tio.c_cflag |= (CLOCAL | CREAD);
tio.c_cflag &= ~PARENB;
tio.c_cflag &= ~CSTOPB;
tio.c_cflag &= ~CSIZE;
tio.c_cflag |= CS8;
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 5;
if (tcsetattr(serial_fd, TCSANOW, &tio)) {
perror("设置串口属性失败");
close(serial_fd);
return -1;
}
printf("串口初始化成功\n");
return 0;
}
// 发送AT命令并读取响应
void send_at_command(const char* command) {
if (serial_fd < 0) {
printf("错误:串口未打开\n");
return;
}
// 构建完整的AT命令(添加回车)
char full_command[256];
snprintf(full_command, sizeof(full_command), "%s\r", command);
// 发送命令
ssize_t written = write(serial_fd, full_command, strlen(full_command));
if (written != strlen(full_command)) {
perror("发送命令失败");
return;
}
printf("发送: %s\n", command);
// 读取响应
printf("响应: ");
char buf[1024];
int total_timeout = 0;
int max_timeout = 100; // 最大超时计数(约2秒)
while (total_timeout < max_timeout) {
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(serial_fd, &read_fds);
struct timeval timeout = {0, 20000}; // 20ms 超时
int ret = select(serial_fd + 1, &read_fds, NULL, NULL, &timeout);
if (ret > 0 && FD_ISSET(serial_fd, &read_fds)) {
int n = read(serial_fd, buf, sizeof(buf) - 1);
if (n > 0) {
buf[n] = '\0';
printf("%s", buf);
fflush(stdout);
total_timeout = 0; // 有数据时重置超时计数
continue;
} else if (n < 0) {
perror("读取响应失败");
break;
}
} else if (ret == 0) {
// 超时
total_timeout++;
} else {
perror("select错误");
break;
}
}
printf("\n");
}
// 清理函数
void cleanup() {
if (serial_fd >= 0) {
close(serial_fd);
printf("\n串口已关闭\n");
}
}
// 命令补全函数
char* command_generator(const char* text, int state) {
static int list_index, len;
const char* commands[] = {
"AT", "ATI", "AT+CGMI", "AT+CGMM", "AT+CGMR", "AT+CGSN",
"AT+CSQ", "AT+CREG", "AT+COPS", "AT+CMGF", "AT+CNMI",
"AT+CPIN", "AT+CCID", "AT+CGATT", "AT+CGACT", "AT+CGPADDR",
"AT+CGDCONT", "AT+CGDATA", "ATD", "ATH", "ATA", "AT+CMGS",
"AT+CMGR", "AT+CMGL", "AT+CMGD", "AT+CPMS", "AT+CMEE",
"AT+CFUN", "AT+CSCS", "AT+CGREG", "AT+CEREG", "AT+CGACT",
"AT+CGLA", "AT+CGSCAN", "AT+CGPS", "AT+CGNSPWR", "AT+CGNSINF",
"AT+CGNSTST", "AT+CGNSURC", "AT+CGNSCMD", NULL
};
if (!state) {
list_index = 0;
len = strlen(text);
}
while (commands[list_index]) {
const char* name = commands[list_index++];
if (strncmp(name, text, len) == 0) {
return strdup(name);
}
}
return NULL;
}
// 补全条目生成器
char** at_completion(const char* text, int start, int end) {
rl_attempted_completion_over = 1;
return rl_completion_matches(text, command_generator);
}
// 显示帮助信息
void show_help() {
printf("\nAT命令交互工具\n");
printf("可用命令:\n");
printf(" help - 显示此帮助信息\n");
printf(" exit - 退出程序\n");
printf(" quit - 退出程序\n");
printf(" clear - 清空屏幕\n");
printf(" history - 显示命令历史\n");
printf(" 或直接输入AT命令 (如: ATI, AT+CGMI, etc.)\n");
printf("支持Tab键自动补全AT命令\n\n");
}
int main() {
printf("=== AT命令交互工具 ===\n");
// 初始化串口
if (init_serial() != 0) {
return -1;
}
// 注册清理函数
atexit(cleanup);
// 设置readline补全
rl_attempted_completion_function = at_completion;
// 设置历史文件
using_history();
read_history(".at_command_history");
show_help();
char* input;
while ((input = readline("AT> ")) != NULL) {
// 处理空输入
if (strlen(input) == 0) {
free(input);
continue;
}
// 添加历史记录
add_history(input);
// 处理内置命令
if (strcmp(input, "exit") == 0 || strcmp(input, "quit") == 0) {
free(input);
break;
} else if (strcmp(input, "help") == 0) {
show_help();
} else if (strcmp(input, "clear") == 0) {
printf("\033[2J\033[H"); // 清屏
} else if (strcmp(input, "history") == 0) {
HIST_ENTRY** hl = history_list();
if (hl) {
for (int i = 0; hl[i]; i++) {
printf("%d: %s\n", i + 1, hl[i]->line);
}
}
} else {
// 发送AT命令
send_at_command(input);
}
free(input);
}
// 保存历史记录
write_history(".at_command_history");
printf("再见!\n");
return 0;
}