首页 > 解决方案 > 通过串行读取字符串

问题描述

我想通过蓝牙(串行)模块从安卓应用程序中读取一个字符串到我的板上。该字符串由滑块确定,并且应该具有来自 的值-30 to 30

使用 MIT 应用程序生成器,变量的输出类型不清楚:

图片

它说send text,所以我假设这是一个字符串。我想在我的代码中读取这个字符串(使用mbed)。

使用:

#include "mbed.h"
#include <string>
#include <sstream>

Serial bluetooth (PTC17, PTC16);
Serial pc(USBTX, USBRX); 

void ReadSerial (void);
volatile char Slider;


int main()
{

    bluetooth.baud(38400);
    bluetooth.attach(&ReadSerial);
}    

void ReadSerial()
{
   Slider = bluetooth.getc();
    pc.printf("got string\n");
    pc.printf("string is %c\n", Slider);
}

打印到串行终端时给了我string is x或其他奇怪的符号。.string is £串行输出

我知道从字符串到字符的转换一定会出错,但我一直在寻找解决方案。为什么会发生这种情况,我怎样才能将此字符串直接写入我的代码中?

注意我也尝试使用bluetooth.gets(), 并printf("", stringname.c_str())达到相同的效果。

标签: c++mbed

解决方案


推荐阅读