硬汉嵌入式论坛

 找回密码
 立即注册
查看: 1191|回复: 4
收起左侧

[技术讨论] iar高版本中串口重定向的问题

[复制链接]

24

主题

50

回帖

122

积分

初级会员

积分
122
发表于 2024-8-27 18:33:46 | 显示全部楼层 |阅读模式
有没有大佬知道在IAR9.30版本中串口重定向scanf怎么重定向?听说是需要重定向read函数,printf重定向是重写的write,scanf应该是read吧,有大佬使用过吗?
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
117512
QQ
发表于 2024-8-28 09:22:00 | 显示全部楼层
此贴的处理方法就可以的。

IAR9.X printf串口底层重定向方法,否则提示Linker Error: "no definition for __write"
https://forum.anfulai.cn/forum.p ... 9542&fromuid=58
(出处: 硬汉嵌入式论坛)
回复

使用道具 举报

24

主题

50

回帖

122

积分

初级会员

积分
122
 楼主| 发表于 2024-8-28 10:07:19 | 显示全部楼层
eric2013 发表于 2024-8-28 09:22
此贴的处理方法就可以的。

IAR9.X printf串口底层重定向方法,否则提示Linker Error: "no definition fo ...

scanf重定向完不能用啊,
[C] 纯文本查看 复制代码
#include <LowLevelIOInterface.h>
#include <stdint.h>
#include "usart.h"
#include <stddef.h>

extern UART_HandleTypeDef hlpuart1;
// __no_init volatile unsigned char kbIO @0x1000;

#pragma module_name = "?__read"

// size_t __read(int handle, unsigned char *buffer, size_t size)
// {
//     if (buffer == 0)

//     {
//         /*
//          * This means that we should flush internal buffers.  Since we
//          * don't we just return.  (Remember, "handle" == -1 means that all
//          * handles should be flushed.)
//          */

//         return 0;
//     }
//     /* This template only writes to "standard out" and "standard err",
//      * for all other file handles it returns failure. */

//     if (handle != _LLIO_STDIN && handle != _LLIO_STDERR)
//     {
//         return _LLIO_ERROR;
//     }

//     /* Sending in normal mode */
//     if (HAL_UART_Receive(&hlpuart1, (uint8_t *)buffer, size, 0xFFFF) == HAL_OK)
//     {
//         return size;
//     }
//     else
//     {
//         return _LLIO_ERROR;
//     }
// }


size_t __read(int handle, unsigned char *buf, size_t bufSize)
{
  size_t nChars = 0; /* Check for stdin      (only necessary if FILE descriptors are enabled) */
  if (handle != 0)
  {
    return -1;
  }
  for (/*Empty*/; bufSize > 0; --bufSize)
  {
    unsigned char c = NULL;
    while (!(hlpuart1.Instance->ISR & 1 << 5))
      ;
    c = (uint8_t)hlpuart1.Instance->RDR & (uint8_t)0x00fff;
    if (c == 0)
      break;
    *buf++ = c;
    ++nChars;
  }
  return nChars;
}
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
117512
QQ
发表于 2024-8-28 10:54:29 | 显示全部楼层
zhouyalong 发表于 2024-8-28 10:07
scanf重定向完不能用啊,[mw_shl_code=c,true]#include
#include
#include "usart.h"

我的问题,没有细看你的问题,你的scanf不能用,我看错。

scanf我没用过。是修改read,你多试试。
回复

使用道具 举报

24

主题

50

回帖

122

积分

初级会员

积分
122
 楼主| 发表于 2024-8-28 11:04:02 | 显示全部楼层
eric2013 发表于 2024-8-28 10:54
我的问题,没有细看你的问题,你的scanf不能用,我看错。

scanf我没用过。是修改read,你多试试。

好的 谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-8-12 06:31 , Processed in 0.042813 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表