Arduino Nano Every的Serial1 readString通信异常求助
两台Arduino Nano Every的Serial1通信故障排查
我正在使用Serial1在两台Arduino Nano Every之间建立通信通道,代码包含通用通信类serialCommunication及主循环。通过GPIO引脚txRxPin将设备配置为两种模式:RC(遥控站)发送字符串后等待工作站响应,Machine(工作站)接收数据后回复。
代码在硬件回环测试中运行正常,但跨接两台设备(Rx与Tx交叉连接)后,接收方法rxData出现问题:双方均无法正常接收数据,且Machine端的readString语句会卡住;添加延迟无效果,替换为循环读取字符的逻辑虽能运行,但会丢失部分字符。
控制台输出
Machine端初始输出
Init started
Init finished; station mode=machine station
TP rxData 1
RC重置后Machine端重复57次的输出
TP rxData 2 8001|8001|8001|409 as received on machine
from machine to rc as transmitted from machine
RC端输出
RC Transmission start at 55512
Transmitted from RC on RC: 8001|8001|8001|77
Error #78; loop time = 500
Communication in time #78/0 - no data; loop time = 500
TP loop RC 1
代码
#define VERBOSE #include <Arduino.h> /*****************************************************/ #define txRxPin 15 class serialCommunication { public: void begin(); bool txData(String txStr); bool rxData(String &rxStr); bool available() { Serial1.available(); } } Comm; void serialCommunication::begin() { Serial1.begin(19200); while (!Serial1) ; } bool serialCommunication::txData(String txStr) { if (Serial1.available()) return false; digitalWrite(txRxPin, true); // If RS485 - transciever to TX mode Serial1.print(txStr); Serial1.flush(); digitalWrite(txRxPin, false); // If RS485 - transciever to TX mode return true; } bool serialCommunication::rxData(String &rxStr) { if (!Serial1.available()) return false; while (digitalRead(txRxPin)) ; Serial.println("TP rxData 1"); rxStr = Serial1.readString(); Serial.println("TP rxData 2"); return rxStr.length() > 0; } /*****************************************************/ /************* Main program **********************/ /*****************************************************/ #define txInterval 500 uint32_t timing; bool stationType; // low - machine, high - remote control mode int commError, previousPacketCounter, packetCounter; String announcement; /*****************************************************/ void setup() { Serial.begin(9600); while (!Serial) ; Serial.println("Init started"); pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, false); Comm.begin(); pinMode(txRxPin, INPUT); timing = millis(); packetCounter = 0; commError = 0; previousPacketCounter = 0; stationType = digitalRead(txRxPin); announcement = stationType ? "remote control station" : "machine station"; Serial.println("Init finished; station mode=" + announcement); delay(1000); } /*****************************************************/ void loop() { digitalWrite(LED_BUILTIN, true); if (stationType) { Serial.println("TP loop RC 1"); //******************** remote controller if ((millis() - timing) > txInterval) { Serial.println("\n\nRC Transmission start at " + String(millis() - timing)); String packet = "8001|"; packet += packet + packet + String(packetCounter++); // create the packet if (!Comm.txData(packet)) // Transmit the packet packetCounter--; // The packet was not transmittedso retain its number #ifdef VERBOSE Serial.println("Transmitted from RC on RC: " + packet); Serial.flush(); #endif // Receive machine reply long txTimer = millis(); while ((millis() - txTimer) < 10) ; // Seria;1 settling time while (!Comm.available() && ((millis() - txTimer) < txInterval)) ; Serial.println("Error #" + String(++commError) + "; loop time = " + String((millis() - txTimer))); String rcRxBuffer; bool rFlag = Comm.rxData(rcRxBuffer); #ifdef VERBOSE if (!rFlag) { Serial.println("Communication timeout, error# " + String(commError) + "/" + String(rFlag) + " - no data; loop time = " + String((millis() - txTimer))); return; } commError = 0; #endif Serial.println("Communication succeeded; rFlag=" + String(rFlag) + "; loop time = " + String((millis() - txTimer))); Serial.println("Data=<" + rcRxBuffer + ">"); // Serial.println(rcRxBuffer + " as received by RC; loop time = " + String(millis() - txTimer)); Serial.flush(); } } else { //******************** Machine Station String machineRxBuffer; if (Comm.rxData(machineRxBuffer)) { String reply = "from machine to rc "; //checkRxStr(machineRxBuffer); #ifdef VERBOSE Serial.println(machineRxBuffer + "\n as received on machine"); Serial.println(reply + " as transmitted from machine"); #endif Comm.txData(reply); } } timing = millis(); digitalWrite(LED_BUILTIN, true); }
内容的提问来源于stack exchange,提问作者user1511360
相关产品推荐
相关产品推荐

