arduino2560串口发送数据出错
我要让arduino2560串口发一个八位的数据,第1、2、8位的数据都为定值。主函数里设置的(‘M‘,10000)是设置的命令模式和值。第3位设置命令模式,将数值转化成第4、5、6、7位。
下载到板子上,Arduino IDE串口中可以接收到“U Eyeb” 等乱码的字符。
电机的驱动板可以收到Arduino发送过去的数据,但是不能执行先关命令。
这个程序可以正确的由串口输出吗?问题出在哪了?
short speed1;
unsigned char cmd = {0x19,0x88,0x00,0x00,0x00,0x00,0x00,0x11};
void setup()
{
// set uart baudrate to 115200
Serial.begin(115200);
delay(500);
// neurons motor module use 'auto baudrate' method to initialize its uart
Serial.print(0x55);
// optional delay
delay(500);
Serial.setTimeout(10);
}
// neurons motor module command
// always has 8 bytes
// cmd first, and always is 0x19
// cmd is always 0x88
// cmd is command code
// cmd is command data, MSB fisrt send
// cmd should be 0x11
// command code(cmd)
//'M' ---- set motor 1 speed to command data(cmd)
//'m' ---- set motor 2 speed to command data(cmd)
//'P' ---- set motor 1 P gain to command data(cmd), PID parameter
//'p' ---- set motor 2 P gain to command data(cmd), PID parameter
//'I' ---- set motor 1 I gain to command data(cmd), PID parameter
//'i' ---- set motor 2 I gain to command data(cmd), PID parameter
//'N' ---- set motor 1 intergral limit to command data(cmd), PID parameter
//'n' ---- set motor 2 intergral limit to command data(cmd), PID parameter
//'D' ---- set motor 1 D gain to command data(cmd), PID parameter
//'d' ---- set motor 2 d gain to command data(cmd), PID parameter
//'A' ---- set motor 1 speed increase to command data(cmd), in /second, MUST be positive value, trapezoid parameter
//'a' ---- set motor 2 speed increase to command data(cmd), in /second, MUST be positive value, trapezoid parameter
//'B' ---- set motor 1 max speed to command data(cmd), MUST be positive value, trapezoid parameter
//'b' ---- set motor 2 max speed to command data(cmd), MUST be positive value, trapezoid parameter
//'C' ---- set motor 1 speed decrease to command data(cmd), in /second, MUST be positive value, trapezoid parameter
//'c' ---- set motor 2 speed decrease to command data(cmd), in /second, MUST be positive value, trapezoid parameter
//'E' ---- set motor 1 distance to command data(cmd)
//'e' ---- set motor 2 distance to command data(cmd)
void SetCmd(unsigned char cmdSend, long motorSpeed)
{
// command
cmd=0x19;
cmd=0x88;
cmd = cmdSend;
// data
// send MSB first
cmd = (unsigned char)(motorSpeed >> 24);
cmd = (unsigned char)(motorSpeed >> 16);
cmd = (unsigned char)(motorSpeed >> 8);
cmd = (unsigned char)(motorSpeed);
cmd=0x11;
// send command
Serial.write(cmd,8);
return;
}
void loop()
{
// set
SetCmd('M',1000);
delay(2000);
SetCmd('M',-1000);
delay(2000);
} 波特率 没见过这么大的…… 为何不试试9600 频率下通信? 高速連接不是所有設備都可以穩定進行的.
先用 9600 測試好, 再慢慢上調吧. 输出格式再看看 码率不匹配吧
你在电脑上设置的码率和下位机的码率要匹配~~~~
不能一个9600一个高速率
还有就是楼上总结的很对 高码率对于线材要求相对更高,一般的线材难以负荷;
码率也不是越高越好,一般是能满足传输需要的最低码率为好。一味的求高码率 对于单片机也有压力的~ 再看到這個帖, 又發現一個問題, 這裡很多朋友也不了解 Serial.print 及 Serial.write 的分別.
樓主的程式中:
// command
cmd=0x19;
cmd=0x88;
cmd = cmdSend;
// data
// send MSB first
cmd = (unsigned char)(motorSpeed >> 24);
cmd = (unsigned char)(motorSpeed >> 16);
cmd = (unsigned char)(motorSpeed >> 8);
cmd = (unsigned char)(motorSpeed);
cmd=0x11;
// send command
Serial.write(cmd,8);
你期望在 Serial Monitor 中會看到什麼?
页:
[1]