Update of /cvsroot/pure-data/externals/iem/comport/comport In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4498
Modified Files: comport.c Log Message: all status is output on second outlet
Index: comport.c =================================================================== RCS file: /cvsroot/pure-data/externals/iem/comport/comport/comport.c,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** comport.c 21 Jun 2006 23:42:00 -0000 1.16 --- comport.c 9 Jul 2006 19:50:37 -0000 1.17 *************** *** 10,13 **** --- 10,14 ---- MP 20060620 Add DTR and RTS control, add outputs to reflect CTS and DSR states. MP 20060621 Do all the above for Windows too. + MP 20060709 All status goes out the status outlet when an info message is received */
*************** *** 54,61 **** char serial_device_name[FILENAME_MAX]; short comport; /* holds the comport # */ - short last_comport; /* the last comport that was output on x_status_outlet */ - short last_dsr;/* last dsr input state */ - short last_cts;/* last cts input state */ t_float baud; /* holds the current baud rate */ short rxerrors; /* holds the rx line errors */ t_clock *x_clock; --- 55,64 ---- char serial_device_name[FILENAME_MAX]; short comport; /* holds the comport # */ t_float baud; /* holds the current baud rate */ + t_float data_bits; /* holds the current number of data bits */ + t_float parity_bit; /* holds the current parity */ + t_float stop_bits; /* holds the current number of stop bits */ + int xonxoff; /* nonzero if xonxoff handshaking is on */ + int ctsrts; /* nonzero if ctsrts handshaking is on */ short rxerrors; /* holds the rx line errors */ t_clock *x_clock; *************** *** 65,70 **** t_outlet *x_data_outlet; t_outlet *x_status_outlet; - t_outlet *x_dsr_outlet; - t_outlet *x_cts_outlet; } t_comport;
--- 68,71 ---- *************** *** 178,181 **** --- 179,236 ---- t_class *comport_class;
+ static long get_baud_ratebits(t_float *baud); + static void comport_pollintervall(t_comport *x, t_floatarg g); + static void comport_tick(t_comport *x); + static float set_baudrate(t_comport *x,t_float baud); + static float set_bits(t_comport *x, int nr); + static float set_parity(t_comport *x,int n); + static float set_stopflag(t_comport *x, int nr); + static int set_ctsrts(t_comport *x, int nr); + static int set_dtr(t_comport *x, int nr); + static int set_rts(t_comport *x, int nr); + static int set_xonxoff(t_comport *x, int nr); + static int set_serial(t_comport *x); + static int write_serial(t_comport *x, unsigned char serial_byte); + int comport_get_dsr(t_comport *x); + int comport_get_cts(t_comport *x); + #ifdef _WIN32 + static HANDLE open_serial(unsigned int com_num, t_comport *x); + static HANDLE close_serial(t_comport *x); + #else + static int open_serial(unsigned int com_num, t_comport *x); + static int close_serial(t_comport *x); + #endif + static void comport_pollintervall(t_comport *x, t_floatarg g); + static void comport_tick(t_comport *x); + static void comport_float(t_comport *x, t_float f); + static void *comport_new(t_floatarg com_num, t_floatarg fbaud); + static void comport_free(t_comport *x); + static void comport_baud(t_comport *x,t_floatarg f); + static void comport_bits(t_comport *x,t_floatarg f); + static void comport_parity(t_comport *x,t_floatarg f); + static void comport_stopbit(t_comport *x,t_floatarg f); + static void comport_rtscts(t_comport *x,t_floatarg f); + static void comport_dtr(t_comport *x,t_floatarg f); + static void comport_rts(t_comport *x,t_floatarg f); + static void comport_xonxoff(t_comport *x,t_floatarg f); + static void comport_close(t_comport *x); + static void comport_open(t_comport *x, t_floatarg f); + static void comport_devicename(t_comport *x, t_symbol *s); + static void comport_print(t_comport *x, t_symbol *s, int argc, t_atom *argv); + static void comport_output_status(t_comport *x, t_symbol *selector, t_float output_value); + static void comport_output_port_status(t_comport *x); + static void comport_output_dsr_status(t_comport *x); + static void comport_output_cts_status(t_comport *x); + static void comport_output_baud_rate(t_comport *x); + static void comport_output_parity_bit(t_comport *x); + static void comport_output_stop_bits(t_comport *x); + static void comport_output_data_bits(t_comport *x); + static void comport_output_rtscts(t_comport *x); + static void comport_output_xonxoff(t_comport *x); + static void comport_info(t_comport *x); + static void comport_verbose(t_comport *x, t_floatarg f); + static void comport_help(t_comport *x); + void comport_setup(void); + /* --------- sys independent serial setup helpers ---------------- */
*************** *** 387,395 **** /* char x->dcb.EofChar; end of input character */ /* char x->dcb.EvtChar; received event character */ ! ! set_bits(x, 8); /* CS8 */ ! set_stopflag(x, 0); /* ~CSTOPB */ ! set_ctsrts(x, 0); /* ~CRTSCTS;*/ ! set_xonxoff(x, 0); /* (IXON | IXOFF | IXANY) */ set_baudrate(x, *baud);
--- 442,449 ---- /* char x->dcb.EofChar; end of input character */ /* char x->dcb.EvtChar; received event character */ ! set_bits(x, x->data_bits); /* CS8 */ ! set_stopflag(x, x->stop_bits); /* ~CSTOPB */ ! set_ctsrts(x, x->ctsrts); /* ~CRTSCTS;*/ ! set_xonxoff(x, x->xonxoff); /* (IXON | IXOFF | IXANY) */ set_baudrate(x, *baud);
*************** *** 480,483 **** --- 534,576 ---- }
+ int comport_get_dsr(t_comport *x) + { + short dsr_state = 0; + if(x->comhandle != INVALID_HANDLE_VALUE) + { + DWORD modemStat; + /* get the DSR input state and if it's changed, output it */ + BOOL status = GetCommModemStatus(x->comhandle, &modemStat); + if (status) + { + dsr_state = ((modemStat&MS_DSR_ON)!=0);/* read the DSR input line */ + } + else + { + ;/* call GetLastError, only once so as not to hang pd */ + } + } + return dsr_state; + } + int comport_get_cts(t_comport *x) + { + short cts_state = 0; + if (x->comhandle != INVALID_HANDLE_VALUE) + { + DWORD modemStat; + /* get the DSR input state and if it's changed, output it */ + BOOL status = GetCommModemStatus(x->comhandle, &modemStat); + if (status) + { + cts_state = ((modemStat&MS_CTS_ON)!=0);/* read the CTS input line */ + } + else + { + ;/* call GetLastError, only once so as not to hang pd */ + } + } + return cts_state; + } + #else /* NT */ /* ----------------- POSIX - UNIX ------------------------------ */ *************** *** 689,697 **** /* defaults, see input */
! set_bits(x,8); /* CS8 */ ! set_stopflag(x,0); /* ~CSTOPB */ ! set_ctsrts(x,0); /* ~CRTSCTS;*/ ! set_xonxoff(x,0); /* (IXON | IXOFF | IXANY) */ ! set_baudrate(x,*baud);
if(tcsetattr(fd, TCSAFLUSH, new) != -1) --- 782,790 ---- /* defaults, see input */
! set_bits(x, x->data_bits); /* CS8 */ ! set_stopflag(x, x->stop_bits); /* ~CSTOPB */ ! set_ctsrts(x, x->ctsrts); /* ~CRTSCTS;*/ ! set_xonxoff(x, x->xonxoff); /* (IXON | IXOFF | IXANY) */ ! set_baudrate(x, *baud);
if(tcsetattr(fd, TCSAFLUSH, new) != -1) *************** *** 708,714 **** } x->comport = com_num; /* output at next comport_tick */ - x->last_comport = 777; /* a wrong number to force ouput at next comport_tick */ - x->last_dsr = -1; /* a wrong number so state will be output at next comport_tick */ - x->last_cts = -1; /* a wrong number so state will be output at next comport_tick */ return fd; } --- 801,804 ---- *************** *** 750,753 **** --- 840,871 ---- }
+ int comport_get_dsr(t_comport *x) + { + short dsr_state = 0; + + if (x->comhandle != INVALID_HANDLE_VALUE) + { + int status;/*dsr outlet*/ + /* get the DSR input state and if it's changed, output it */ + ioctl(x->comhandle, TIOCMGET, &status);/*dsr outlet*/ + dsr_state = ((status&TIOCM_LE)!=0);/* read the DSR input line */ + } + return dsr_state; + } + + int comport_get_cts(t_comport *x) + { + short cts_state = 0; + + if (x->comhandle != INVALID_HANDLE_VALUE) + { + int status;/*cts outlet*/ + /* get the CTS input state and if it's changed, output it */ + ioctl(x->comhandle, TIOCMGET, &status); + cts_state = ((status&TIOCM_CTS)!=0);/* read the CTS input line */ + } + return cts_state; + } + #endif /* else NT */
*************** *** 763,810 **** int err; HANDLE fd = x->comhandle; - short newdsr, newcts; - #ifdef _WIN32 - { - DWORD modemStat; - /* get the DSR input state and if it's changed, output it */ - BOOL status = GetCommModemStatus( fd, &modemStat); - if (status) - { - newdsr = ((modemStat&MS_DSR_ON)!=0);/* read the DSR input line */ - newcts = ((modemStat&MS_CTS_ON)!=0);/* read the CTS input line */ - } - else - { - ;/* call GetLastError, only once so as not to hang pd */ - } - } - #else - { - int status;/*dsr outlet*/ - - /* get the DSR input state and if it's changed, output it */ - ioctl(fd, TIOCMGET, &status);/*dsr outlet*/ - newdsr = ((status&TIOCM_LE)!=0);/* read the DSR input line */ - newcts = ((status&TIOCM_CTS)!=0);/* read the CTS input line */ - } - #endif - if (newdsr != x->last_dsr) - { - x->last_dsr = newdsr; - if(x->x_dsr_outlet!=NULL) outlet_float(x->x_dsr_outlet, (float)(x->last_dsr));/*dsr outlet*/ - } - if (newcts != x->last_cts) - { - x->last_cts = newcts; - if(x->x_cts_outlet!=NULL) outlet_float(x->x_cts_outlet, (float)(x->last_cts));/*cts outlet*/ - }
x->x_hit = 0;
- if(x->last_comport != x->comport) - { - x->last_comport = x->comport; - if (x->x_status_outlet != NULL) outlet_float(x->x_status_outlet, (float)x->comport); - } if(fd == INVALID_HANDLE_VALUE) return;
--- 881,887 ---- *************** *** 897,900 **** --- 974,982 ---- strncpy(test.serial_device_name, serial_device_name, strlen(serial_device_name)+1); test.baud = fbaud; + test.data_bits = 8; /* default 8 data bits */ + test.parity_bit = 0;/* default no parity bit */ + test.stop_bits = 0;/* default 1 stop bit */ + test.ctsrts = 0; /* default no hardware handshaking */ + test.xonxoff = 0; /* default no software handshaking */ fd = open_serial((unsigned int)com_num, &test);
*************** *** 903,911 ****
x->comport = test.comport;/* com_num */ - x->last_comport = 777; /* no known comport to force output at comport_tick */ 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 */
x->baud = test.baud; x->comhandle = fd; /* holds the comport handle */
--- 985,997 ----
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 */
x->baud = test.baud; + x->data_bits = test.data_bits; + x->parity_bit = test.parity_bit; + x->stop_bits = test.stop_bits; + x->ctsrts = test.ctsrts; + x->xonxoff = test.xonxoff; x->comhandle = fd; /* holds the comport handle */
*************** *** 931,936 **** x->x_data_outlet = outlet_new(&x->x_obj, &s_float); x->x_status_outlet = outlet_new(&x->x_obj, &s_float); - x->x_dsr_outlet = outlet_new(&x->x_obj, &s_float); - x->x_cts_outlet = outlet_new(&x->x_obj, &s_float);
x->x_hit = 0; --- 1017,1020 ---- *************** *** 987,993 **** --- 1071,1079 ---- 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; }
*************** *** 1003,1009 **** --- 1089,1097 ---- 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; }
*************** *** 1018,1024 **** --- 1106,1114 ---- 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; }
*************** *** 1033,1039 **** --- 1123,1131 ---- 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; }
*************** *** 1078,1084 **** --- 1170,1178 ---- 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; }
*************** *** 1090,1094 **** x->comport = -1; /* none */ if (x->x_status_outlet != NULL) outlet_float(x->x_status_outlet, (float)x->comport); - x->last_comport = x->comport; }
--- 1184,1187 ---- *************** *** 1132,1135 **** --- 1225,1295 ---- } } + + static void comport_output_status(t_comport *x, t_symbol *selector, t_float output_value) + { + t_atom *output_atom = getbytes(sizeof(t_atom)); + SETFLOAT(output_atom, output_value); + outlet_anything( x->x_status_outlet, selector, 1, output_atom); + freebytes(output_atom,sizeof(t_atom)); + } + + static void comport_output_port_status(t_comport *x) + { + comport_output_status(x, gensym("port"), (float)x->comport); + } + + static void comport_output_dsr_status(t_comport *x) + { + comport_output_status(x, gensym("dsr"), (float)comport_get_dsr(x)); + } + + static void comport_output_cts_status(t_comport *x) + { + comport_output_status(x, gensym("cts"), (float)comport_get_cts(x)); + } + + static void comport_output_baud_rate(t_comport *x) + { + comport_output_status(x, gensym("baud"), x->baud); + } + + static void comport_output_parity_bit(t_comport *x) + { + comport_output_status(x, gensym("parity"), x->parity_bit); + } + + static void comport_output_stop_bits(t_comport *x) + { + comport_output_status(x, gensym("stop"), x->stop_bits+1); + } + + static void comport_output_data_bits(t_comport *x) + { + comport_output_status(x, gensym("data"), x->data_bits); + } + + static void comport_output_rtscts(t_comport *x) + { + comport_output_status(x, gensym("rtscts"), x->ctsrts); + } + + static void comport_output_xonxoff(t_comport *x) + { + comport_output_status(x, gensym("xonxoff"), x->xonxoff); + } + + static void comport_info(t_comport *x) + { + comport_output_port_status(x); + comport_output_baud_rate(x); + comport_output_dsr_status(x); + comport_output_cts_status(x); + comport_output_parity_bit(x); + comport_output_stop_bits(x); + comport_output_data_bits(x); + comport_output_rtscts(x); + comport_output_xonxoff(x); + } + /* ---------------- HELPER ------------------------- */ static void comport_verbose(t_comport *x, t_floatarg f) *************** *** 1148,1166 ****
post(" Methods:"); ! post(" baud <baud> ... set baudrate to nearest possible baud\n" ! " bits <bits> ... set number of bits (7 or 8)\n" ! " stopbit <0|1> ... set off|on stopbit\n" ! " rtscts <0|1> ... set rts/cts off|on\n" ! " parity <0|1> ... set parity off|on\n" ! " xonxoff <0|1> ... set xon/xoff off|on\n" ! " dtr <0|1> ... set dtr off|on\n" ! " rts <0|1> ... set rts off|on\n" ! " close ... close device\n" ! " open <num> ... open device number num\n" ! " devicename <d> ... set device name to s (eg. /dev/ttyS8)\n" ! " print <list> ... print list of atoms on serial\n" " pollintervall <t> ... set poll interval to t ticks\n" ! " verbose <level> ... for debug set verbosity to level\n" ! " help ... post this help"); }
--- 1308,1327 ----
post(" Methods:"); ! post(" baud <baud> ... set baudrate to nearest possible baud\n" ! " bits <bits> ... set number of bits (7 or 8)\n" ! " stopbit <0|1> ... set off|on stopbit\n" ! " rtscts <0|1> ... set rts/cts off|on\n" ! " parity <0|1> ... set parity off|on\n" ! " xonxoff <0|1> ... set xon/xoff off|on\n" ! " dtr <0|1> ... set dtr off|on\n" ! " rts <0|1> ... set rts off|on\n" ! " close ... close device\n" ! " open <num> ... open device number num\n" ! " devicename <d> ... set device name to s (eg. /dev/ttyS8)\n" ! " print <list> ... print list of atoms on serial\n" " pollintervall <t> ... set poll interval to t ticks\n" ! " verbose <level> ... for debug set verbosity to level\n" ! " info ... output info on status outlet\n" ! " help ... post this help"); }
*************** *** 1193,1196 **** --- 1354,1358 ---- class_addmethod(comport_class, (t_method)comport_verbose, gensym("verbose"), A_FLOAT, 0); class_addmethod(comport_class, (t_method)comport_help, gensym("help"), 0); + class_addmethod(comport_class, (t_method)comport_info, gensym("info"), 0);
#ifndef _WIN32