标题: 单片机C语言程序 串口发送字符加变量问题 [打印本页]

作者: samxon    时间: 2024-5-11 08:54
标题: 单片机C语言程序 串口发送字符加变量问题
大家好!

我想在串口发送端发送字符串+变量,接收端分解出字符串和变量值再做相应处理。不成功,下面是代码部分,
通常在实际工程中,通过串口发送一个命令(比如我的命令是standby)加一个值(180),是怎么处理的。

请各位指点。
以下是测试代码:
                unsigned char  nbt=180;  //该值会变动
                 i=Read_key();  //读按键值
                if(i==7)
                {                                                                                                                                
                        while(Read_key()==i);   //等待按键释放
                        Write_DATA(0,tab[7]);                                
                        SendString("standby\n"+nbt);                                                               
                        delay(100);
                }


void SendString(unsigned char *str)
{
        while(*str!='\0')
        {        
                SendByte(*str);
                str++;        
        }
}


作者: xxxevery    时间: 2024-5-11 10:40
把数值也转化成ascii码,和命令码就可以一起发了,接收端再把数字的ascii码提出来
作者: lkc8210    时间: 2024-5-11 10:47
SendString("standby\n"+nbt);这种写法是错误的

应该分开写
uchar nbtBuff[4];
nbtBuff[0] = nbt/100%10 + '0';//按位取值转ASCII
nbtBuff[1] = nbt/10%10;
nbtBuff[2] = nbt%10;
nbtBuff[3] = 0;//字串结束

SendString("standby\n");
SendString(nbtBuff);
作者: xiaobendan001    时间: 2024-5-11 11:15
到了\0就结束了,后面即使有也不发了吧
作者: xiaobendan001    时间: 2024-5-11 14:27
lkc8210 发表于 2024-5-11 10:47
SendString("standby\n"+nbt);这种写法是错误的

应该分开写

楼主的意思是180这是一个字节的数据。不是三个字节
作者: LaoYuTou    时间: 2024-5-11 16:22
用sprintf全转为ascii码后在发送,不要混发。
作者: LaoYuTou    时间: 2024-5-11 16:36
                #include <stdio.h>
                unsigned char str[20];  //
                unsigned char  nbt=180;  //该值会变动
                 i=Read_key();  //读按键值
                if(i==7)
                {                                                                                                                                
                        while(Read_key()==i);   //等待按键释放
                        Write_DATA(0,tab[7]);   
                        sprintf(str,"standby%d\r\n",nbt);                             
                        SendString(str);                                                               
                        delay(100);
                }
作者: samxon    时间: 2024-5-11 17:38
LaoYuTou 发表于 2024-5-11 16:36
#include
                unsigned char str[20];  //
                unsigned char ...

串口助手接收到了-19456 ,是什么原因
[17:36:58.100]接收←standby-19456
[17:36:58.632]接收←standby-19456
[17:36:58.699]接收←standby-19456
[17:36:59.278]接收←standby-19456
[17:36:59.873]接收←standby-19456
[17:37:00.377]接收←standby-19456
作者: yzwzfyz    时间: 2024-5-11 21:01
发送只能按字节进行,
1、在发送端,将需要发送的东东一一拆解成字节,依序发出。
2、在接收端,按发送的拆解的协议,重新组装。
作者: cien_s    时间: 2025-2-10 16:00
samxon 发表于 2024-5-11 17:38
串口助手接收到了-19456 ,是什么原因
[17:36:58.100]接收←standby-19456
[17:36:58.632]接收←standby ...


                #include <stdio.h>
                unsigned char str[20];  //
                unsigned char  nbt=180;  //该值会变动
                 i=Read_key();  //读按键值
                if(i==7)
                {                                                                                                                                
                        while(Read_key()==i);   //等待按键释放
                        Write_DATA(0,tab[7]);   
                        sprintf(str,"standby%d\r\n",(unsigned int)nbt);                             
                        SendString(str);                                                               
                        delay(100);
                }
试试这样能行吗
作者: rundstedt    时间: 2025-2-11 06:04
cien_s 发表于 2025-2-10 16:00
#include
                unsigned char str[20];  //
                unsigned  ...

行是形,但这里隐藏的问题是楼主接收到以后不知道怎么解析。
作者: cien_s    时间: 2025-2-11 10:13
cien_s 发表于 2025-2-10 16:00
#include
                unsigned char str[20];  //
                unsigned  ...

或者用这种方式

unsigned char str[4];
str[0] = nbt/100%10 + '0';
str[1] = nbt/10%10 + '0';
str[2] = nbt%10 + '0';
str[3] = 0;
SendString("standby\r\n");
SendString(str);
SendString("\r\n")
这种方式占用资源会比较少




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1