Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

auxSerialExample.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "Aria.h"
00027 
00054 // our robot object
00055 ArRobot *robot;
00056 
00061 bool getAuxPrinter(ArRobotPacket *packet)
00062 {
00063   char c;
00064   
00065   // If this is not an aux. serial data packet, then return false to allow other
00066   // packet handlers to recieve the packet. The packet type ID numbers are found
00067   // in the description of the GETAUX commands in the robot manual.
00068   if (packet->getID() != 0xb0) // 0xB8 is SERAUXpac. SERAUX2pac is 0xB8, SERAUX3pac is 0xC8.
00069     return false;
00070 
00071   // Get bytes out of the packet buffer and print them.
00072   while (packet->getReadLength () < packet->getLength() - 2)
00073   {
00074     c = packet->bufToUByte();
00075     if (c == '\r' || c == '\n')
00076     {
00077       putchar('\n');
00078 
00079       // How to send data to the serial port. See robot manual
00080       // (but note that TTY2 is for the AUX1 port, TTY3 for AUX2, etc.)
00081       robot->comStr(ArCommands::TTY2, "Hello, World!\n\r");
00082     }
00083     else
00084       putchar(c);
00085     fflush(stdout);
00086   }
00087 
00088   
00089   // Request more data:
00090   robot->comInt(ArCommands::GETAUX, 1);
00091 
00092   // To request 12 bytes at a time, specify that instead:
00093   //robot->comInt(ArCommands::GETAUX, 12);
00094 
00095   // If you wanted to recieve information from the second aux. serial port, use
00096   // the GETAUX2 command instead; but the packet returned will also have a
00097   // different type ID.
00098   //robot->comInt(ArCommands::GETAUX2, 1);
00099   
00100 
00101   // Return true to indicate to ArRobot that we have handled this packet.
00102   return true;
00103 }
00104   
00105 
00106 int main(int argc, char **argv) 
00107 {
00108   Aria::init();
00109 
00110   ArArgumentParser argParser(&argc, argv);
00111   ArSimpleConnector conn(&argParser);
00112   argParser.loadDefaultArguments();
00113   if(!Aria::parseArgs() || !argParser.checkHelpAndWarnUnparsed())
00114   {
00115     Aria::logOptions();
00116     return 1;
00117   }
00118   
00119   // This is a global pointer so the global functions can use it.
00120   robot = new ArRobot;
00121 
00122   // functor for the packet handler
00123   ArGlobalRetFunctor1<bool, ArRobotPacket *> getAuxCB(&getAuxPrinter);
00124   // add our packet handler as the first one in the list
00125   robot->addPacketHandler(&getAuxCB, ArListPos::FIRST);
00126 
00127   // Connect to the robot
00128   if(!conn.connectRobot(robot))
00129   {
00130       ArLog::log(ArLog::Terse, "getAuxExample: Error connecting to the robot.");
00131       return 2;
00132   }
00133 
00134   ArLog::log(ArLog::Normal, "getAuxExample: Connected to the robot. Sending command to change AUX1 baud rate to 9600...");
00135   robot->comInt(ArCommands::AUX1BAUD, 0); // See robot manual
00136 
00137   // Send the first GETAUX request
00138   robot->comInt(ArCommands::GETAUX, 1);
00139 
00140   // If you wanted to recieve information from the second aux. serial port, use
00141   // the GETAUX2 command instead; but the packet returned will also have a
00142   // different type ID.
00143   //robot->comInt(ArCommands::GETAUX2, 1);
00144 
00145   // run the robot until disconnect, then shutdown and exit.
00146   robot->run(true);
00147   Aria::shutdown();
00148   return 0;  
00149 }
00150 

Generated on Tue Feb 20 10:51:42 2007 for Aria by  doxygen 1.4.0