int uart_write(int fd, char *send_buf,int data_len) { int len = 0; int ret; tcflush(fd,TCOFLUSH); tcflush(fd,TCIOFLUSH); len = write(fd,send_buf,data_len); if (len == data_len ) { ///printf("write %d bytes \r\n",len); return len; } else { tcflush(fd,TCOFLUSH); return 0; } }
时间: 2024-02-10 09:17:55 浏览: 28
这是一个函数,函数名为uart_write,它的作用是向指定的文件描述符fd中写入数据。数据存储在send_buf中,长度为data_len。函数使用了write函数向文件描述符写入数据,并返回实际写入的数据长度。如果实际写入的数据长度等于要写入的数据长度,表示数据写入成功,函数返回实际写入的数据长度。如果实际写入的数据长度不等于要写入的数据长度,则表示数据写入失败,函数返回0,并使用tcflush函数清空输入输出缓冲区。
相关问题
int aciga_action_in_proc(aciga_peer_device_t *src,uint8_t msgid, uint32_t runid, uint8_t aiid, aciga_service_data_t *svc ) { unsigned char cmd_buf[UART_SEND_CMD_BUF_SIZE]; int cmd_len; int i; int ret =-1; if((svc!=NULL)&&(aciga_action_in_scv_check(svc)!=0)) { ACIGA_LOGD("svc siid error"); return -1; } for( i = 0; i <ARRAY_SIZE(g_action_in_cmd_pro); i++ ) { if( aiid == g_action_in_cmd_pro[i].cmd && NULL != g_action_in_cmd_pro[i].func ) { ret = g_action_in_cmd_pro[i].func(msgid,runid,aiid,svc,(uint8_t *)&cmd_buf,&cmd_len); if(ret==0){ aciga_device_uart_send(src,cmd_buf,cmd_len); } return 0; } } return -1; }
这是一个名为aciga_action_in_proc的函数,该函数接受一些参数,包括一个aciga_peer_device_t类型的指针src、三个整型参数msgid、runid和aiid、以及一个aciga_service_data_t类型的指针svc。该函数返回一个整型值。
该函数首先检查svc指针是否非空,如果不是,则调用aciga_action_in_scv_check函数检查svc指向的aciga_service_data_t类型数据的siid是否正确。如果不正确,则返回-1。
接下来,该函数遍历一个名为g_action_in_cmd_pro的数组,并查找与参数aiid匹配的元素。如果找到了匹配的元素,则调用该元素中的函数,并将msgid、runid、aiid、svc等参数传递给该函数。该函数返回的结果存储在cmd_buf缓冲区中,并将cmd_buf中的数据通过src指向的设备发送出去。如果函数执行成功,则返回0。
如果遍历完整个数组都没有找到匹配的元素,则返回-1。
用cc2530单片机实现以下功能: 在#include "ioCC2530.h" #include <string.h> #define UART_TX_BUF_SIZE 64 unsigned char uart_tx_buf[UART_TX_BUF_SIZE]; void uart_init(void) { PERCFG |= 0x00; P0SEL |= 0x0C; P2DIR |= 0x01; P2SEL &= ~0x01; P2_0 = 0; U0CSR |= 0x80; U0GCR |= 11; U0BAUD = 216; } void uart_send_str(const char *str) { unsigned int len = strlen(str); if (len > UART_TX_BUF_SIZE - 1) { len = UART_TX_BUF_SIZE - 1; } memcpy(uart_tx_buf, str, len); uart_tx_buf[len] = '\0'; for (int i = 0; i < len; i++) { U0DBUF = uart_tx_buf[i]; while (!(U0CSR & 0x20)); } } int main(void) { uart_init(); for (int i = 0; i < 5; i++) { uart_send_str("安徽工商职业学院\\n"); } P2_0 = 1; while (1); return 0; }优化为 1.通过串口调试助手,在串口调试助手界面上显示“安徽工商职业学院” 2.字样“安徽工商职业学院”在调试助手界面上显示5次后停止显示。
根据你的要求,我可以给出以下优化方案:
1. 修改 uart_send_str 函数,直接发送字符串 "安徽工商职业学院\n",不需要使用缓存区。
```c
void uart_send_str(const char *str) {
while (*str != '\0') {
U0DBUF = *str++;
while (!(U0CSR & 0x20));
}
}
```
2. 在主函数中使用循环发送 5 次字符串 "安徽工商职业学院\n"。
```c
int main(void) {
uart_init();
for (int i = 0; i < 5; i++) {
uart_send_str("安徽工商职业学院\n");
}
while (1);
return 0;
}
```
这样就可以在串口调试助手界面上显示 5 次 "安徽工商职业学院" 字符串了。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)