diff --git a/my_pro/myproject.c b/my_pro/myproject.c index f24755e..40a8416 100644 --- a/my_pro/myproject.c +++ b/my_pro/myproject.c @@ -403,6 +403,9 @@ void cdc_entry(void *parameter) rt_thread_mdelay(500); } } + +/* 用于接收消息的信号量 */ +static struct rt_semaphore rx_sem; /* 接收数据回调函数 */ static rt_err_t uart_input(rt_device_t dev, rt_size_t size) { @@ -414,19 +417,19 @@ static rt_err_t uart_input(rt_device_t dev, rt_size_t size) static void serial_thread_entry(void *parameter) { - char ch; + char ch[105]; while (1) { /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */ - while (rt_device_read(serial, -1, &ch, 1) != 1) + int Size; + while ((Size=rt_device_read(serial, -1, &ch, 104) )!= 1) { /* 阻塞等待接收信号量,等到信号量后再次读取数据 */ rt_sem_take(&rx_sem, RT_WAITING_FOREVER); } - /* 读取到的数据通过串口错位输出 */ - ch = ch + 1; - rt_device_write(serial, 0, &ch, 1); + + rt_kprintf("%s",ch); } } @@ -464,9 +467,22 @@ void serial_init(void) /* 以 DMA 接收及轮询发送方式打开串口设备 */ rt_device_open(serial, RT_DEVICE_FLAG_INT_RX); + /* 设置接收回调函数 */ + rt_device_set_rx_indicate(serial, uart_input); char str[] = "hello RTT\r\n"; /* 发送字符串 */ rt_device_write(serial, 0, str, (sizeof(str) - 1)); + /* 创建 serial 线程 */ + rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10); + /* 创建成功则启动线程 */ + if (thread != RT_NULL) + { + rt_thread_startup(thread); + } + else + { + rt_kprintf("serial Thread Create Failed!\n"); + } } void mqt_init(void) {