之前的版本其实在一定程度上有一定可用度了,但是,缺点在于换个波特率就需要重新烧写程序。这个比较令人恼火,使用不是很便捷。
其实,要做一个波特率切换也不是很麻烦,而且C++在字符串等处理上比C容易不少。这样,还是增加一下相应的功能。
常用的波特率250kps、500kps、1000kps,暂时在软件中支持这么多。增加代码如下:
void loop(void)
{
if (Serial.available() > 0)
{
String can_config_str = Serial.readString();
if (can_config_str == "250\n")
{
if (CAN.begin(CAN_250KBPS) == CAN_OK)
{
Serial.println("can baudrate is set as 250kps");
}
}
else if (can_config_str == "500\n")
{
if (CAN.begin(CAN_500KBPS) == CAN_OK)
{
Serial.println("can baudrate is set as 500kps");
}
}
else if (can_config_str == "1000\n")
{
if (CAN.begin(CAN_1000KBPS) == CAN_OK)
{
Serial.println("can baudrate is set as 1000kps");
}
}
else
{
Serial.println("wrong configureation was input!");
}
}
delay(100); // send data per 100ms
}
这样,就可以直接通过串口设置所需要的波特率。做一个测试如下:
效果还不错,几种波特率的设置都实现了。