Dexter NXTBee (NXC)
Posted: 18 Jul 2012, 08:56
hi,
Looking through the following code (provided by Dexter) I am curious about a few things.
First, the receive program:
- at the start the variable byte mlen is declared but never used: what was it intended for?
- how can the receiver know how long the recieved string is, or does the code work with all strings of all lengths (which would be very convenient)?
- what is the maximum string length? 128? 256? 32767?
- a baud rate of 9600 has been used; is it possible to use a faster one? How much is possible?
for completion, this is the sender program (which I don't understand already completely by all details):
ps
will now be afk for 2 weeks!
Looking through the following code (provided by Dexter) I am curious about a few things.
First, the receive program:
- at the start the variable byte mlen is declared but never used: what was it intended for?
- how can the receiver know how long the recieved string is, or does the code work with all strings of all lengths (which would be very convenient)?
- what is the maximum string length? 128? 256? 32767?
- a baud rate of 9600 has been used; is it possible to use a faster one? How much is possible?
Code: Select all
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
////
//// NXTBEE RAW RECEIVE PROGRAM
//// NXTBee receiver program
//// For use with the NXTBee and Mindstorms NXT
//// www.dexterindustries.com
//// Use the latest release of NXC (1.2.1 r4 or later).
//// You can find it at http://bricxcc.sourceforge.net/nbc/
////
//// John Hansen helped adapt this code to NXC.
//// http://bricxcc.sourceforge.net/
////
//// More information on the NXTBee can be found at
//// http://www.dexterindustries.com/NXTBee.html
////
//// 3/21/2011
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
task main()
{
byte mlen;
string buffer;
// configure the S4 port as RS485
UseRS485();
// make sure the RS485 system is turned on
RS485Enable();
// initialize the UART to default values
RS485Initialize();
RS485Uart(HS_BAUD_9600, HS_MODE_DEFAULT); // use 9600 baud rather than default rate
Wait(MS_10);
while (true) {
// wait for a message to arrive.
until(RS485DataAvailable());
RS485Read(buffer);
// display message
TextOut(0, LCD_LINE2, buffer);
Wait(SEC_1);
}
// shut down the RS485 system
RS485Disable();
bool sendingData, dataAvail;
// check RS485 status
RS485Status(sendingData, dataAvail);
// turn the system back on (this is equivalent to RS485Enable)
RS485Control(HS_CTRL_INIT, HS_BAUD_DEFAULT, HS_MODE_DEFAULT);
// configure the UART (this is equivalent to RS485Initialize)
RS485Uart(HS_BAUD_DEFAULT, HS_MODE_DEFAULT);
}
Code: Select all
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
////
//// NXTBEE RAW SEND 4.0
//// NXTBee Sender program
//// For use with the NXTBee and Mindstorms NXT
//// www.dexterindustries.com
//// Use the latest release of NXC (1.2.1 r4 or later).
//// You can find it at http://bricxcc.sourceforge.net/nbc/
////
//// John Hansen helped adapt this code to NXC.
//// http://bricxcc.sourceforge.net/
////
//// More information on the NXTBee can be found at
//// http://www.dexterindustries.com/NXTBee.html
////
//// 3/21/2011
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void SendRS485Bool1(const bool value)
{
byte msg[];
ArrayBuild(msg, value, 0);
SetHSOutputBuffer(0, 2, msg);
SetHSOutputBufferOutPtr(0);
SetHSOutputBufferInPtr(2);
SetHSState(HS_SEND_DATA);
SetHSFlags(HS_UPDATE); //send it
}
void SendRS485Number1(const int value)
{
string msg = Flatten(value);
SetHSOutputBuffer(0, 5, msg);
SetHSOutputBufferOutPtr(0);
SetHSOutputBufferInPtr(5);
SetHSState(HS_SEND_DATA);
SetHSFlags(HS_UPDATE); //send it
}
void SendRS485String1(const string msg)
{
byte mlen = ArrayLen(msg);
SetHSOutputBuffer(0, mlen, msg);
SetHSOutputBufferOutPtr(0);
SetHSOutputBufferInPtr(mlen);
SetHSState(HS_SEND_DATA);
SetHSFlags(HS_UPDATE); //send it
}
inline void WaitForMessageToBeSent()
{
// use hi level API functions (preferred)
while (RS485SendingData())
Wait(MS_1);
}
task main()
{
// configure the S4 port as RS485
UseRS485();
// make sure the RS485 system is turned on
// hi level API function call
RS485Enable();
// initialize the UART to default values
// hi level API function call
RS485Uart(HS_BAUD_9600, HS_MODE_DEFAULT); // use 9600 baud rather than default rate
Wait(MS_10);
int i;
while (true) {
string msg;
//msg = "9";
msg = "goofy ";
msg += NumToStr(i);
TextOut(0, LCD_LINE1, msg);
SendRS485String(msg); // this is exactly the same as RS485Write but takes a string instead of a byte array
WaitForMessageToBeSent();
i++;
Wait(SEC_1);
}
// disable RS485 (not usually needed)
RS485Disable();
}
will now be afk for 2 weeks!