made [devicename( message open the port by devicename directly; commented out code for 2nd inlet for status because it was causing segfaults :-/

svn path=/trunk/externals/iem/comport/; revision=5107
This commit is contained in:
Hans-Christoph Steiner 2006-05-22 13:55:14 +00:00
parent df122aae6a
commit 8b90624de8

View file

@ -62,6 +62,8 @@ typedef struct comport
int verbose; int verbose;
t_outlet *x_data_outlet;
t_outlet *x_status_outlet;
} t_comport; } t_comport;
#ifndef TRUE #ifndef TRUE
@ -406,6 +408,8 @@ static HANDLE open_serial(unsigned int com_num, t_comport *x)
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
}; };
// this causes a segfault... WHY?!?
// outlet_float(x->x_status_outlet, (t_float)com_num);
return fd; return fd;
} }
@ -416,15 +420,19 @@ static HANDLE close_serial(t_comport *x)
if (!SetCommState(x->comhandle, &(x->dcb_old)) ) if (!SetCommState(x->comhandle, &(x->dcb_old)) )
{ {
post("** ERROR ** could not reset params to DCB of device %s\n", post("[comport] ** ERROR ** could not reset params to DCB of device %s\n",
x->serial_device->s_name); x->serial_device->s_name);
} }
if (!SetCommTimeouts(x->comhandle, &(x->old_timeouts))){ if (!SetCommTimeouts(x->comhandle, &(x->old_timeouts))){
post("Couldnt reset old_timeouts for serial device"); post("[comport] Couldnt reset old_timeouts for serial device");
}; };
CloseHandle(x->comhandle); CloseHandle(x->comhandle);
// for some reason, this causes a segfault...
// post("[comport] closed %s",x->serial_device->s_name);
} }
// this causes a segfault... WHY?!?
// outlet_float(x->x_status_outlet, 0);
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
@ -557,13 +565,17 @@ static int open_serial(unsigned int com_num, t_comport *x)
float *baud = &(x->baud); float *baud = &(x->baud);
glob_t glob_buffer; glob_t glob_buffer;
if(com_num >= COMPORT_MAX) { /* if com_num == 9999, use device name directly, else try port # */
if(com_num != 9999)
{
if(com_num >= COMPORT_MAX)
{
post("[comport] ** WARNING ** port %d not valid, must be between 0 and %d", post("[comport] ** WARNING ** port %d not valid, must be between 0 and %d",
com_num, COMPORT_MAX - 1); com_num, COMPORT_MAX - 1);
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
// post("[comport] globbing %s",x->serial_device_name); // post("[comport] globbing %s",x->serial_device_name);
/* get the device path based on the com# and the glob pattern */ /* get the device path based on the port# and the glob pattern */
switch( glob( x->serial_device_name, 0, NULL, &glob_buffer ) ) switch( glob( x->serial_device_name, 0, NULL, &glob_buffer ) )
{ {
case GLOB_NOSPACE: case GLOB_NOSPACE:
@ -587,7 +599,7 @@ static int open_serial(unsigned int com_num, t_comport *x)
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
globfree( &(glob_buffer) ); globfree( &(glob_buffer) );
}
if((fd = open(x->serial_device->s_name, OPENPARAMS)) == INVALID_HANDLE_VALUE) if((fd = open(x->serial_device->s_name, OPENPARAMS)) == INVALID_HANDLE_VALUE)
{ {
error("[comport] ** ERROR ** could not open device %s:\n failure(%d): %s\n", error("[comport] ** ERROR ** could not open device %s:\n failure(%d): %s\n",
@ -640,6 +652,8 @@ static int open_serial(unsigned int com_num, t_comport *x)
{ {
post("[comport] opened serial line device %d (%s)\n", post("[comport] opened serial line device %d (%s)\n",
com_num,x->serial_device->s_name); com_num,x->serial_device->s_name);
// this causes a segfault... WHY?!?
// outlet_float(x->x_status_outlet, (t_float)com_num);
} }
else else
{ {
@ -661,6 +675,10 @@ static int close_serial(t_comport *x)
if(fd != INVALID_HANDLE_VALUE){ if(fd != INVALID_HANDLE_VALUE){
tcsetattr(fd, TCSANOW, tios); tcsetattr(fd, TCSANOW, tios);
close(fd); close(fd);
// for some reason, this causes a segfault...
// post("[comport] closed %s",x->serial_device->s_name);
// this causes a segfault... WHY?!?
// outlet_float(x->x_status_outlet, 0);
} }
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
@ -724,7 +742,7 @@ static void comport_tick(t_comport *x)
{ {
for(dwX=0;dwX<dwRead;dwX++) for(dwX=0;dwX<dwRead;dwX++)
{ {
outlet_float(x->x_obj.ob_outlet, (t_float) serial_byte[dwX]); outlet_float(x->x_data_outlet, (t_float) serial_byte[dwX]);
} }
} }
} }
@ -746,7 +764,7 @@ static void comport_tick(t_comport *x)
err = read(fd,(char *) &serial_byte,1); err = read(fd,(char *) &serial_byte,1);
/* while( (err = read(fd,(char *) &serial_byte,1)) > 0){ */ /* while( (err = read(fd,(char *) &serial_byte,1)) > 0){ */
outlet_float(x->x_obj.ob_outlet, (t_float) serial_byte); outlet_float(x->x_data_outlet, (t_float) serial_byte);
}; };
} }
@ -823,7 +841,9 @@ static void *comport_new(t_floatarg com_num, t_floatarg fbaud)
x->rxerrors = 0; /* holds the rx line errors */ x->rxerrors = 0; /* holds the rx line errors */
outlet_new(&x->x_obj, &s_float); x->x_data_outlet = (t_outlet *)outlet_new(&x->x_obj, &s_float);
// for some unknown reason, outputting on this outlet causes segfaults...
// x->x_status_outlet = (t_outlet *)outlet_new(&x->x_obj, &s_float);
x->x_hit = 0; x->x_hit = 0;
x->x_deltime = 1; x->x_deltime = 1;
@ -946,7 +966,6 @@ static void comport_open(t_comport *x, t_floatarg f)
if(x->comhandle != INVALID_HANDLE_VALUE) if(x->comhandle != INVALID_HANDLE_VALUE)
comport_close(x); comport_close(x);
x->comhandle = open_serial(f,x); x->comhandle = open_serial(f,x);
clock_delay(x->x_clock, x->x_deltime); clock_delay(x->x_clock, x->x_deltime);
@ -959,14 +978,8 @@ static void comport_open(t_comport *x, t_floatarg f)
static void comport_devicename(t_comport *x, t_symbol *s) static void comport_devicename(t_comport *x, t_symbol *s)
{ {
if(x->comport >= 0 && x->comport < COMPORT_MAX){ x->serial_device = s;
x->serial_device->s_name = s->s_name; x->comhandle = open_serial(9999,x);
if(x->verbose > 0)
post("[comport] %d: set devicename %s",x->comport,x->serial_device->s_name);
}
else if(x->verbose > 0)
post("[comport] %d: could not set devicename %s",x->comport,s->s_name);
} }
static void comport_print(t_comport *x, t_symbol *s, int argc, t_atom *argv) static void comport_print(t_comport *x, t_symbol *s, int argc, t_atom *argv)
@ -1051,7 +1064,7 @@ void comport_setup(void)
null_tv.tv_usec = 0; null_tv.tv_usec = 0;
#endif /* NOT _WIN32 */ #endif /* NOT _WIN32 */
post("comport - PD external for unix/windows\n" post("comport - PD external for unix/windows\n"
"LGPL 1998-2005, Winfried Ritsch and others (see LICENCE.txt)\n" "LGPL 1998-2006, Winfried Ritsch and others (see LICENCE.txt)\n"
"Institute for Electronic Music - Graz"); "Institute for Electronic Music - Graz");
} }