static void serial_thread_entry(void parameter)
{
char ch_test;
rt_kprintf(“HQ enter serial_thread_entry\n “);
while (1)
{
/ 从串口读取一个字节的数据,没有读取到则等待接收信号量 /
while (rt_device_read(serial, -1, &ch_test, 1) != 1)//*
此处一直读不到数据**
{
rt_kprintf("HQ rt_device_read\n");
/* 阻塞等待接收信号量,等到信号量后再次读取数据 /
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
}
/ 读取到的数据通过串口错位输出 */
ch_test = ch_test + 1;
rt_kprintf("HQ %d\n",ch_test);
rt_device_write(serial, 0, &ch_test, 1);
}
}
static int uart_sample(void)
{
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
char str[] = “hello RT-Thread!\r\n”;
rt_kprintf(“HQ enter uart_sample\n “);
/* 查找系统中的串口设备 /
serial = rt_device_find(SAMPLE_UART_NAME);
if (!serial)
{
rt_kprintf("find %s failed!\n", SAMPLE_UART_NAME);
return RT_ERROR;
}
/ 初始化信号量 /
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
/ 以中断接收及轮询发送模式打开串口设备 /
rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
/ 设置接收回调函数 /
rt_device_set_rx_indicate(serial, uart_input);
/ 发送字符串 /
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
{
ret = RT_ERROR;
}
return ret;
}
更多回帖