Update of /cvsroot/pure-data/externals/iem/comport/comport In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29655
Modified Files: comport.c Log Message: The ports message now outputs index / path pairs on the status outlet.
Index: comport.c =================================================================== RCS file: /cvsroot/pure-data/externals/iem/comport/comport/comport.c,v retrieving revision 1.28 retrieving revision 1.29 diff -C2 -d -r1.28 -r1.29 *** comport.c 19 Jul 2007 15:26:57 -0000 1.28 --- comport.c 22 Jul 2007 19:10:50 -0000 1.29 *************** *** 59,63 **** #endif t_symbol *serial_device; ! char serial_device_name[FILENAME_MAX]; short comport; /* holds the comport # */ t_float baud; /* holds the current baud rate */ --- 59,63 ---- #endif t_symbol *serial_device; ! char serial_device_prefix[FILENAME_MAX];/* the device name without the number */ short comport; /* holds the comport # */ t_float baud; /* holds the current baud rate */ *************** *** 408,426 **** static HANDLE open_serial(unsigned int com_num, t_comport *x) { ! HANDLE fd; ! COMMTIMEOUTS timeouts; ! char buffer[MAX_PATH]; ! float *baud = &(x->baud); ! DWORD dw;
! if(com_num < 1 || com_num >= COMPORT_MAX) { ! post("comport number %d out of range (0-%d)", com_num, COMPORT_MAX); ! return INVALID_HANDLE_VALUE; } ! ! sprintf(buffer, "%s%d", x->serial_device_name, com_num); ! x->serial_device = gensym(buffer); ! post("Opening %s",x->serial_device->s_name); fd = CreateFile( x->serial_device->s_name, GENERIC_READ | GENERIC_WRITE, --- 408,436 ---- static HANDLE open_serial(unsigned int com_num, t_comport *x) { ! HANDLE fd; ! COMMTIMEOUTS timeouts; ! char buffer[MAX_PATH]; ! float *baud = &(x->baud); ! DWORD dw; ! int i; ! char *errStr;
! if (com_num != USE_DEVICENAME) { ! if(com_num < 1 || com_num >= COMPORT_MAX) ! { ! post("comport number %d out of range (1-%d)", com_num, COMPORT_MAX); ! return INVALID_HANDLE_VALUE; ! } ! ! sprintf(buffer, "%s%d", x->serial_device_prefix, com_num); ! x->serial_device = gensym(buffer); } ! else ! { ! sprintf(buffer, "\\.\%s", x->serial_device->s_name); /* assume the slashes were not prefixed by user */ ! x->serial_device = gensym(buffer); ! } ! post("Opening %s", &x->serial_device->s_name[4]);/* skip slashes and dot */ fd = CreateFile( x->serial_device->s_name, GENERIC_READ | GENERIC_WRITE, *************** *** 434,439 **** { dw = GetLastError(); ! post("** ERROR ** could not open device %s:\n failure(%d)\n", ! x->serial_device->s_name,dw); return INVALID_HANDLE_VALUE; } --- 444,470 ---- { dw = GetLastError(); ! switch (dw) ! { ! case 2: ! errStr = "ERROR_FILE_NOT_FOUND"; ! break; ! case 3: ! errStr = "ERROR_PATH_NOT_FOUND"; ! break; ! case 5: ! errStr = "ERROR_ACCESS_DENIED"; ! break; ! case 53: ! errStr = "ERROR_BAD_NETPATH"; ! break; ! case 123: ! errStr = "ERROR_INVALID_NAME"; ! break; ! default: ! errStr = " "; ! break; ! } ! post("** ERROR ** could not open device %s:\n failure(%d) %s\n", ! &x->serial_device->s_name[4], dw, errStr); return INVALID_HANDLE_VALUE; } *************** *** 444,448 **** { post("** ERROR ** could not get old dcb of device %s\n", ! x->serial_device->s_name); CloseHandle(fd); return INVALID_HANDLE_VALUE; --- 475,479 ---- { post("** ERROR ** could not get old dcb of device %s\n", ! &x->serial_device->s_name[4]); CloseHandle(fd); return INVALID_HANDLE_VALUE; *************** *** 454,458 **** { post("** ERROR ** could not get new dcb of device %s\n", ! x->serial_device->s_name);
CloseHandle(fd); --- 485,489 ---- { post("** ERROR ** could not get new dcb of device %s\n", ! &x->serial_device->s_name[4]);
CloseHandle(fd); *************** *** 485,497 **** x->comhandle = fd;
if(set_serial(x)) { post("[comport] opened serial line device %d (%s)\n", ! com_num,x->serial_device->s_name); } else { error("[comport] ** ERROR ** could not set params to control dcb of device %s\n", ! x->serial_device->s_name); CloseHandle(fd); return INVALID_HANDLE_VALUE; --- 516,535 ---- x->comhandle = fd;
+ if (com_num == USE_DEVICENAME) + { + /* extract index from device name */ + for (i = 0; x->serial_device->s_name[i] != 0; ++i) + if ((x->serial_device->s_name[i] >= '0') && (x->serial_device->s_name[i] <= '9')) + com_num = atoi(&x->serial_device->s_name[i]); + } if(set_serial(x)) { post("[comport] opened serial line device %d (%s)\n", ! com_num, &x->serial_device->s_name[4]); } else { error("[comport] ** ERROR ** could not set params to control dcb of device %s\n", ! &x->serial_device->s_name[4]); CloseHandle(fd); return INVALID_HANDLE_VALUE; *************** *** 519,523 **** post("[comport] Couldn't do SetupComm (%d)", GetLastError()); } - x->comport = com_num;/* output on next tick */ return fd; --- 557,560 ---- *************** *** 531,535 **** { post("[comport] ** ERROR ** couldn't reset params to DCB of device %s\n", ! x->serial_device->s_name); } if (!SetCommTimeouts(x->comhandle, &(x->old_timeouts))) --- 568,572 ---- { post("[comport] ** ERROR ** couldn't reset params to DCB of device %s\n", ! &x->serial_device->s_name[4]); } if (!SetCommTimeouts(x->comhandle, &(x->old_timeouts))) *************** *** 538,542 **** } CloseHandle(x->comhandle); ! post("[comport] closed %s",x->serial_device->s_name); } return INVALID_HANDLE_VALUE; --- 575,579 ---- } CloseHandle(x->comhandle); ! post("[comport] closed %s", &x->serial_device->s_name[4]); } return INVALID_HANDLE_VALUE; *************** *** 775,793 **** return INVALID_HANDLE_VALUE; } ! /* post("[comport] globbing %s",x->serial_device_name);*/ /* get the device path based on the port# and the glob pattern */ ! switch( glob( x->serial_device_name, 0, NULL, &glob_buffer ) ) { case GLOB_NOSPACE: ! error("[comport] out of memory for "%s"",x->serial_device_name); break; #ifdef GLOB_ABORTED case GLOB_ABORTED: ! error("[comport] aborted "%s"",x->serial_device_name); break; #endif #ifdef GLOB_NOMATCH case GLOB_NOMATCH: ! error("[comport] no serial devices found for "%s"",x->serial_device_name); break; #endif --- 812,830 ---- 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 *************** *** 1025,1037 **** /* for UNIX, this is a glob pattern for matching devices */ #ifdef _WIN32 ! const char *serial_device_name = "COM"; #else # ifdef __APPLE__ ! const char *serial_device_name = "/dev/tty.*"; # else # ifdef IRIX ! const char *serial_device_name = "/dev/ttyd*"; # else ! const char *serial_device_name = "/dev/tty[SU]*"; # endif /* IRIX */ # endif /* __APPLE__ */ --- 1062,1081 ---- /* for UNIX, this is a glob pattern for matching devices */ #ifdef _WIN32 ! /* ! According to http://msdn2.microsoft.com/en-us/library/aa363858.aspx ! To specify a COM port number greater than 9, ! use the following syntax: "\\.\COM10". ! This syntax works for all port numbers and hardware ! that allows COM port numbers to be specified. ! */ ! const char *serial_device_prefix = "\\.\COM"; #else # ifdef __APPLE__ ! const char *serial_device_prefix = "/dev/tty.*"; # else # ifdef IRIX ! const char *serial_device_prefix = "/dev/ttyd*"; # else ! const char *serial_device_prefix = "/dev/tty[SU]*"; # endif /* IRIX */ # endif /* __APPLE__ */ *************** *** 1041,1045 **** /* Open the Comport for RD and WR and get a handle */ /* this line should use a real serial device */ ! strncpy(test.serial_device_name, serial_device_name, strlen(serial_device_name)+1); test.baud = fbaud; test.data_bits = 8; /* default 8 data bits */ --- 1085,1089 ---- /* Open the Comport for RD and WR and get a handle */ /* this line should use a real serial device */ ! strncpy(test.serial_device_prefix, serial_device_prefix, strlen(serial_device_prefix)+1); test.baud = fbaud; test.data_bits = 8; /* default 8 data bits */ *************** *** 1054,1058 ****
x->comport = test.comport;/* com_num */ ! strncpy(x->serial_device_name,serial_device_name,strlen(serial_device_name)+1); x->serial_device = test.serial_device; /* we need this so 'help' doesn't crash */
--- 1098,1102 ----
x->comport = test.comport;/* com_num */ ! strncpy(x->serial_device_prefix,serial_device_prefix,strlen(serial_device_prefix)+1); x->serial_device = test.serial_device; /* we need this so 'help' doesn't crash */
*************** *** 1068,1072 **** { /* postings in open routine */ ! post("[comport] invalid handle for %s",x->serial_device_name); } else --- 1112,1116 ---- { /* postings in open routine */ ! post("[comport] invalid handle for %s", x->serial_device_prefix); } else *************** *** 1124,1131 **** { error("[comport] ** ERROR ** could not set baudrate of device %s\n", x->serial_device->s_name); } else if(x->verbose > 0) ! post("set baudrate of %s to %f\n",x->serial_device->s_name,x->baud); }
--- 1168,1184 ---- { error("[comport] ** ERROR ** could not set baudrate of device %s\n", + #ifdef _WIN32 + &x->serial_device->s_name[4]); + #else x->serial_device->s_name); + #endif } else if(x->verbose > 0) ! post("set baudrate of %s to %f\n", ! #ifdef _WIN32 ! &x->serial_device->s_name[4], x->baud); ! #else ! x->serial_device->s_name, x->baud); ! #endif }
*************** *** 1139,1147 **** { error("[comport] ** ERROR ** could not set bits of device %s\n", x->serial_device->s_name); return; } else if(x->verbose > 0) ! post("set bits of %s to %f\n",x->serial_device->s_name,f); x->data_bits = f; } --- 1192,1209 ---- { error("[comport] ** ERROR ** could not set bits of device %s\n", + #ifdef _WIN32 + &x->serial_device->s_name[4]); + #else x->serial_device->s_name); + #endif return; } else if(x->verbose > 0) ! post("set bits of %s to %f\n", ! #ifdef _WIN32 ! &x->serial_device->s_name[4], f); ! #else ! x->serial_device->s_name, f); ! #endif x->data_bits = f; } *************** *** 1157,1165 **** { error("[comport] ** ERROR ** could not set extra paritybit of device %s\n", x->serial_device->s_name); return; } else if(x->verbose > 0) ! post("[comport] set extra paritybit of %s to %f\n",x->serial_device->s_name,f); x->parity_bit = f; } --- 1219,1236 ---- { error("[comport] ** ERROR ** could not set extra paritybit of device %s\n", + #ifdef _WIN32 + &x->serial_device->s_name[4]); + #else x->serial_device->s_name); + #endif return; } else if(x->verbose > 0) ! post("[comport] set extra paritybit of %s to %f\n", ! #ifdef _WIN32 ! &x->serial_device->s_name[4], f); ! #else ! x->serial_device->s_name, f); ! #endif x->parity_bit = f; } *************** *** 1174,1182 **** { error("[comport] ** ERROR ** could not set extra stopbit of device %s\n", ! x->serial_device->s_name); return; } else if(x->verbose > 0) ! post("[comport] set extra stopbit of %s to %f\n",x->serial_device->s_name,f); x->stop_bits = f; } --- 1245,1262 ---- { error("[comport] ** ERROR ** could not set extra stopbit of device %s\n", ! #ifdef _WIN32 ! &x->serial_device->s_name[4]); ! #else ! x->serial_device->s_name); ! #endif return; } else if(x->verbose > 0) ! post("[comport] set extra stopbit of %s to %f\n", ! #ifdef _WIN32 ! &x->serial_device->s_name[4], f); ! #else ! x->serial_device->s_name, f); ! #endif x->stop_bits = f; } *************** *** 1191,1199 **** { error("[comport] ** ERROR ** could not set rts_cts of device %s\n", x->serial_device->s_name); return; } else if(x->verbose > 0) ! post("[comport] set rts-cts of %s to %f\n",x->serial_device->s_name,f); x->ctsrts = f; } --- 1271,1288 ---- { error("[comport] ** ERROR ** could not set rts_cts of device %s\n", + #ifdef _WIN32 + &x->serial_device->s_name[4]); + #else x->serial_device->s_name); + #endif return; } else if(x->verbose > 0) ! post("[comport] set rts-cts of %s to %f\n", ! #ifdef _WIN32 ! &x->serial_device->s_name[4], f); ! #else ! x->serial_device->s_name, f); ! #endif x->ctsrts = f; } *************** *** 1208,1215 **** { error("[comport] ** ERROR ** could not set dtr of device %s\n", x->serial_device->s_name); } else if(x->verbose > 0) ! post("[comport] set dtr of %s to %f\n",x->serial_device->s_name,f); }
--- 1297,1313 ---- { error("[comport] ** ERROR ** could not set dtr of device %s\n", + #ifdef _WIN32 + &x->serial_device->s_name[4]); + #else x->serial_device->s_name); + #endif } else if(x->verbose > 0) ! post("[comport] set dtr of %s to %f\n", ! #ifdef _WIN32 ! &x->serial_device->s_name[4], f); ! #else ! x->serial_device->s_name, f); ! #endif }
*************** *** 1223,1230 **** { error("[comport] ** ERROR ** could not set rts of device %s\n", x->serial_device->s_name); } else if(x->verbose > 0) ! post("[comport] set rts of %s to %f\n",x->serial_device->s_name,f); }
--- 1321,1337 ---- { error("[comport] ** ERROR ** could not set rts of device %s\n", + #ifdef _WIN32 + &x->serial_device->s_name[4]); + #else x->serial_device->s_name); + #endif } else if(x->verbose > 0) ! post("[comport] set rts of %s to %f\n", ! #ifdef _WIN32 ! &x->serial_device->s_name[4], f); ! #else ! x->serial_device->s_name, f); ! #endif }
*************** *** 1238,1246 **** { error("[comport] ** ERROR ** could not set xonxoff of device %s\n", ! x->serial_device->s_name); return; } else if(x->verbose > 0) ! post("[comport] set xonxoff of %s to %f\n",x->serial_device->s_name,f); x->xonxoff = f; } --- 1345,1362 ---- { error("[comport] ** ERROR ** could not set xonxoff of device %s\n", ! #ifdef _WIN32 ! &x->serial_device->s_name[4]); ! #else ! x->serial_device->s_name); ! #endif return; } else if(x->verbose > 0) ! post("[comport] set xonxoff of %s to %f\n", ! #ifdef _WIN32 ! &x->serial_device->s_name[4], f); ! #else ! x->serial_device->s_name, f); ! #endif x->xonxoff = f; } *************** *** 1273,1276 **** --- 1389,1395 ---- { x->serial_device = s; + if(x->comhandle != INVALID_HANDLE_VALUE) + comport_close(x); + x->comhandle = open_serial(USE_DEVICENAME,x); clock_delay(x->x_clock, x->x_deltime); *************** *** 1303,1310 **** unsigned int i; DWORD dw; - for(i = 1; i < COMPORT_MAX; i++) { ! sprintf(device_name, "\\.\COM%d", i);/* the recommended way to specify COMs above 9 */ fd = CreateFile( device_name, GENERIC_READ | GENERIC_WRITE, --- 1422,1428 ---- 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, GENERIC_READ | GENERIC_WRITE, *************** *** 1329,1345 ****
/* first look for registered devices in the filesystem */ ! switch( glob( x->serial_device_name, 0, NULL, &glob_buffer ) ) { case GLOB_NOSPACE: ! error("[comport] out of memory for "%s"",x->serial_device_name); break; # ifdef GLOB_ABORTED case GLOB_ABORTED: ! error("[comport] aborted "%s"",x->serial_device_name); break; # endif /* GLOB_ABORTED */ # ifdef GLOB_NOMATCH case GLOB_NOMATCH: ! error("[comport] no serial devices found for "%s"",x->serial_device_name); break; # endif /* GLOB_NOMATCH */ --- 1447,1463 ----
/* first look for registered devices in the filesystem */ ! 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 /* GLOB_ABORTED */ # ifdef GLOB_NOMATCH case GLOB_NOMATCH: ! error("[comport] no serial devices found for "%s"",x->serial_device_prefix); break; # endif /* GLOB_NOMATCH */ *************** *** 1362,1366 **** { /* the same as comport_enum except outputs list of available ports on status outlet */ unsigned int i, j = 0; ! int ports[COMPORT_MAX]; /* we don't know how many there might be but 99 is probably safe */ #ifdef _WIN32 HANDLE fd; --- 1480,1484 ---- { /* the same as comport_enum except outputs list of available ports on status outlet */ unsigned int i, j = 0; ! t_atom output_atom[2]; #ifdef _WIN32 HANDLE fd; *************** *** 1370,1374 **** for(i = 1; i < COMPORT_MAX; i++) { ! sprintf(device_name, "\\.\COM%d", i);/* the recommended way to specify COMs above 9 */ fd = CreateFile( device_name, GENERIC_READ | GENERIC_WRITE, --- 1488,1492 ---- for(i = 1; i < COMPORT_MAX; i++) { ! sprintf(device_name, "%s%d", x->serial_device_prefix, i); fd = CreateFile( device_name, GENERIC_READ | GENERIC_WRITE, *************** *** 1383,1387 **** else CloseHandle(fd); ! if ((dw == 0)||(dw == ERROR_ACCESS_DENIED)) ports[j++]=i; } #else --- 1501,1510 ---- else CloseHandle(fd); ! if ((dw == 0)||(dw == ERROR_ACCESS_DENIED)) ! { /* output index and name as a list */ ! SETFLOAT(&output_atom[0], i); ! SETSYMBOL(&output_atom[1], gensym(&device_name[4]));/* strip the slashes and dot */ ! outlet_anything( x->x_status_outlet, gensym("ports"), 2, output_atom); ! } } #else *************** *** 1391,1407 ****
/* first look for registered devices in the filesystem */ ! switch( glob( x->serial_device_name, 0, NULL, &glob_buffer ) ) { case GLOB_NOSPACE: ! error("[comport] out of memory for "%s"",x->serial_device_name); break; # ifdef GLOB_ABORTED case GLOB_ABORTED: ! error("[comport] aborted "%s"",x->serial_device_name); break; # endif /* GLOB_ABORTED */ # ifdef GLOB_NOMATCH case GLOB_NOMATCH: ! error("[comport] no serial devices found for "%s"",x->serial_device_name); break; # endif /* GLOB_NOMATCH */ --- 1514,1530 ----
/* first look for registered devices in the filesystem */ ! 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 /* GLOB_ABORTED */ # ifdef GLOB_NOMATCH case GLOB_NOMATCH: ! error("[comport] no serial devices found for "%s"",x->serial_device_prefix); break; # endif /* GLOB_NOMATCH */ *************** *** 1414,1429 **** /* now see if it has attributes */ if ((tcgetattr(fd, &test)) != -1) ! ports[j++] = i;/* this one really exists */ close (fd); } } #endif /* _WIN32 */ - if (j) - { - t_atom *output_atom = getbytes(j*sizeof(t_atom)); - for (i = 0; i < j; ++i) SETFLOAT(&output_atom[i], ports[i]); - outlet_anything( x->x_status_outlet, gensym("ports"), j, output_atom); - freebytes(output_atom, j*sizeof(t_atom)); - } }
--- 1537,1549 ---- /* now see if it has attributes */ if ((tcgetattr(fd, &test)) != -1) ! { /* output index and name as a list */ ! SETFLOAT(&output_atom[0], i); ! SETSYMBOL(&output_atom[1], gensym(glob_buffer.gl_pathv[i])); ! outlet_anything( x->x_status_outlet, gensym("ports"), 2, output_atom); ! } close (fd); } } #endif /* _WIN32 */ }
*************** *** 1527,1531 **** if(x->comport >= 0 && x->comport < COMPORT_MAX) { ! post("\tdevicename: %s",x->serial_device->s_name); }
--- 1647,1655 ---- if(x->comport >= 0 && x->comport < COMPORT_MAX) { ! #ifdef WIN32 ! post("\tdevicename: %s", &x->serial_device->s_name[4]); ! #else ! post("\tdevicename: %s", x->serial_device->s_name); ! #endif }