打印

单片机usart串口控制台大揭秘

[复制链接]
4289|21
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
xlsbz|  楼主 | 2014-10-19 14:47 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
沙发
xlsbz|  楼主 | 2014-10-19 14:48 | 只看该作者
PC机上的用个串口助手 sscom 就是聂小丁的那个 写的就很好

使用特权

评论回复
板凳
keer_zu| | 2014-10-19 15:18 | 只看该作者
好想法,我以前也有这个想法,是不是先把命令定义好。

使用特权

评论回复
地板
xlsbz|  楼主 | 2014-10-19 21:29 | 只看该作者
本帖最后由 xlsbz 于 2014-10-19 22:20 编辑
keer_zu 发表于 2014-10-19 15:18
好想法,我以前也有这个想法,是不是先把命令定义好。

也就随便弄几个命令。
不打算用串口助手控制,只打算串口助手显示。

使用特权

评论回复
5
xlsbz|  楼主 | 2014-10-19 21:55 | 只看该作者
第三部分

关于sscom,的回车命令。聂小梦的的回车有两种。

如果在sscom上面的显示区域,按电脑上的回车 发送0x0D,
在sscom下方区域,选择“发送新行”,则发送0x0D + 0x0A


使用特权

评论回复
6
xlsbz|  楼主 | 2014-10-19 22:01 | 只看该作者
本帖最后由 xlsbz 于 2014-10-19 22:06 编辑

第4部门

关于接收。
开辟数组用来存放每次敲入的字符,敲的再快  一般也就敲个一两个字符,为啥?就是中断的3.5个字节的超时处理的原因。
static uint8_t recMem[100] = {0};    /* 10 is enough in fact */
再开辟数组存放每次敲写回车之前的字符。
static uint8_t multiMem[100] = {0};
敲完毕回车后,新输入字符就从multiMem[0]开始放。因此需要定义一个cnt,一旦收到回车命令,cnt就清零。
就是前文版主提到的命令行,一般也不会太长



----------------------------------------------------------------------------------



使用特权

评论回复
7
xlsbz|  楼主 | 2014-10-19 22:18 | 只看该作者
有了前面的准备工作就剩写代码了。

u16 reccnt = 0;//新帧字节个数
u16 cnt =0;//第4部分提到的cnt,一旦收到回车,cnt就清零

if (收到新帧)
{
           获得新帧的 个数recCnt;
           CBUSART_Read(USARTx, recMem, 10); //新帧放到 recMem里面;

         if ((cnt + recCnt) < sizeof(multiMem)) {  // if (收到字符个数不会导致multiMem溢出)
           memcpy(&multiMem[cnt], recMem, recCnt);
           USART_Write(USARTx, &multiMem[cnt], recCnt);

          cnt = cnt + recCnt;

          if (收到回车0x0D)或者(收到回车0x0D+0X0A) {
             if ((multiMem[cnt - 1] == 0x0D)) {
                cnt = cnt;
             }
             else {
               cnt = cnt - 1;
             }
             /*-----------------------------------------------------------------------------------------------*/
             if (((multiMem[cnt - 2] == '?') && (cnt == 2))  {  //若收到?命令

                                  __IO uint16_t timeout = 1000;  
                 while ((0 != Printf(USARTx, "\r\n    help    ?    id  info"\
                                               "\r\n    time    hw   ver"\
                                               "\r\nmicroC>")) && (timeout--));
             }
             esleif ()//收到某命令
             elseif ()
             else {
                                  if (cnt > 1) {
                                    Printf(USARTx, "\r\nCommand not found..."\
                                                         "\r\nmicro>");
                                     }
                                 else {
                                 Printf(USARTx, "\r\nmicro>");                                 
                                  }

             }
             /*-----------------------------------------------------------------------------------------------*/
             cnt =0;            

         }
         else {
                                                          Printf(USARTx, "\r\nCommand not found..."\                                                         "\r\nmicro>");
         }
         
         
   
}

使用特权

评论回复
8
xlsbz|  楼主 | 2014-10-19 22:26 | 只看该作者
========================================================
完毕。


使用特权

评论回复
9
xlsbz|  楼主 | 2014-10-19 22:28 | 只看该作者
yyy71cj 发表于 2014-10-19 16:40
至于一般的数据收发,很多串口调试软件都可以。

还有一种软件,可以在上位机与下位机串口通信的过程中截获 ...

这个 vsnprintf((char*)print_buffer, sizeof(print_buffer), (char*)format, args);函数我不太熟。能不能有什么风险,如会造成溢出之类的。

使用特权

评论回复
10
xjhw| | 2014-10-20 10:55 | 只看该作者
还要写解析回调程序,控制台程序是一个架构,搭建好了使用很方便。

使用特权

评论回复
11
xlsbz|  楼主 | 2014-10-20 11:15 | 只看该作者
yyy71cj 发表于 2014-10-20 09:17
这个函数增加了安全机制 sizeof(print_buffer) 的处理,若print_buffer没问题,应该不会有什么风险。 ...

好的

使用特权

评论回复
12
xlsbz|  楼主 | 2014-10-21 18:26 | 只看该作者
xjhw 发表于 2014-10-20 10:55
还要写解析回调程序,控制台程序是一个架构,搭建好了使用很方便。

偶尔回调一下,什么都回调,累得慌

使用特权

评论回复
13
xlsbz|  楼主 | 2014-10-23 09:16 | 只看该作者
本帖最后由 xlsbz 于 2014-10-23 09:18 编辑


Command not found...
microCB>info

CPU unique id     = 5DB0030-30394134-43172850
Runtime Name     = micoCB
      Created     = Oct 23 2014, 08:55:56
Armcc version     = 5.03.0024
microCB>help

    help    ?     info  time
    can     mvb   ver   hw
microCB>time

hh:mm:ss:ms       = 0:05:31:952
ms                = 331952
max scan time     = 3
current scan time = 1
microCB>can tpdo

Command not found...
microCB>can rpdo

Command not found...
microCB>can1 rpdo

RPDO-Index[000] |  Time[000:05:40:933] | Message = 1001000000001h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[001] |  Time[000:05:40:944] | Message = 1002000000002h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[002] |  Time[000:05:40:955] | Message = 1003000000003h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[003] |  Time[000:05:40:967] | Message = 1004000000004h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[004] |  Time[000:05:40:978] | Message = 1005000000005h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[005] |  Time[000:05:40:989] | Message = 1404000000006h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[006] |  Time[000:05:41:001] | Message = 1403000000007h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[007] |  Time[000:05:41:012] | Message = 1402000000008h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[008] |  Time[000:05:41:023] | Message = 1401000000009h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[009] |  Time[000:05:41:035] | Message = 140600000000Ah | Data = 00 00 00 00 00 00 00 00 | Count = 0

mircrCB>can1 rpdo

RPDO-Index[000] |  Time[000:05:51:673] | Message = 1001000000001h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[001] |  Time[000:05:51:684] | Message = 1002000000002h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[002] |  Time[000:05:51:696] | Message = 1003000000003h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[003] |  Time[000:05:51:707] | Message = 1004000000004h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[004] |  Time[000:05:51:718] | Message = 1005000000005h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[005] |  Time[000:05:51:730] | Message = 1404000000006h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[006] |  Time[000:05:51:741] | Message = 1403000000007h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[007] |  Time[000:05:51:753] | Message = 1402000000008h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[008] |  Time[000:05:51:764] | Message = 1401000000009h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[009] |  Time[000:05:51:775] | Message = 140600000000Ah | Data = 00 00 00 00 00 00 00 00 | Count = 0

mircrCB>can1 rpdo

RPDO-Index[000] |  Time[000:05:52:174] | Message = 1001000000001h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[001] |  Time[000:05:52:185] | Message = 1002000000002h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[002] |  Time[000:05:52:196] | Message = 1003000000003h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[003] |  Time[000:05:52:207] | Message = 1004000000004h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[004] |  Time[000:05:52:218] | Message = 1005000000005h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[005] |  Time[000:05:52:230] | Message = 1404000000006h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[006] |  Time[000:05:52:240] | Message = 1403000000007h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[007] |  Time[000:05:52:251] | Message = 1402000000008h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[008] |  Time[000:05:52:263] | Message = 1401000000009h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[009] |  Time[000:05:52:273] | Message = 140600000000Ah | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[000] |  Time[000:05:52:285] | Message = 1001000000001h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[001] |  Time[000:05:52:296] | Message = 1002000000002h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[002] |  Time[000:05:52:307] | Message = 1003000000003h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[003] |  Time[000:05:52:318] | Message = 1004000000004h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[004] |  Time[000:05:52:329] | Message = 1005000000005h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[005] |  Time[000:05:52:340] | Message = 1404000000006h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[006] |  Time[000:05:52:351] | Message = 1403000000007h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[007] |  Time[000:05:52:363] | Message = 1402000000008h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[008] |  Time[000:05:52:373] | Message = 1401000000009h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[009] |  Time[000:05:52:385] | Message = 140600000000Ah | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[000] |  Time[000:05:52:396] | Message = 1001000000001h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[001] |  Time[000:05:52:406] | Message = 1002000000002h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[002] |  Time[000:05:52:418] | Message = 1003000000003h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[003] |  Time[000:05:52:429] | Message = 1004000000004h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[004] |  Time[000:05:52:440] | Message = 1005000000005h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[005] |  Time[000:05:52:451] | Message = 1404000000006h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[006] |  Time[000:05:52:462] | Message = 1403000000007h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[007] |  Time[000:05:52:473] | Message = 1402000000008h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[008] |  Time[000:05:52:484] | Message = 1401000000009h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[009] |  Time[000:05:52:496] | Message = 140600000000Ah | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[000] |  Time[000:05:52:506] | Message = 1001000000001h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[001] |  Time[000:05:52:518] | Message = 1002000000002h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[002] |  Time[000:05:52:529] | Message = 1003000000003h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[003] |  Time[000:05:52:539] | Message = 1004000000004h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[004] |  Time[000:05:52:551] | Message = 1005000000005h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[005] |  Time[000:05:52:562] | Message = 1404000000006h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[006] |  Time[000:05:52:573] | Message = 1403000000007h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[007] |  Time[000:05:52:584] | Message = 1402000000008h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[008] |  Time[000:05:52:595] | Message = 1401000000009h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[009] |  Time[000:05:52:607] | Message = 140600000000Ah | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[000] |  Time[000:05:52:618] | Message = 1001000000001h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[001] |  Time[000:05:52:629] | Message = 1002000000002h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[002] |  Time[000:05:52:639] | Message = 1003000000003h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[003] |  Time[000:05:52:651] | Message = 1004000000004h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[004] |  Time[000:05:52:662] | Message = 1005000000005h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[005] |  Time[000:05:52:673] | Message = 1404000000006h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[006] |  Time[000:05:52:684] | Message = 1403000000007h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[007] |  Time[000:05:52:695] | Message = 1402000000008h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[008] |  Time[000:05:52:706] | Message = 1401000000009h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[009] |  Time[000:05:52:717] | Message = 140600000000Ah | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[000] |  Time[000:05:52:729] | Message = 1001000000001h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[001] |  Time[000:05:52:739] | Message = 1002000000002h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[002] |  Time[000:05:52:750] | Message = 1003000000003h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[003] |  Time[000:05:52:762] | Message = 1004000000004h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[004] |  Time[000:05:52:772] | Message = 1005000000005h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[005] |  Time[000:05:52:784] | Message = 1404000000006h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[006] |  Time[000:05:52:795] | Message = 1403000000007h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[007] |  Time[000:05:52:806] | Message = 1402000000008h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[008] |  Time[000:05:52:817] | Message = 1401000000009h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[009] |  Time[000:05:52:828] | Message = 140600000000Ah | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[000] |  Time[000:05:52:839] | Message = 1001000000001h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[001] |  Time[000:05:52:850] | Message = 1002000000002h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[002] |  Time[000:05:52:862] | Message = 1003000000003h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[003] |  Time[000:05:52:872] | Message = 1004000000004h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[004] |  Time[000:05:52:884] | Message = 1005000000005h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[005] |  Time[000:05:52:895] | Message = 1404000000006h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[006] |  Time[000:05:52:905] | Message = 1403000000007h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[007] |  Time[000:05:52:917] | Message = 1402000000008h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[008] |  Time[000:05:52:928] | Message = 1401000000009h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[009] |  Time[000:05:52:940] | Message = 140600000000Ah | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[000] |  Time[000:05:52:951] | Message = 1001000000001h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[001] |  Time[000:05:52:962] | Message = 1002000000002h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[002] |  Time[000:05:52:974] | Message = 1003000000003h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[003] |  Time[000:05:52:985] | Message = 1004000000004h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[004] |  Time[000:05:52:996] | Message = 1005000000005h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[005] |  Time[000:05:53:008] | Message = 1404000000006h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[006] |  Time[000:05:53:019] | Message = 1403000000007h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[007] |  Time[000:05:53:030] | Message = 1402000000008h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[008] |  Time[000:05:53:042] | Message = 1401000000009h | Data = 00 00 00 00 00 00 00 00 | Count = 0
RPDO-Index[009] |  Time[000:05:53:053] | Message = 140600000000Ah | Data = 00 00 00 00 00 00 00 00 | Count = 0

microCB>

使用特权

评论回复
评分
参与人数 1威望 +6 收起 理由
keer_zu + 6 很给力!
14
keer_zu| | 2014-10-23 09:19 | 只看该作者
xlsbz 发表于 2014-10-23 09:16
Command not found...
microCB>info

看着挺像回事啊,不错。

使用特权

评论回复
15
xlsbz|  楼主 | 2014-10-23 09:26 | 只看该作者
keer_zu 发表于 2014-10-23 09:19
看着挺像回事啊,不错。

关键是DMA啊 要不然就够呛了。

使用特权

评论回复
16
xlsbz|  楼主 | 2014-10-23 09:34 | 只看该作者
本帖最后由 xlsbz 于 2014-10-23 09:37 编辑



右面是CAN分析仪  以后打算把这个所谓的控制台 留在出厂代码得了。

左面串口还差个接收周期没有显示。

使用特权

评论回复
17
keer_zu| | 2014-10-23 09:45 | 只看该作者
xlsbz 发表于 2014-10-23 09:26
关键是DMA啊 要不然就够呛了。

不用DMA试过没?什么结果?

使用特权

评论回复
评论
xlsbz 2014-10-23 09:51 回复TA
我是裸奔。没操作系统 不用DMA就稍微多耗点时间。 现在代码转一圈就是 那个scantime大概3ms。不用DMA那就一次串口1000个字节的话就100ms啦 115200 
18
keer_zu| | 2014-10-23 09:46 | 只看该作者
xlsbz 发表于 2014-10-23 09:34
右面是CAN分析仪  以后打算把这个所谓的控制台 留在出厂代码得了。

左面串口还差个接收周期没有显示。

有控制台很方便,比其他交互方式传递信息量大。

使用特权

评论回复
19
xlsbz|  楼主 | 2014-10-23 09:52 | 只看该作者



使用特权

评论回复
20
keer_zu| | 2014-10-23 10:01 | 只看该作者
xlsbz 发表于 2014-10-23 09:52

我之前做can的时候用过ti A8的平台,也是这样打印出来,这是时间戳是个问题,没法完全替换逻分仪。你把时间也打印了,这个时间准确吗?

使用特权

评论回复
评论
xlsbz 2014-10-23 10:59 回复TA
51的时候就搞过 1s 一千个帧没问题,收的话 
xlsbz 2014-10-23 10:58 回复TA
准啊!得在中断里面搞 和count是一样的。 
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

190

主题

1614

帖子

4

粉丝