/* ******************************************************************************** * Arduino_RcvFm_NXT.pde * Author : CH, Chen (Taiwan) * Date : 2011.5.21 * Description: Arduino communicating with NXT via RS485 * Arduino receiving message sending from NXT * DFRobot I/O Expansion Shield for Arduino (DFR0088) * 1. RO(Data Output) - Arduino D0(RX) * 2. RE(Receiver Enable) - Arduino D2 * 3. DE(Output Enable) - Arduino D2 - HIGH/LOW - Transmit/Receive enable * 4. DI(Data Input) - Arduino D1(TX) * 5. GND - NXT cable Pin 2/3 (GND) (Black/Red) * 6. A - NXT cable Pin 5 (DIGI0) (Yellow) * 7. B - NXT cable Pin 6 (DIGI1) (Blue) * 8. VCC - Arduino +5V * ******************************************************************************** */ const int DE_RE_PIN = 2; // The DFR0088 RS485 DE and RE pins are connected to Arduino D2, Set High/Low to enable Transmit/Receive data const int MSG_LENGTH_LIMIT = 16; char rcvByte; int byteIndex=0; char rcvMessage [MSG_LENGTH_LIMIT]; void printMessage() { Serial.print(byteIndex+1); Serial.println(" bytes received"); for (int xi=0; xi <= byteIndex; xi++) { if (xi < byteIndex) Serial.print(rcvMessage[xi]); else Serial.println(rcvMessage[xi]); rcvMessage[xi] = ' '; } } void setup() { pinMode(DE_RE_PIN, OUTPUT); Serial.begin(9600); } void loop() { digitalWrite(DE_RE_PIN, LOW); // Enable data receive if (Serial.available() > 0) { rcvByte = Serial.read(); if ((rcvByte == '@') || (byteIndex == MSG_LENGTH_LIMIT)) // End of message receiving { printMessage(); byteIndex = 0; Serial.flush(); } else { if ((rcvByte >= ' ') && (rcvByte <= 'z')) { rcvMessage[byteIndex] = rcvByte; byteIndex++; } } } }