[PD-cvs] externals/iem/comport/comport comport.c,1.33,1.34

Martin Peach mrpeach at users.sourceforge.net
Tue Nov 13 19:31:47 CET 2007


Update of /cvsroot/pure-data/externals/iem/comport/comport
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29117

Modified Files:
	comport.c 
Log Message:
Correctly set port index when opened by name on linux & OSX.


Index: comport.c
===================================================================
RCS file: /cvsroot/pure-data/externals/iem/comport/comport/comport.c,v
retrieving revision 1.33
retrieving revision 1.34
diff -C2 -d -r1.33 -r1.34
*** comport.c	12 Oct 2007 02:56:08 -0000	1.33
--- comport.c	13 Nov 2007 18:31:45 -0000	1.34
***************
*** 17,20 ****
--- 17,21 ----
  MP 20070719 added "ports" method to output list of available ports on status outlet
  MP 20071011 added comport_list and write_serials for list processing based on code by Thomas O Fredericks <tof at danslchamp.org>
+ MP 20071113 modified non-windows open_serial to set the index of the port when it's opened by name
  */
  
***************
*** 436,440 ****
              return INVALID_HANDLE_VALUE;
          }
!     
          sprintf(buffer, "%s%d", x->serial_device_prefix, com_num);
          x->serial_device = gensym(buffer);
--- 437,441 ----
              return INVALID_HANDLE_VALUE;
          }
! 
          sprintf(buffer, "%s%d", x->serial_device_prefix, com_num);
          x->serial_device = gensym(buffer);
***************
*** 529,533 ****
      x->comhandle = fd;
  
! 	if (com_num == USE_DEVICENAME)
      {
          /* extract index from device name */
--- 530,534 ----
      x->comhandle = fd;
  
!     if (com_num == USE_DEVICENAME)
      {
          /* extract index from device name */
***************
*** 566,573 ****
          return INVALID_HANDLE_VALUE;
      }
! 	if (!SetupComm(x->comhandle, 4096L, 4096L))/* try to get big buffers to avoid overruns*/
! 	{
! 		post("[comport] Couldn't do SetupComm (%d)", GetLastError());
! 	}
      x->comport = com_num;/* output on next tick */
      return fd;
--- 567,574 ----
          return INVALID_HANDLE_VALUE;
      }
!     if (!SetupComm(x->comhandle, 4096L, 4096L))/* try to get big buffers to avoid overruns*/
!     {
!         post("[comport] Couldn't do SetupComm (%d)", GetLastError());
!     }
      x->comport = com_num;/* output on next tick */
      return fd;
***************
*** 600,604 ****
      DWORD      dwToWrite = 1L;
      DWORD      dwErr;
! 	DWORD      numTransferred = 0L;
  
      osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
--- 601,605 ----
      DWORD      dwToWrite = 1L;
      DWORD      dwErr;
!     DWORD      numTransferred = 0L;
  
      osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
***************
*** 618,626 ****
          }
      }
! 	if (!GetOverlappedResult(x->comhandle, &osWrite, &numTransferred, TRUE))
! 	{/* wait for the character to be sent */
          dwErr = GetLastError();
! 		post("WriteFile:GetOverlappedResult error: %d", (int)dwErr);
! 	}
      CloseHandle(osWrite.hEvent);
      return 1;
--- 619,627 ----
          }
      }
!     if (!GetOverlappedResult(x->comhandle, &osWrite, &numTransferred, TRUE))
!     {/* wait for the character to be sent */
          dwErr = GetLastError();
!         post("WriteFile:GetOverlappedResult error: %d", (int)dwErr);
!     }
      CloseHandle(osWrite.hEvent);
      return 1;
***************
*** 633,637 ****
      DWORD      dwToWrite = (DWORD)buf_length;
      DWORD      dwErr;
! 	DWORD      numTransferred = 0L;
  
      osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
--- 634,638 ----
      DWORD      dwToWrite = (DWORD)buf_length;
      DWORD      dwErr;
!     DWORD      numTransferred = 0L;
  
      osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
***************
*** 651,659 ****
          }
      }
! 	if (!GetOverlappedResult(x->comhandle, &osWrite, &numTransferred, TRUE))
! 	{/* wait for the character(s) to be sent */
          dwErr = GetLastError();
! 		post("WriteFile:GetOverlappedResult error: %d", (int)dwErr);
! 	}
      CloseHandle(osWrite.hEvent);
      return 1;
--- 652,660 ----
          }
      }
!     if (!GetOverlappedResult(x->comhandle, &osWrite, &numTransferred, TRUE))
!     {/* wait for the character(s) to be sent */
          dwErr = GetLastError();
!         post("WriteFile:GetOverlappedResult error: %d", (int)dwErr);
!     }
      CloseHandle(osWrite.hEvent);
      return 1;
***************
*** 709,715 ****
      while(i < BAUDRATETABLE_LEN && baudratetable[i] > *baud) i++;
  
! 	if(baudratetable[i] != *baud)
! 		post("[comport]: %d not valid, using closest value: ", *baud, baudratetable[i]);
! 	
      /* nearest Baudrate finding */
      if(i==BAUDRATETABLE_LEN ||  baudspeedbittable[i] < 0)
--- 710,716 ----
      while(i < BAUDRATETABLE_LEN && baudratetable[i] > *baud) i++;
  
!     if(baudratetable[i] != *baud)
!         post("[comport]: %d not valid, using closest value: ", *baud, baudratetable[i]);
! 
      /* nearest Baudrate finding */
      if(i==BAUDRATETABLE_LEN ||  baudspeedbittable[i] < 0)
***************
*** 719,723 ****
      }
      *baud =  baudratetable[i];
! 	post("get_baud_ratebits: %f", *baud);
  
      return baudspeedbittable[i];
--- 720,724 ----
      }
      *baud =  baudratetable[i];
!     post("get_baud_ratebits: %f", *baud);
  
      return baudspeedbittable[i];
***************
*** 727,736 ****
  {
      struct termios  *tio = &(x->com_termio);
!     speed_t            baudbits = get_baud_ratebits(&baud);
! 	post("set_baudrate baudbits: %d", baudbits);
      if( cfsetispeed(tio, baudbits) != 0 )
! 		post("[comport]: ERROR failed to set bitrate: %d", baudbits);
      if( cfsetospeed(tio, baudbits) != 0 )
! 		post("[comport]: ERROR failed to set bitrate: %d", baudbits);
  
      return baud;
--- 728,737 ----
  {
      struct termios  *tio = &(x->com_termio);
!     speed_t         baudbits = get_baud_ratebits(&baud);
!     post("set_baudrate baudbits: %d", baudbits);
      if( cfsetispeed(tio, baudbits) != 0 )
!         post("[comport]: ERROR failed to set bitrate: %d", baudbits);
      if( cfsetospeed(tio, baudbits) != 0 )
!         post("[comport]: ERROR failed to set bitrate: %d", baudbits);
  
      return baud;
***************
*** 849,921 ****
  static int set_hupcl(t_comport *x, int nr)
  {
! 	struct termios settings;
! 	int result;
  
! 	result = tcgetattr(x->comhandle, &settings);
! 	if (result < 0)
      {
! 		perror ("error in tcgetattr");
! 		return 0;
      }
! 	settings.c_iflag &= ~HUPCL;
! 	if(nr)
! 		settings.c_iflag |= HUPCL;
! 	result = tcsetattr(x->comhandle, TCSANOW, &settings);
! 	if (result < 0)
      {
! 		pd_error(x,"[comport] could not set HUPCL");
! 		return 0;
! 	}
! 	x->hupcl = nr;
! 	return 1;
  }
  
  static int open_serial(unsigned int com_num, t_comport *x)
  {
!     int            fd;
!     struct termios *old = &(x->oldcom_termio);
!     struct termios *new = &(x->com_termio);
!     float          *baud = &(x->baud);
!     glob_t         glob_buffer;
  
      /* if com_num == USE_DEVICENAME, use device name directly, else try port # */
!     if(com_num != USE_DEVICENAME)
      {
!         if(com_num >= COMPORT_MAX)
!         {
!             post("[comport] ** WARNING ** port %d not valid, must be between 0 and %d",
!                 com_num, COMPORT_MAX - 1);
!             return INVALID_HANDLE_VALUE;
!         }
!         /*  post("[comport] globbing %s",x->serial_device_prefix);*/
!         /* get the device path based on the port# and the glob pattern */
!         switch( glob( x->serial_device_prefix, 0, NULL, &glob_buffer ) )
!         {
!             case GLOB_NOSPACE:
!                 error("[comport] out of memory for \"%s\"",x->serial_device_prefix);
!                 break;
  #ifdef GLOB_ABORTED
!             case GLOB_ABORTED:
!                 error("[comport] aborted \"%s\"",x->serial_device_prefix);
!                 break;
  #endif
  #ifdef GLOB_NOMATCH
!             case GLOB_NOMATCH:
!                 error("[comport] no serial devices found for \"%s\"",x->serial_device_prefix);
!                 break;
  #endif
!         }
!         if(com_num < glob_buffer.gl_pathc)
!         {
!             x->serial_device = gensym(glob_buffer.gl_pathv[com_num]);
!         }
!         else
          {
!             post("[comport] ** WARNING ** port #%d does not exist! (max == %d)",
!                 com_num,glob_buffer.gl_pathc - 1);
!             return INVALID_HANDLE_VALUE;
          }
-         globfree( &(glob_buffer) );
      }
      if((fd = open(x->serial_device->s_name, OPENPARAMS)) == INVALID_HANDLE_VALUE)
      {
--- 850,930 ----
  static int set_hupcl(t_comport *x, int nr)
  {
!     struct termios  settings;
!     int             result;
  
!     result = tcgetattr(x->comhandle, &settings);
!     if (result < 0)
      {
!         perror ("error in tcgetattr");
!         return 0;
      }
!     settings.c_iflag &= ~HUPCL;
!     if(nr)
!     settings.c_iflag |= HUPCL;
!     result = tcsetattr(x->comhandle, TCSANOW, &settings);
!     if (result < 0)
      {
!         pd_error(x,"[comport] could not set HUPCL");
!         return 0;
!     }
!     x->hupcl = nr;
!     return 1;
  }
  
  static int open_serial(unsigned int com_num, t_comport *x)
  {
!     int             fd;
!     unsigned int    i;
!     struct termios  *old = &(x->oldcom_termio);
!     struct termios  *new = &(x->com_termio);
!     float           *baud = &(x->baud);
!     glob_t          glob_buffer;
  
      /* if com_num == USE_DEVICENAME, use device name directly, else try port # */
!     if((com_num != USE_DEVICENAME)&&(com_num >= COMPORT_MAX))
      {
!         post("[comport] ** WARNING ** port %d not valid, must be between 0 and %d",
!             com_num, COMPORT_MAX - 1);
!         return INVALID_HANDLE_VALUE;
!     }
!     /*  post("[comport] globbing %s",x->serial_device_prefix);*/
!     /* get the device path based on the port# and the glob pattern */
!     switch( glob( x->serial_device_prefix, 0, NULL, &glob_buffer ) )
!     {
!         case GLOB_NOSPACE:
!             error("[comport] out of memory for \"%s\"",x->serial_device_prefix);
!             break;
  #ifdef GLOB_ABORTED
!         case GLOB_ABORTED:
!             error("[comport] aborted \"%s\"",x->serial_device_prefix);
!             break;
  #endif
  #ifdef GLOB_NOMATCH
!         case GLOB_NOMATCH:
!             error("[comport] no serial devices found for \"%s\"",x->serial_device_prefix);
!             break;
  #endif
!     }
!     if (com_num == USE_DEVICENAME)
!     { /* if possible, find the index of the devicename */
!         for (i = 0; i < glob_buffer.gl_pathc; ++i)
          {
!             if (0 == strcmp(x->serial_device->s_name, glob_buffer.gl_pathv[i]))
!             {
!                 com_num = i;
!                 break;
!             }
          }
      }
+     else if(com_num < glob_buffer.gl_pathc)
+         x->serial_device = gensym(glob_buffer.gl_pathv[com_num]);
+     else
+     {
+         post("[comport] ** WARNING ** port #%d does not exist! (max == %d)",
+             com_num,glob_buffer.gl_pathc - 1);
+         return INVALID_HANDLE_VALUE;
+     }
+     globfree( &(glob_buffer) );
+ 
      if((fd = open(x->serial_device->s_name, OPENPARAMS)) == INVALID_HANDLE_VALUE)
      {
***************
*** 972,976 ****
      else
      {
! 		error("[comport] ** ERROR ** could not set params to ioctl of device %s\n",
              x->serial_device->s_name);
          close(fd);
--- 981,985 ----
      else
      {
!         error("[comport] ** ERROR ** could not set params to ioctl of device %s\n",
              x->serial_device->s_name);
          close(fd);
***************
*** 1065,1071 ****
  {
  #ifdef _WIN32
! 	HANDLE       fd = x->comhandle;
  #else
! 	int  fd = x->comhandle;
  #endif /* _WIN32 */
      int          err;
--- 1074,1080 ----
  {
  #ifdef _WIN32
!     HANDLE       fd = x->comhandle;
  #else
!     int  fd = x->comhandle;
  #endif /* _WIN32 */
      int          err;
***************
*** 1104,1110 ****
  #else
      {
!         unsigned char serial_byte;
!         fd_set        com_rfds;
! 		int count = 0;
  
          FD_ZERO(&com_rfds);
--- 1113,1119 ----
  #else
      {
!         unsigned char   serial_byte;
!         fd_set          com_rfds;
!         int             count = 0;
  
          FD_ZERO(&com_rfds);
***************
*** 1116,1123 ****
              /*  while(    (err = read(fd,(char *) &serial_byte,1)) > 0){ */
              outlet_float(x->x_data_outlet, (t_float) serial_byte);
! 			++count;
          }
! //		if( count > 0)
! //			post("--- %d", count);
      }
  #endif
--- 1125,1132 ----
              /*  while(    (err = read(fd,(char *) &serial_byte,1)) > 0){ */
              outlet_float(x->x_data_outlet, (t_float) serial_byte);
!             ++count;
          }
! //      if( count > 0)
! //          post("--- %d", count);
      }
  #endif
***************
*** 1536,1543 ****
      HANDLE          fd;
      char            device_name[10];
! 	unsigned int    i;
      DWORD           dw;
      for(i = 1; i < COMPORT_MAX; i++)
! 	{
          sprintf(device_name, "%s%d", x->serial_device_prefix, i);
          fd = CreateFile( device_name,
--- 1545,1552 ----
      HANDLE          fd;
      char            device_name[10];
!     unsigned int    i;
      DWORD           dw;
      for(i = 1; i < COMPORT_MAX; i++)
!     {
          sprintf(device_name, "%s%d", x->serial_device_prefix, i);
          fd = CreateFile( device_name,
***************
*** 1732,1748 ****
  {
      if(x->comhandle == INVALID_HANDLE_VALUE)
! 		comport_output_status(x, gensym("open"), 0);
! 	else
! 		comport_output_status(x, gensym("open"), 1);
  }
  
  static void comport_devices(t_comport *x)
  {
! 	comport_output_print(x);
  }
  
  static void comport_info(t_comport *x)
  {
! 	comport_output_open_status(x);
      comport_output_port_status(x);
      comport_output_baud_rate(x);
--- 1741,1757 ----
  {
      if(x->comhandle == INVALID_HANDLE_VALUE)
!         comport_output_status(x, gensym("open"), 0);
!     else
!         comport_output_status(x, gensym("open"), 1);
  }
  
  static void comport_devices(t_comport *x)
  {
!     comport_output_print(x);
  }
  
  static void comport_info(t_comport *x)
  {
!     comport_output_open_status(x);
      comport_output_port_status(x);
      comport_output_baud_rate(x);





More information about the Pd-cvs mailing list