<acronym id="s8ci2"><small id="s8ci2"></small></acronym>
<rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
<acronym id="s8ci2"></acronym>
<acronym id="s8ci2"><center id="s8ci2"></center></acronym>
0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

解決printf無法打印輸出的問題

撞上電子 ? 2024-01-04 08:00 ? 次閱讀

FreeRTOS中直接使用newlib庫是有問題的,相信使用過freertos進行printf都能發現這個問題,這個問題網上有兩種方法:1、使用printf.stdarg.c,問題在于,這個庫沒有包含float型的輸出!你沒辦法printf出浮點數。2、使用優化過的printf,這個能輸出float型,但是在中斷中如果使用float輸出,就會莫名其妙的整個程序卡住,我找不出bug。

static int inHandlerMode (void) //若在中斷中__get_IPSR()返回1,否則返回0{ return __get_IPSR();} void print_usart2(char *format, ...){ char buf[64]; if(inHandlerMode() != 0) { taskDISABLE_INTERRUPTS(); } else { while(HAL_UART_GetState(&huart2) == HAL_UART_STATE_BUSY_TX)//若串口忙則掛起此任務 taskYIELD(); } va_list ap; va_start(ap, format); vsprintf(buf, format, ap); HAL_UART_Transmit(&huart2, (uint8_t *)buf, strlen(buf), 100); va_end(ap); if(inHandlerMode() != 0) taskENABLE_INTERRUPTS();}

這破問題一直找不到bug在哪里,煩死了,我忍不了了!敲了個偽printf,思路很簡單,遍歷一遍要輸出的字符串,這過程中遇到%就標記,再遇到'.'這個字符就記錄一下'.'后面的數字,然后從va_list中根據%x 判斷一下屬于哪個類型,用va_arg讀取到值,再將該值的每個十進制位讀取成字符后放進輸出字符串里面,就完成了!代碼如下:

#include"iostream"#include "stdarg.h"using namespace std; void GetIntToString(char *target,int* target_site,int value,int num_value){char temp[20];int temp_site = 0,site = *target_site,flag = 0,neg_flag = 0;
if(num_value != 0)flag = 1;if(value < 0){neg_flag = 1;value = -value;}
while(value){temp[temp_site++] = (value % 10) + '0';value /= 10;}if(neg_flag)target[site++] = '-';while(temp_site--){if(flag){if(num_value != 0)num_value --;elsebreak;}target[site++] = temp[temp_site];}*target_site = site;} void my_sprintf(char* target,char* string,...){va_list next_value;int percent_flag = 0; // mark the emergence of percentage signint num_value = 0,num_flag = 0; // mark the value from the back of %.int target_site = 0,i = 0,value;double float_value;char *string_value;
va_start(next_value,string);while(string[i] != '\0'){if(string[i] == '%'){percent_flag = 1;}else if(percent_flag && string[i] == '.'){num_value = string[i+1] - '0';i++;}else if(percent_flag && percent_flag && (string[i] == 'd' || string[i] == 'c')){value = va_arg(next_value,int);if(value == 0)target[target_site++] = '0';elseGetIntToString(target,&target_site,value,num_value);
percent_flag = num_value = 0;}else if(percent_flag && string[i] == 's'){value = va_arg(next_value,int);string_value = (char *)value;for(int j = 0;string_value[j] != '\0';j++)target[target_site++] = string_value[j];
percent_flag = num_flag = num_value = 0;}else if(percent_flag && string[i] == 'f'){float_value = va_arg(next_value,double); // 2.14if((int)float_value == 0)target[target_site++] = '0';elseGetIntToString(target,&target_site,(int)float_value,0); // 2if(float_value < 0)float_value = -float_value;float_value -= (int)float_value;target[target_site++] = '.'; // 2.
if(num_value != 0)num_flag = 1;while(!(float_value >= -0.000001 && float_value <= 0.000001)){if(num_flag){if(num_value != 0)num_value --;elsebreak;}float_value *= 10;target[target_site++] = (int)float_value + '0';float_value -= (int)float_value;} // 2.14
percent_flag = num_flag = num_value = 0;}elsetarget[target_site++] = string[i];i++;}target[target_site] = '\0';va_end(next_value);} void my_printf(char* string,...){va_list next_value;int percent_flag = 0; // mark the emergence of percentage signint num_value = 0,num_flag = 0; // mark the value from the back of %.int target_site = 0,i = 0,value;double float_value;char target[100];char *string_value;
va_start(next_value,string);while(string[i] != '\0'){if(string[i] == '%'){percent_flag = 1;}else if(percent_flag && string[i] == '.'){num_value = string[i+1] - '0';i++;}else if(percent_flag && percent_flag && (string[i] == 'd' || string[i] == 'c')){value = va_arg(next_value,int);if(value == 0)target[target_site++] = '0';elseGetIntToString(target,&target_site,value,num_value);
percent_flag = num_value = 0;}else if(percent_flag && string[i] == 's'){value = va_arg(next_value,int);string_value = (char *)value;for(int j = 0;string_value[j] != '\0';j++)target[target_site++] = string_value[j];
percent_flag = num_flag = num_value = 0;}else if(percent_flag && string[i] == 'f'){float_value = va_arg(next_value,double); // 2.14if((int)float_value == 0)target[target_site++] = '0';elseGetIntToString(target,&target_site,(int)float_value,0); // 2if(float_value < 0)float_value = -float_value;float_value -= (int)float_value;target[target_site++] = '.'; // 2.
if(num_value != 0)num_flag = 1;while(!(float_value >= -0.000001 && float_value <= 0.000001)){if(num_flag){if(num_value != 0)num_value --;elsebreak;}float_value *= 10;target[target_site++] = (int)float_value + '0';float_value -= (int)float_value;} // 2.14
percent_flag = num_flag = num_value = 0;}elsetarget[target_site++] = string[i];i++;}target[target_site] = '\0';va_end(next_value);
for(int i = 0;i < target_site ;i ++) // 在devc++ 調試時的輸出 printf("%c",target[i]);} int main(){uint8_t k = 10;char target[100];char a = -10,b = -100;my_printf("MotorRun,%d,%d\n",a,b);printf("%c %c",55,32);//my_printf("temp value is %c and %d\n",k,k);//my_printf("you are so %s,yes \n%f","handsome",0.09);}

不過精度的問題,不知道怎么改,所以使用這個程序輸出float應該限制小數點長度。代碼應該不難看懂,不過這段代碼還沒有實現字符寬度輸出,也就是沒有實現%后跟著數字的輸出,在用該代碼也不應該在%后加數字。突然發現這個函數的target字符串 就可以作為sprintf的輸出耶,把得到target字符串的那一大串代碼包裝一下,就是sprintf了。在單片機的代碼:通過uart2輸出:

static char getint_temp[20];void GetIntToString(char *target,int* target_site,int value,int num_value){int temp_site = 0,site = *target_site;uint8_t flag = 0,neg_flag = 0; if(num_value != 0)flag = 1;if(value < 0){neg_flag = 1;value = -value;}while(value){getint_temp[temp_site++] = (value % 10) + '0';value /= 10;}if(neg_flag)target[site++] = '-';while(temp_site--){if(flag){if(num_value != 0)num_value --;elsebreak;}target[site++] = getint_temp[temp_site];}*target_site = site;} uint8_t percent_flag; // mark the emergence of percentage signuint8_t num_value,num_flag; // mark the value from the back of %.int target_site;int i,value;double float_value;char* string_value;char target[100];void my_printf(char* string,...){#if USE_PRINTF if(inHandlerMode() != 0){ taskDISABLE_INTERRUPTS();} else {while(HAL_UART_GetState(&huart2) == HAL_UART_STATE_BUSY_TX) //若串口忙則掛起此任務taskYIELD();} va_list next_value;i = target_site = percent_flag = num_flag = num_value = 0; va_start(next_value,string);while(string[i] != '\0'){if(string[i] == '%'){percent_flag = 1;}else if(percent_flag && string[i] == '.'){num_value = string[i+1] - '0';i++;}else if(percent_flag && (string[i] == 'd' || string[i] == 'c')){value = va_arg(next_value,int);if(value == 0)target[target_site++] = '0';elseGetIntToString(target,&target_site,value,num_value); percent_flag = num_value = 0;}else if(percent_flag && string[i] == 's'){value = va_arg(next_value,int);string_value = (char *)value;for(int j = 0;string_value[j] != '\0';j++)target[target_site++] = string_value[j]; percent_flag = num_flag = num_value = 0;}else if(percent_flag && string[i] == 'f'){float_value = va_arg(next_value,double); // 2.14if((int)float_value == 0)target[target_site++] = '0';elseGetIntToString(target,&target_site,(int)float_value,0); // 2if(float_value < 0)float_value = -float_value;float_value -= (int)float_value;target[target_site++] = '.'; // 2. if(num_value != 0)num_flag = 1;while(!(float_value >= -0.000001 && float_value <= 0.000001)){if(num_flag){if(num_value != 0)num_value --;elsebreak;}float_value *= 10;target[target_site++] = (int)float_value + '0';float_value -= (int)float_value;} // 2.14 percent_flag = num_flag = num_value = 0;}elsetarget[target_site++] = string[i];i++;}//target[target_site++] = '\0';va_end(next_value); HAL_UART_Transmit(&PRINTF_UART, (uint8_t *)target, target_site, 100); if(inHandlerMode() != 0)taskENABLE_INTERRUPTS();#endif} void my_sprintf(char* target,char* string,...){ if(inHandlerMode() != 0){ taskDISABLE_INTERRUPTS();} else{while(HAL_UART_GetState(&huart2) == HAL_UART_STATE_BUSY_TX) //若串口忙則掛起此任務taskYIELD();} va_list next_value;uint8_t percent_flag = 0; // mark the emergence of percentage signuint8_t num_value = 0,num_flag = 0; // mark the value from the back of %.int target_site = 0;int i = 0,value;double float_value;char *string_value; va_start(next_value,string);while(string[i] != '\0'){if(string[i] == '%'){percent_flag = 1;}else if(percent_flag && string[i] == '.'){num_value = string[i+1] - '0';i++;}else if(percent_flag && percent_flag && (string[i] == 'd' || string[i] == 'c')){value = va_arg(next_value,int);if(value == 0)target[target_site++] = '0';elseGetIntToString(target,&target_site,value,num_value); percent_flag = num_value = 0;}else if(percent_flag && string[i] == 's'){value = va_arg(next_value,int);string_value = (char *)value;for(int j = 0;string_value[j] != '\0';j++)target[target_site++] = string_value[j]; percent_flag = num_flag = num_value = 0;}else if(percent_flag && string[i] == 'f'){float_value = va_arg(next_value,double); // 2.14if((int)float_value == 0)target[target_site++] = '0';elseGetIntToString(target,&target_site,(int)float_value,0); // 2if(float_value < 0)float_value = -float_value;float_value -= (int)float_value;target[target_site++] = '.'; // 2. if(num_value != 0)num_flag = 1;while(!(float_value >= -0.000001 && float_value <= 0.000001)){if(num_flag){if(num_value != 0)num_value --;elsebreak;}float_value *= 10;target[target_site++] = (int)float_value + '0';float_value -= (int)float_value;} // 2.14 percent_flag = num_flag = num_value = 0;}elsetarget[target_site++] = string[i];i++;}target[target_site] = '\0';va_end(next_value); if(inHandlerMode() != 0)taskENABLE_INTERRUPTS();}

在freertos兩個任務中輸出,并且在串口中斷中輸出浮點數成功(突然發現,task拼成了tast。。

523ce930-aa94-11ee-8a62-92fbcf53809c.png

徹底解決!目前使用沒有bug,有bug我再來改文章。存在個bug,當傳入過多參數時,后面的參數會出現亂碼,如下

5243b85a-aa94-11ee-8a62-92fbcf53809c.png

最后一個%d會出現亂碼。將其分成兩個printf輸出,可以成功輸出。

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • FreeRTOS
    +關注

    關注

    12

    文章

    474

    瀏覽量

    61442
  • Printf
    +關注

    關注

    0

    文章

    80

    瀏覽量

    13498
收藏 人收藏

    評論

    相關推薦

    M451在Keil中,printf函數打印輸出到哪里了?

    在Keil中,printf函數,打印輸出到哪里了? 我的項目中沒有使用UART0,而是使用了UART3
    發表于 01-17 07:55

    STM32例程之串口打印輸出(源碼下載)

    STM32串口打印輸出,使用USART1輸出數據,用查詢的方式讀取從串口接收到的數據并打印輸出輸入的數據。主函數:/*** @brief串口打印輸出* @paramNone* @ret
    發表于 01-09 15:21

    使用printf打印輸出壓力值為什么會導致程序無法運行

    我用STM32F107片子對壓力傳感器MMR901XA 進行控制采集氣囊壓力值,在使用中為了能使用printf打印輸出壓力值,于是對printf進行了重定義,因為printf()之類的
    發表于 08-05 07:53

    重定向printf到串口打印輸出

    嵌入式的開發離不開 log 的打印,我們常常使用的是重定向printf到串口打印輸出,但是會對系統的實時性產生一定的影響,RTT技術可以在一定程度解決這個問題。
    發表于 08-24 07:11

    如何對printf()函數所依賴的打印輸出函數fputc進行重定向呢

    如何對printf()函數所依賴的打印輸出函數fputc進行重定向呢?
    發表于 11-30 06:28

    如何去使用printf這個C語言常用的打印輸出函數呢

    如何去實現基于C庫的printf函數呢?如何去使用printf這個C語言常用的打印輸出函數呢?
    發表于 11-30 06:41

    為什么用串口3 printf打印輸出會調試失敗呢

    為什么用串口3 printf打印輸出會調試失敗呢?是什么原因呢?如何去解決呢?
    發表于 11-30 07:24

    怎么去實現printf函數打印輸出

    什么是串口通信?同步通信與異步通信有何區別?怎么去實現printf函數打印輸出呢?
    發表于 12-01 07:12

    怎么實現printf作為串口打印輸出函數?

    怎么實現printf作為串口打印輸出函數?
    發表于 12-02 06:19

    如何實現STM32對printf打印輸出信息的支持呢

    如何實現STM32對printf打印輸出信息的支持呢?
    發表于 12-02 06:10

    使用printf進行打印輸出步驟記錄

    如何使用printf進行打印輸出呢?有哪些關鍵步驟?
    發表于 12-02 06:11

    在Keil中printf函數打印輸出到哪里了?

    在Keil中,printf函數,打印輸出到哪里了? 我的項目中沒有使用UART0,而是使用了UART3
    發表于 08-29 07:05

    什么是串口通信?基于STM32的printf打印輸出

    平時我們進行c語言編程的時候會經常用到printf函數進行打印輸出,來調試代碼??墒沁@個printf函數C庫已經幫我們實現好了,通常只需要直接調用即可,但是如果在一個新的開發平臺,如果庫沒有幫我們實現好,比如STM32開發板,那
    發表于 06-22 09:08 ?1.4w次閱讀
    什么是串口通信?基于STM32的<b class='flag-5'>printf</b><b class='flag-5'>打印輸出</b>

    簡述單片機常見的打印輸出方式及區別

    作者 |strongerHuang 微信公眾號 | 嵌入式專欄 單片機開發中,打印輸出比較常見,也比較重要,今天就為大家分享一下常見的打印輸出內容以及區別。 1寫在前面 在MCU項目中,printf
    的頭像 發表于 09-23 09:58 ?3118次閱讀
    簡述單片機常見的<b class='flag-5'>打印輸出</b>方式及區別

    單片機常見的打印輸出方式及區別

    單片機開發中,打印輸出比較常見,也比較重要,今天就為大家分享一下常見的打印輸出內容以及區別。
    發表于 02-08 15:13 ?0次下載
    單片機常見的<b class='flag-5'>打印輸出</b>方式及區別
    亚洲欧美日韩精品久久_久久精品AⅤ无码中文_日本中文字幕有码在线播放_亚洲视频高清不卡在线观看
    <acronym id="s8ci2"><small id="s8ci2"></small></acronym>
    <rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
    <acronym id="s8ci2"></acronym>
    <acronym id="s8ci2"><center id="s8ci2"></center></acronym>