/* Program ID : RS485Comm-Slave.nxc Create date : 4-8-2011 Author : CH, Chen (Taiwan) Description : Slave side of NXT Hi-Speed communication(RS485) over NXTBee testing */ #define NXTLIGHT_PORT S1 //Connect NXT Light sensor to this port #define NXTCOLOR_PORT S2 //Connect NXT Color sensor to this port /* ******************************************************* * Flashing the specified color of the NXT Color sensor * xcolor : NXT Color sensor type, * SENSOR_TYPE_COLORFULL, SENSOR_TYPE_COLORBLUE, * SENSOR_TYPE_COLORGREEN, SENSOR_TYPE_COLORRED * ******************************************************* */ void FlashColor(const byte xcolor) { for(short xii=0; xii<10; xii++) { SetSensorType(NXTCOLOR_PORT, xcolor); Wait(300); SetSensorColorNone(NXTCOLOR_PORT); Wait(100); } } /* ******************************************************* * Flashing the NXT Light sensor's light * ******************************************************* */ void FlashLight() { for(short xii=0; xii<3; xii++) { SetSensorLight(NXTLIGHT_PORT); Wait(300); SetSensorType(NXTLIGHT_PORT, SENSOR_TYPE_LIGHT_INACTIVE); Wait(100); } } /* ******************************************************* * Execute command * ******************************************************* */ void ExecuteCommand(string strAction) { TextOut(0, LCD_LINE8, "Executing ... " ); switch(strAction) { case "R" : FlashColor(SENSOR_TYPE_COLORRED); break; case "G" : FlashColor(SENSOR_TYPE_COLORGREEN); break; case "B" : FlashColor(SENSOR_TYPE_COLORBLUE); break; } } /* ******************************************************* * Waiting for command * ******************************************************* */ void Go_Rtn() { string strAction, strDone="DONE" ; TextOut(0, LCD_LINE4, "Command: " ); while(TRUE) { TextOut(54, LCD_LINE4, " " ); TextOut( 0, LCD_LINE8, "Listening ... " ); until(RS485DataAvailable()) Wait(50); RS485Read(strAction); TextOut(54, LCD_LINE4, strAction); TextOut( 0, LCD_LINE8, "Command received" ); FlashLight(); ExecuteCommand(strAction); TextOut( 0, LCD_LINE8, "Reply JobIsDone " ); SendRS485String(strDone); while (RS485SendingData()); } } /* ******************************************************* * Initialization * ******************************************************* */ void Init_Rtn() { // Configure Hi-Speed port(port 4) UseRS485(); RS485Control(HS_CTRL_INIT, HS_BAUD_9600, HS_MODE_8N1); RS485Uart(HS_BAUD_9600, HS_MODE_8N1); } /* ******************************************************* * Main routine * ******************************************************* */ task main() { ClearScreen(); TextOut(0, LCD_LINE1, "RS485 over XBee " ); TextOut(0, LCD_LINE2, " <> " ); TextOut(0, LCD_LINE4, " CH Lego (2011) " ); Init_Rtn(); Go_Rtn(); }