Update of /cvsroot/playerstage/code/player/server/drivers/rfid
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10644

Modified Files:
        rfi341_protocol.cc rfi341_protocol.h sickrfi341.cc 
Log Message:
finalized the sickrfi341 driver


Index: sickrfi341.cc
===================================================================
RCS file: /cvsroot/playerstage/code/player/server/drivers/rfid/sickrfi341.cc,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** sickrfi341.cc       14 Mar 2007 18:01:08 -0000      1.1
--- sickrfi341.cc       15 Mar 2007 12:06:45 -0000      1.2
***************
*** 118,122 ****
      // connection parameters
      const char* portName;
!     int         portSpeed;
      
      int debug;
--- 118,124 ----
      // connection parameters
      const char* portName;
!     int         connect_rate;
!     int         transfer_rate;
!     int         current_rate;
      
      int debug;
***************
*** 131,136 ****
  {
    // Read connection settings
!   portName  = cf->ReadString (section, "port", DEFAULT_RFI341_PORT);
!   portSpeed = cf->ReadInt    (section, "speed", DEFAULT_RFI341_RATE);
    
    debug = cf->ReadInt (section, "debug", 0);
--- 133,140 ----
  {
    // Read connection settings
!   portName = cf->ReadString (section, "port", DEFAULT_RFI341_PORT);
!   connect_rate  = cf->ReadInt (section, "connect_rate", DEFAULT_RFI341_RATE);
!   transfer_rate = cf->ReadInt (section, "transfer_rate", DEFAULT_RFI341_RATE);
!   current_rate  = 0;
    
    debug = cf->ReadInt (section, "debug", 0);
***************
*** 152,158 ****
  
    // Attempt to connect to the rfid unit
!   if (rfi341->Connect (portSpeed) != 0)
      return (-1);
  
    // Start the device thread
    StartThread ();
--- 156,173 ----
  
    // Attempt to connect to the rfid unit
!   if (rfi341->Connect (connect_rate) != 0)
      return (-1);
  
+   current_rate = connect_rate;
+   
+   if (connect_rate != transfer_rate)
+   {
+     // Attempt to connect to the rfid unit
+     if (rfi341->SetupSensor (transfer_rate) != 0)
+       return (-1);
+     else
+       current_rate = transfer_rate;
+   }
+ 
    // Start the device thread
    StartThread ();
***************
*** 169,172 ****
--- 184,191 ----
    StopThread ();
   
+   // Change back to the original speed
+   if (current_rate != connect_rate)
+     rfi341->SetupSensor (connect_rate);
+   
    // Disconnect from the rfid unit
    rfi341->Disconnect ();

Index: rfi341_protocol.cc
===================================================================
RCS file: 
/cvsroot/playerstage/code/player/server/drivers/rfid/rfi341_protocol.cc,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** rfi341_protocol.cc  14 Mar 2007 18:01:08 -0000      1.1
--- rfi341_protocol.cc  15 Mar 2007 12:06:45 -0000      1.2
***************
*** 17,23 ****
  rfi341_protocol::rfi341_protocol (const char* port_name, int debug_mode)
  {
!   port   = port_name;
!   verbose  = debug_mode;
!   tags = (char**)NULL;
    number_of_tags = 0;
  }
--- 17,23 ----
  rfi341_protocol::rfi341_protocol (const char* port_name, int debug_mode)
  {
!   port    = port_name;
!   verbose = debug_mode;
!   tags    = (char**)NULL;
    number_of_tags = 0;
  }
***************
*** 33,41 ****
    if (fd < 0)
    {
!   PLAYER_ERROR2 ("> Connecting to SICK RFI341 on [%s]; [%s]...[failed!]",
!            (char*) port, strerror (errno));
!   return (-1);
    }
-   PLAYER_MSG0 (1, "> Connecting to SICK RFI341...[done]");
    
    // Change port settings
--- 33,40 ----
    if (fd < 0)
    {
!     PLAYER_ERROR2 ("> Connecting to SICK RFI341 on [%s]; [%s]...[failed!]",
!                    (char*) port, strerror (errno));
!     return (-1);
    }
    
    // Change port settings
***************
*** 44,50 ****
    // Get the current port settings
    if (tcgetattr (fd, &options) != 0) {
!   PLAYER_ERROR (">> Unable to get serial port attributes !");
!   return (-1);
    }
  
    // turn off break sig, cr->nl, parity off, 8 bit strip, flow control
--- 43,50 ----
    // Get the current port settings
    if (tcgetattr (fd, &options) != 0) {
!     PLAYER_ERROR (">> Unable to get serial port attributes !");
!     return (-1);
    }
+   tcgetattr (fd, &initial_options);
  
    // turn off break sig, cr->nl, parity off, 8 bit strip, flow control
***************
*** 108,112 ****
  
    // Activate the settings for the port
!   if (tcsetattr (this->fd, TCSAFLUSH, &options) < 0)
    {
      PLAYER_ERROR (">> Unable to set serial port attributes !");
--- 108,112 ----
  
    // Activate the settings for the port
!   if (tcsetattr (fd, TCSAFLUSH, &options) < 0)
    {
      PLAYER_ERROR (">> Unable to set serial port attributes !");
***************
*** 114,119 ****
    }
  
    // Make sure queues are empty before we begin
!   tcflush (this->fd, TCIOFLUSH);
    
    return (0);
--- 114,120 ----
    }
  
+   PLAYER_MSG1 (1, "> Connecting to SICK RFI341 at %dbps...[done]", 
port_speed);
    // Make sure queues are empty before we begin
!   tcflush (fd, TCIOFLUSH);
    
    return (0);
***************
*** 126,132 ****
--- 127,137 ----
    rfi341_protocol::Disconnect ()
  {
+   // Close the serial port
+   tcsetattr (fd, TCSANOW, &initial_options);
    return (close (fd));
  }
  
+ 
////////////////////////////////////////////////////////////////////////////////
+ // Changes the transfer speed (or other parameters) of the RFI341 unit.
  int
    rfi341_protocol::SetupSensor (int transfer_speed)
***************
*** 194,207 ****
    // Tell sensor to change the baud rate
    char *c = (char*) malloc (10); 
!   sprintf (c, "1003%s", transferspeed_string);
!   SendCommand (c);
!   ReadResult ();
    
!   //OK, we told the sensor to change baud rate, so let's do it also
    struct termios options;
!   memset (&options, 0, sizeof (options));// clear the struct for new port 
settings
    
    // Get the current port settings
!   if (tcgetattr (this->fd, &options) != 0) {
      PLAYER_ERROR (">> Unable to get serial port attributes !");
      return (-1);
--- 199,216 ----
    // Tell sensor to change the baud rate
    char *c = (char*) malloc (10); 
!   while (strncmp ((const char*)buffer, "1003", 4) != 0)
!   {
!     sprintf (c, "1003%s", transferspeed_string);
!     SendCommand (c);
!     ReadResult ();
!   }
    
!   // OK, we told the sensor to change baud rate, so let's do it also
    struct termios options;
!   // clear the struct for new port settings
!   memset (&options, 0, sizeof (options));
    
    // Get the current port settings
!   if (tcgetattr (fd, &options) != 0) {
      PLAYER_ERROR (">> Unable to get serial port attributes !");
      return (-1);
***************
*** 212,216 ****
  
    // Activate the settings for the port
!   if (tcsetattr (this->fd, TCSAFLUSH, &options) < 0)
    {
      PLAYER_ERROR (">> Unable to set serial port attributes !");
--- 221,225 ----
  
    // Activate the settings for the port
!   if (tcsetattr (fd, TCSAFLUSH, &options) < 0)
    {
      PLAYER_ERROR (">> Unable to set serial port attributes !");
***************
*** 218,232 ****
    }
  
-   // Make sure queues are empty before we begin
-   tcflush (this->fd, TCIOFLUSH);
-   
- //  usleep (1000);
    // issue Interface Test request so the sensor knows the baud rate change 
went fine
!   if (buffer[0] == 0x06) // got ACK?
    {
      SendCommand ("1002");
      ReadResult  ();
      return (0);
    }
    return (-1);
  }
--- 227,240 ----
    }
  
    // issue Interface Test request so the sensor knows the baud rate change 
went fine
!   if (checksum == 0x05) // if checksum == 5, then it's only STX "1003" ETX
    {
      SendCommand ("1002");
      ReadResult  ();
+     PLAYER_MSG1 (1, "> Changing transfer speed to %dbps...[done]", 
transfer_speed);
      return (0);
    }
+   else
+     PLAYER_WARN1 ("> Checksum error [0x%x]!", checksum); 
    return (-1);
  }
***************
*** 261,287 ****
    // Read ACK
    int n = read (fd, buffer, 1);
!   if (n < 0)
!     printf ("Error!\n");
! //  else
! //  printf ("Read one byte: %s\n",(buffer[0]==0x06)?"ACK":"no ack... :(");
  
    // Read STX
    n = read (fd, buffer, 1);
  
    int read_count = 0;
!   do // read until we find ETX
    {
      n = read (fd, &buffer[read_count], 1);
      read_count += n;
!   } while (buffer[read_count-1]!=0x03);
  
    // don't forget to read checksum
    n = read (fd, &buffer[read_count], 1);
!   int checksum = buffer[read_count];
!   read_count+=n;
    
!   //TODO: check the checksum (that's what it's for!)
! 
!   buffer[read_count-2]=0x00;
    bufferlength = read_count-2;
  
--- 269,294 ----
    // Read ACK
    int n = read (fd, buffer, 1);
!   if (verbose && ((n < 0) || (buffer[0] != ACK)))
!     printf (">> Error reading ACK [0x%x]!\n", buffer[0]);
  
    // Read STX
    n = read (fd, buffer, 1);
+   if (verbose && ((n < 0) || (buffer[0] != STX)))
+     printf (">> Error reading STX [0x%x]!\n", buffer[0]);
  
    int read_count = 0;
!   do  // read until we find ETX
    {
      n = read (fd, &buffer[read_count], 1);
      read_count += n;
!   } while (buffer[read_count-1] != ETX);
  
    // don't forget to read checksum
    n = read (fd, &buffer[read_count], 1);
!   checksum = buffer[read_count];
!   read_count += n;
    
!   // TODO: check the checksum (that's what it's for!)
!   buffer[read_count-2] = 0x00;
    bufferlength = read_count-2;
  
***************
*** 292,301 ****
      printf ("ETX 0x%X\n", checksum); 
    }
- //  printf ("> %s\n", buffer);
    return (0);
  }
  
  
////////////////////////////////////////////////////////////////////////////////
! // adds a header and the checksum to the command to be sent
  int
    rfi341_protocol::assemblecommand (unsigned char* cmd, int len)
--- 299,307 ----
      printf ("ETX 0x%X\n", checksum); 
    }
    return (0);
  }
  
  
////////////////////////////////////////////////////////////////////////////////
! // Adds a header and the checksum to the command to be sent
  int
    rfi341_protocol::assemblecommand (unsigned char* cmd, int len)
***************
*** 304,316 ****
    int index = 0;
    
!   command[0]  = STX;      // Messages start with STX
    
    for (index = 0; index < len; index++)
!   command[index + 1]  = cmd[index];
  
!   command[1 + len] = ETX;     // Messages end with ETX
    
    for (int i = 0; i < len+2; i++)
!   checksum ^= command[i];
    
    command[2 + len] = checksum;
--- 310,322 ----
    int index = 0;
    
!   command[0] = STX;           // Messages start with STX
    
    for (index = 0; index < len; index++)
!     command[index + 1]  = cmd[index];
  
!   command[1 + len] = ETX;     // Messages end with ETX
    
    for (int i = 0; i < len+2; i++)
!     checksum ^= command[i];
    
    command[2 + len] = checksum;
***************
*** 321,324 ****
--- 327,333 ----
  }
  
+ 
////////////////////////////////////////////////////////////////////////////////
+ // Create an inventory of tags from the reader, fill in the Player data packet
+ // and return it
  player_rfid_data_t
    rfi341_protocol::ReadTags ()
***************
*** 328,341 ****
    
    // create inventory, single mode
!   SendCommand("6C20s");
!   ReadResult();
          
    // get inventory
!   SendCommand("6C21");
!   ReadResult();
    
!   if (tags!=NULL)
    {
!     for (int i=0; i < number_of_tags; i++)
        free (tags[i]);
      free (tags);
--- 337,350 ----
    
    // create inventory, single mode
!   SendCommand ("6C20s");
!   ReadResult  ();
          
    // get inventory
!   SendCommand ("6C21");
!   ReadResult  ();
    
!   if (tags != NULL)
    {
!     for (int i = 0; i < number_of_tags; i++)
        free (tags[i]);
      free (tags);
***************
*** 343,349 ****
  
    // read number of tags:
!   memcpy (buf, &buffer[4],4);
!   buf[4]=0;
! 
    number_of_tags = atol(buf);
    
--- 352,357 ----
  
    // read number of tags:
!   memcpy (buf, &buffer[4], 4);
!   buf[4] = 0;
    number_of_tags = atol(buf);
    
***************
*** 354,361 ****
  
    // read the tag UID's
!   for (int i=0; i < number_of_tags; i++)
    {
      memcpy (tags[i], &buffer[8+i*16], 16);
!     tags[i][16]=0;
    }
  
--- 362,369 ----
  
    // read the tag UID's
!   for (int i = 0; i < number_of_tags; i++)
    {
      memcpy (tags[i], &buffer[8+i*16], 16);
!     tags[i][16] = 0;
    }
  
***************
*** 363,382 ****
    player_rfid_data_t player_data;
    player_data.tags_count = number_of_tags;
!     player_rfid_tag_t tag;
    for (int i=0; i < number_of_tags; i++)
    {
!     tag.type = 1;
      tag.guid_count = 8;
!     for (int j=0; j<8; j++)
      {
        // transfer ASCII 0x30 into a char '0', f. ex.
        // this is done in steps of two digits (1 char)
!       sscanf(tags[i]+j*2 , "%2X", &hexnumber);
        tag.guid[7-j] = (char) hexnumber;
      }
!     player_data.tags[i]=tag;
    }
    return player_data;
- 
  }
- 
--- 371,389 ----
    player_rfid_data_t player_data;
    player_data.tags_count = number_of_tags;
!   
!   player_rfid_tag_t tag;
    for (int i=0; i < number_of_tags; i++)
    {
!     tag.type       = 1;
      tag.guid_count = 8;
!     for (int j = 0; j < 8; j++)
      {
        // transfer ASCII 0x30 into a char '0', f. ex.
        // this is done in steps of two digits (1 char)
!       sscanf (tags[i]+j*2, "%2X", &hexnumber);
        tag.guid[7-j] = (char) hexnumber;
      }
!     player_data.tags[i] = tag;
    }
    return player_data;
  }

Index: rfi341_protocol.h
===================================================================
RCS file: 
/cvsroot/playerstage/code/player/server/drivers/rfid/rfi341_protocol.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** rfi341_protocol.h   14 Mar 2007 18:01:08 -0000      1.1
--- rfi341_protocol.h   15 Mar 2007 12:06:45 -0000      1.2
***************
*** 5,8 ****
--- 5,9 ----
   CVS: $Id$
  */
+ #include <termios.h>
  #include <sys/types.h>
  #include <libplayercore/playercore.h>
***************
*** 45,48 ****
--- 46,52 ----
      char **tags;
  
+     // Initial serial port attributes
+     struct termios initial_options;
+ 
      // Internal Parameters:
      int verbose;
***************
*** 54,57 ****
--- 58,62 ----
      unsigned char buffer[4096];
      unsigned int bufferlength;
+     int checksum;
      
      // for sending:


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to