一直以来都在看各位大佬的文章,一直在学习,自己也想着能做一些有趣的程序项目发表一下,也是自己第一次来写文章,如果有写的不好的,或是大家没有理解的地方还请大家能提出来!
自己准备开始做物联网这一块,开发平台确定是用Arduino,要开始涉及几种通信协议,其中一种就是串口,问题来了,手上只有Arduino,杜邦线和支持串口的设备,但是没有USB-TTL(USB转串口)模块来调试怎么办,这里就是想着自己做一个Arduino程序来进行串口调试,可以用来代替USB转串口模块(暂时不可完全代替串口助手)。
在此之前需要了解一下串口指令,已经会的朋友可以跳过这一步,有兴趣想学习的朋友可以参考一下这两篇,在这里感谢一下 iracer大大 和 jake_wolf大大的贡献。
Arduino - 串口操作函数与示例代码大全 by iracerr
Arduino串口的一些高级用法 by jake_wolf
将主控板的RXD1对应接到串口模块的TXD,并把主控板的TXD1对应接到串口模块的RXD.。
APP使用方法
进入APP后点击右上角<加号>,点击下拉栏选择<添加设备>,进入后点击DIY设备中的
调出这个调试框后我们就可以向已经连接到Arduino主控板的蓝牙模块进行调试了。
注意:
串口助手发送(Blinker接收):发送字符为偶位字符时,将会把字符进行十六进制到十进制的转换(串口设备不接收十六进制码,需要在这里转换成十进制)
串口助手接收(Blinker发送):串口助手会将接收到的所有字符(ASCII十进制)转换成正常的字符(ASCII符号)来显示。例:接收字符:65 ——> 转换后:A 。此外一点要注意的就是Blinker发送的的格式有以下这几种,由于APP功能博主也不是很清楚,所以介绍会放到以后来讲。
int DTS_val;
int DTS_return;
int DTS_return_1;
int DTS_return_2;
int ASCII_Extra;//额外内容
int line_count;//命令行计数
int line_state;
int ASCII_Control_Character;//控制字符
int ASCII_Display_Character;//可显示字符
int serial_begin = 115200;//串口波特率
void setup()
{
//Serial.begin(speed<串口波特率>,config<配置数据位、校验位、停止位>)
//config格式:SERIAL_<数据位5~8><校验位 N.无 E.偶 O.奇><停止位0~1>
//例:Serial.begin(115200,SERIAL_8N1);
Serial.begin(115200);
Serial1.begin(115200);
//***********初始化***********
line_count = 0;//命令行计数
//**********用户配置**********
line_state = 1;//0.不显示行数 1.显示行数
ASCII_Extra = 1;//0.不显示额外内容 1.显示额外内容
ASCII_Control_Character = 0;//0.不打印<控制字符> 1.打印<控制字符>
ASCII_Display_Character = 1;//0.不打印<可显示字符> 1.打印<可显示字符>
}
void loop()
{
//*************写入,TXD1,发送信息*************
if(Serial.available()>=2)
{
int available_count = Serial.available();
int serial_read_flash1 = Serial.read();
int serial_read_flash2 = Serial.read();
DecToSymbol(serial_read_flash1,serial_read_flash2);
Serial.print("写入<");
Serial.print(DTS_return);
Serial1.write(DTS_return);
Serial.print(" DEC>");
}
//*************写入,TXD1,发送信息*************
//*************读取,RXD1,传回信息*************
if(Serial1.available()>0)
{
//******行数******
if(line_state == 1)
{
Serial.print("<");
Serial.print(line_count);
line_count++;
Serial.print("> ");
}
//******行数******
while(Serial1.available() > 0)
{
delay(2);
int serial1_read_flash = Serial1.read();
if(ASCII_Control_Character == 1)
{
switch(serial1_read_flash)
{
case 0:Serial.print("NUL");if(ASCII_Extra == 1){Serial.print("<空字符>");}break;
case 1:Serial.print("SOH");if(ASCII_Extra == 1){Serial.print("<标题开始>");}break;
case 2:Serial.print("STX");if(ASCII_Extra == 1){Serial.print("<本文开始>");}break;
case 3:Serial.print("ETX");if(ASCII_Extra == 1){Serial.print("<本文结束>");}break;
case 4:Serial.print("EOT");if(ASCII_Extra == 1){Serial.print("<传输结束>");}break;
case 5:Serial.print("ENQ");if(ASCII_Extra == 1){Serial.print("<请求>");}break;
case 6:Serial.print("ACK");if(ASCII_Extra == 1){Serial.print("<确认回应>");}break;
case 7:Serial.print("BEL");if(ASCII_Extra == 1){Serial.print("<响铃>");}break;
case 8:Serial.print("BS");if(ASCII_Extra == 1){Serial.print("<退格>");}break;
case 9:Serial.print("HT");if(ASCII_Extra == 1){Serial.print("<水平定位符号>");}break;
case 10:Serial.print("LF");if(ASCII_Extra == 1){Serial.print("<换行键>");}break;
case 11:Serial.print("VT");if(ASCII_Extra == 1){Serial.print("<垂直定位符号>");}break;
case 12:Serial.print("FF");if(ASCII_Extra == 1){Serial.print("<换页键>");}break;
case 13:Serial.print("CR");if(ASCII_Extra == 1){Serial.print("<归位键>");}break;
case 14:Serial.print("SO");if(ASCII_Extra == 1){Serial.print("<取消变换(Shift out)>");}break;
case 15:Serial.print("SI");if(ASCII_Extra == 1){Serial.print("<启动变换(Shift in)>");}break;
case 16:Serial.print("DLE");if(ASCII_Extra == 1){Serial.print("<跳出数据通讯>");}break;
case 17:Serial.print("DC1");if(ASCII_Extra == 1){Serial.print("<设备控制一(XON启用软件速度控制)>");}break;
case 18:Serial.print("DC2");if(ASCII_Extra == 1){Serial.print("<设备控制二>");}break;
case 19:Serial.print("DC3");if(ASCII_Extra == 1){Serial.print("<设备控制三(XOFF 停用软件速度控制)>");}break;
case 20:Serial.print("DC4");if(ASCII_Extra == 1){Serial.print("<设备控制四>");}break;
case 21:Serial.print("NAK");if(ASCII_Extra == 1){Serial.print("<确认失败回应>");}break;
case 22:Serial.print("SYN");if(ASCII_Extra == 1){Serial.print("<同步用暂停>");}break;
case 23:Serial.print("ETB");if(ASCII_Extra == 1){Serial.print("<区块传输结束>");}break;
case 24:Serial.print("CAN");if(ASCII_Extra == 1){Serial.print("<取消>");}break;
case 25:Serial.print("EM");if(ASCII_Extra == 1){Serial.print("<连接介质中断>");}break;
case 26:Serial.print("SUB");if(ASCII_Extra == 1){Serial.print("<替换>");}break;
case 27:Serial.print("ESC");if(ASCII_Extra == 1){Serial.print("<跳出>");}break;
case 28:Serial.print("FS");if(ASCII_Extra == 1){Serial.print("<文件分割符>");}break;
case 29:Serial.print("GS");if(ASCII_Extra == 1){Serial.print("<组群分隔符>");}break;
case 30:Serial.print("RS");if(ASCII_Extra == 1){Serial.print("<记录分隔符>");}break;
case 31:Serial.print("US");if(ASCII_Extra == 1){Serial.print("<单元分隔符>");}break;
case 32:Serial.print(" ");if(ASCII_Extra == 1){Serial.print("<空格(SP)>");}break;
case 127:Serial.print("DEL");if(ASCII_Extra == 1){Serial.print("<删除>");}break;
}
}
if(ASCII_Display_Character == 1)
{
switch(serial1_read_flash)
{
case 33:Serial.print("!");break;
case 34:Serial.print(""");break;
case 35:Serial.print("#");break;
case 36:Serial.print("$");break;
case 37:Serial.print("%");break;
case 38:Serial.print("&");break;
case 39:Serial.print("'");break;
case 40:Serial.print("(");break;
case 41:Serial.print(")");break;
case 42:Serial.print("*");break;
case 43:Serial.print("+");break;
case 44:Serial.print(",");break;
case 45:Serial.print("-");break;
case 46:Serial.print(".");break;
case 47:Serial.print("/");break;
case 48:Serial.print("0");break;
case 49:Serial.print("1");break;
case 50:Serial.print("2");break;
case 51:Serial.print("3");break;
case 52:Serial.print("4");break;
case 53:Serial.print("5");break;
case 54:Serial.print("6");break;
case 55:Serial.print("7");break;
case 56:Serial.print("8");break;
case 57:Serial.print("9");break;
case 58:Serial.print(":");break;
case 59:Serial.print(";");break;
case 60:Serial.print("<");break;
case 61:Serial.print("=");break;
case 62:Serial.print(">");break;
case 63:Serial.print("?");break;
case 64:Serial.print("@");break;
case 65:Serial.print("A");break;
case 66:Serial.print("B");break;
case 67:Serial.print("C");break;
case 68:Serial.print("D");break;
case 69:Serial.print("E");break;
case 70:Serial.print("F");break;
case 71:Serial.print("G");break;
case 72:Serial.print("H");break;
case 73:Serial.print("I");break;
case 74:Serial.print("J");break;
case 75:Serial.print("K");break;
case 76:Serial.print("L");break;
case 77:Serial.print("M");break;
case 78:Serial.print("N");break;
case 79:Serial.print("O");break;
case 80:Serial.print("P");break;
case 81:Serial.print("Q");break;
case 82:Serial.print("R");break;
case 83:Serial.print("S");break;
case 84:Serial.print("T");break;
case 85:Serial.print("U");break;
case 86:Serial.print("V");break;
case 87:Serial.print("W");break;
case 88:Serial.print("X");break;
case 89:Serial.print("Y");break;
case 90:Serial.print("Z");break;
case 91:Serial.print("[");break;
case 92:Serial.print("\");break;
case 93:Serial.print("]");break;
case 94:Serial.print("^");break;
case 95:Serial.print("_");break;
case 96:Serial.print("`");break;
case 97:Serial.print("a");break;
case 98:Serial.print("b");break;
case 99:Serial.print("c");break;
case 100:Serial.print("d");break;
case 101:Serial.print("e");break;
case 102:Serial.print("f");break;
case 103:Serial.print("g");break;
case 104:Serial.print("h");break;
case 105:Serial.print("i");break;
case 106:Serial.print("j");break;
case 107:Serial.print("k");break;
case 108:Serial.print("l");break;
case 109:Serial.print("m");break;
case 110:Serial.print("n");break;
case 111:Serial.print("o");break;
case 112:Serial.print("p");break;
case 113:Serial.print("q");break;
case 114:Serial.print("r");break;
case 115:Serial.print("s");break;
case 116:Serial.print("t");break;
case 117:Serial.print("u");break;
case 118:Serial.print("v");break;
case 119:Serial.print("w");break;
case 120:Serial.print("x");break;
case 121:Serial.print("y");break;
case 122:Serial.print("z");break;
case 123:Serial.print("{");break;
case 124:Serial.print("|");break;
case 125:Serial.print("}");break;
case 126:Serial.print("~");break;
}
}
}
Serial.println();
}
//*************读取RXD1传回信息*************
}
void DecToSymbol(int DTS_val_1,int DTS_val_2)
{
Serial.print("输入<0x");
for(int i = 0; i < 2; i++)
{
if(i==0){DTS_val = DTS_val_1;}
if(i==1){DTS_val = DTS_val_2;}
if(DTS_val == 48){DTS_return = 0;}
else if(DTS_val == 49){DTS_return = 1;}
else if(DTS_val == 50){DTS_return = 2;}
else if(DTS_val == 51){DTS_return = 3;}
else if(DTS_val == 52){DTS_return = 4;}
else if(DTS_val == 53){DTS_return = 5;}
else if(DTS_val == 54){DTS_return = 6;}
else if(DTS_val == 55){DTS_return = 7;}
else if(DTS_val == 56){DTS_return = 8;}
else if(DTS_val == 57){DTS_return = 9;}
else if(DTS_val == 65){DTS_return = 10;}
else if(DTS_val == 66){DTS_return = 11;}
else if(DTS_val == 67){DTS_return = 12;}
else if(DTS_val == 68){DTS_return = 13;}
else if(DTS_val == 69){DTS_return = 14;}
else if(DTS_val == 70){DTS_return = 15;}
else if(DTS_val == 97){DTS_return = 10;}
else if(DTS_val == 98){DTS_return = 11;}
else if(DTS_val == 99){DTS_return = 12;}
else if(DTS_val == 100){DTS_return = 13;}
else if(DTS_val == 101){DTS_return = 14;}
else if(DTS_val == 102){DTS_return = 15;}
if(i==0)
{
DTS_return_1 = DTS_return;
Serial.print(DTS_return_1);
}
if(i==1)
{
DTS_return_2 = DTS_return;
Serial.print(DTS_return_2);
Serial.print(" HEX> ");
DTS_return = DTS_return_1*16 + DTS_return_2;
}
}
}