auxSerialExample.cpp

Demonstrates the use of a robot packet handler to recieve data from a device attached to the robot microcontroller's auxilliary serial port.

This example shows how to use the GETAUX command and how to recieve SERAUX serial port data. To use this example, you must have the auxilliary serial port AUX1 on the microcontroller (inside the robot) connected to a device that is sending text data.

You can connect AUX1 to a computer through an RS-232 cable with a NULL modem adapter, and run a program such as minicom (Linux) or HyperTerminal (Windows). Then just type to send data to the AUX1 port. Configure minicom or HyperTerminal to have the following serial port settings: 9600 baud 8 data bits, 1 stop bit, no parity No flow control (not hardware, not software)!

This program creates a packet handler function (getAuxPrinter) and adds it to the ArRobot object. All packets recieved from the robot are passed to all packet handlers. getAuxPrinter() checks whether the packet is an SERAUX packet, and if so, prints out the data contained within the packet. If a newline or carriage return character is recieved, then it sends a command to send data back out through the AUX serial port. The packet handler then sends a GETAUX command to the robot to request more data.

00001 #include "Aria.h"
00002 
00029 // our robot object
00030 ArRobot *robot;
00031 
00036 bool getAuxPrinter(ArRobotPacket *packet)
00037 {
00038   char c;
00039   
00040   // If this is not an aux. serial data packet, then return false to allow other
00041   // packet handlers to recieve the packet. The packet type ID numbers are found
00042   // in the description of the GETAUX commands in the robot manual.
00043   if (packet->getID() != 0xb0) // 0xB8 is SERAUXpac. SERAUX2pac is 0xB8, SERAUX3pac is 0xC8.
00044     return false;
00045 
00046   // Get bytes out of the packet buffer and print them.
00047   while (packet->getReadLength () < packet->getLength() - 2)
00048   {
00049     c = packet->bufToUByte();
00050     if (c == '\r' || c == '\n')
00051     {
00052       putchar('\n');
00053 
00054       // How to send data to the serial port. See robot manual
00055       // (but note that TTY2 is for the AUX1 port, TTY3 for AUX2, etc.)
00056       robot->comStr(ArCommands::TTY2, "Hello, World!\n\r");
00057     }
00058     else
00059       putchar(c);
00060     fflush(stdout);
00061   }
00062 
00063   
00064   // Request more data:
00065   robot->comInt(ArCommands::GETAUX, 1);
00066 
00067   // To request 12 bytes at a time, specify that instead:
00068   //robot->comInt(ArCommands::GETAUX, 12);
00069 
00070   // If you wanted to recieve information from the second aux. serial port, use
00071   // the GETAUX2 command instead; but the packet returned will also have a
00072   // different type ID.
00073   //robot->comInt(ArCommands::GETAUX2, 1);
00074   
00075 
00076   // Return true to indicate to ArRobot that we have handled this packet.
00077   return true;
00078 }
00079   
00080 
00081 int main(int argc, char **argv) 
00082 {
00083   Aria::init();
00084 
00085   ArArgumentParser argParser(&argc, argv);
00086   ArSimpleConnector conn(&argParser);
00087   argParser.loadDefaultArguments();
00088   if(!Aria::parseArgs() || !argParser.checkHelpAndWarnUnparsed())
00089   {
00090     Aria::logOptions();
00091     return 1;
00092   }
00093   
00094   // This is a global pointer so the global functions can use it.
00095   robot = new ArRobot;
00096 
00097   // functor for the packet handler
00098   ArGlobalRetFunctor1<bool, ArRobotPacket *> getAuxCB(&getAuxPrinter);
00099   // add our packet handler as the first one in the list
00100   robot->addPacketHandler(&getAuxCB, ArListPos::FIRST);
00101 
00102   // Connect to the robot
00103   if(!conn.connectRobot(robot))
00104   {
00105       ArLog::log(ArLog::Terse, "getAuxExample: Error connecting to the robot.");
00106       return 2;
00107   }
00108 
00109   ArLog::log(ArLog::Normal, "getAuxExample: Connected to the robot. Sending command to change AUX1 baud rate to 9600...");
00110   robot->comInt(ArCommands::AUX1BAUD, 0); // See robot manual
00111 
00112   // Send the first GETAUX request
00113   robot->comInt(ArCommands::GETAUX, 1);
00114 
00115   // If you wanted to recieve information from the second aux. serial port, use
00116   // the GETAUX2 command instead; but the packet returned will also have a
00117   // different type ID.
00118   //robot->comInt(ArCommands::GETAUX2, 1);
00119 
00120   // run the robot until disconnect, then shutdown and exit.
00121   robot->run(true);
00122   Aria::shutdown();
00123   return 0;  
00124 }
00125 

Generated on Fri Jul 31 12:36:37 2009 for Aria by  doxygen 1.4.7