/* Program ID : RS485Comm-Main.nxc Create date : 4-10-2011 Author : CH, Chen (Taiwan) Description : Controlling side of NXT Hi-Speed communication(RS485) over NXTBee testing */ #define NXTLIGHT_PORT S1 //Connect NXT Light sensor to this port /* ******************************************************* * 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); } } /* ******************************************************* * Select command and send to client NXT * ******************************************************* */ void Go_Rtn() { TextOut(0, LCD_LINE4, " (R)Red " ); TextOut(0, LCD_LINE5, " (G)Green " ); TextOut(0, LCD_LINE6, " (B)Blue " ); short menuIdx=0, remoteAction; long chkTick; string strAction, strDone; while(TRUE) { TextOut(0, LCD_LINE8, "Up Select Down" ); while(TRUE) { TextOut(0, (4-menuIdx)*8, ">> " ); Wait(200); if ((ButtonPressed(BTNCENTER, TRUE))) break; if ((ButtonPressed(BTNLEFT, TRUE))) { TextOut(0, (4-menuIdx)*8, " " ); menuIdx = ((menuIdx == 0)? 2: menuIdx-1); } else if ((ButtonPressed(BTNRIGHT, TRUE))) { TextOut(0, (4-menuIdx)*8, " " ); menuIdx = ((menuIdx == 2)? 0: menuIdx+1); } } strAction = SubStr("RGB" ,menuIdx, 1); TextOut(0, LCD_LINE8, " Sending ... " ); SendRS485String(strAction); while (RS485SendingData()); TextOut(0, LCD_LINE8, " Waiting for ACK" ); until(RS485DataAvailable()) Wait(50); // Adding delay is REQUIRED ! strDone = " " ; chkTick = CurrentTick(); until ((strDone=="DONE" )||((CurrentTick()-chkTick)>10000)) RS485Read(strDone); FlashLight(); } } /* ******************************************************* * Initialization * ******************************************************* */ void Init_Rtn() { 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_LINE8, "Initializing ..." ); Init_Rtn(); Go_Rtn(); }