added HUPCL support in an attempt to get things working better with the Arduino Diecimila

svn path=/trunk/externals/iem/comport/; revision=8794
This commit is contained in:
Hans-Christoph Steiner 2007-10-10 00:20:56 +00:00
parent f423522228
commit dd2de83ed4
2 changed files with 83 additions and 21 deletions

View file

@ -1,4 +1,4 @@
#N canvas 504 0 746 619 10; #N canvas 504 22 750 623 10;
#X obj 18 436 comport 1 9600; #X obj 18 436 comport 1 9600;
#X msg 13 189 66; #X msg 13 189 66;
#X msg 7 147 64; #X msg 7 147 64;
@ -10,16 +10,16 @@
#X msg 25 323 71; #X msg 25 323 71;
#X text 49 283 run; #X text 49 283 run;
#X text 50 323 sleep; #X text 50 323 sleep;
#X msg 314 61 bits 8; #X msg 314 51 bits 8;
#X msg 314 87 stopbit 0; #X msg 314 77 stopbit 0;
#X msg 314 113 parity 0; #X msg 314 103 parity 0;
#X text 381 112 parity 1=even \, -1=odd \, 0=off; #X text 381 102 parity 1=even \, -1=odd \, 0=off;
#X text 385 86 extra stopbit 1=on \, 0=off; #X text 385 76 extra stopbit 1=on \, 0=off;
#X text 363 60 databits 5 \, 6 \, 7 \, 8; #X text 363 50 databits 5 \, 6 \, 7 \, 8;
#X obj 56 414 r comctl; #X obj 56 414 r comctl;
#X obj 245 438 s comctl; #X obj 245 438 s comctl;
#X text 394 138 use handshake xon/off 1=on 0=off; #X text 394 128 use handshake xon/off 1=on 0=off;
#X text 386 164 cts/rts hardwarehandshake 1=on 0=off; #X text 386 154 cts/rts hardwarehandshake 1=on 0=off;
#X msg 318 214 pollintervall 1; #X msg 318 214 pollintervall 1;
#X text 428 213 (default is 1 tick 1ms); #X text 428 213 (default is 1 tick 1ms);
#X msg 317 246 close; #X msg 317 246 close;
@ -142,22 +142,26 @@ maybe if suid is root damage the system.;
#X connect 32 1 31 1; #X connect 32 1 31 1;
#X connect 33 0 24 0; #X connect 33 0 24 0;
#X restore 622 72 pd bauds; #X restore 622 72 pd bauds;
#X msg 314 139 xonxoff \$1; #X msg 314 129 xonxoff \$1;
#X obj 286 140 tgl 15 0 empty empty empty 0 -6 0 8 -241291 -250685 #X obj 286 130 tgl 15 0 empty empty empty 0 -6 0 8 -241291 -250685
-1 0 1; -1 0 1;
#X msg 314 165 rtscts \$1; #X msg 314 155 rtscts \$1;
#X obj 286 167 tgl 15 0 empty empty empty 0 -6 0 8 -241291 -250685 #X obj 286 157 tgl 15 0 empty empty empty 0 -6 0 8 -241291 -250685
-1 0 1; -1 0 1;
#X text 117 11 comport: a serial port interface; #X text 117 11 comport: a serial port interface;
#X msg 102 118 13; #X msg 102 118 13;
#X obj 421 501 tgl 15 0 empty empty open -6 20 1 11 -24198 -258699 #X obj 421 501 tgl 15 0 empty empty open -6 20 1 11 -24198 -258699
-1 1 1; -1 0 1;
#X msg 317 320 ports; #X msg 317 320 ports;
#X obj 111 462 route baud dsr cts parity stop data rtscts xonxoff port #X obj 111 462 route baud dsr cts parity stop data rtscts xonxoff port
open ports; open ports;
#X obj 456 530 print port_list; #X obj 456 530 print port_list;
#X text 363 319 list available ports on right outlet; #X text 363 319 list available ports on right outlet;
#X text 110 360 print usage info; #X text 110 360 print usage info;
#X obj 289 182 tgl 15 0 empty empty empty 0 -6 0 8 -241291 -250685
-1 0 1;
#X msg 318 179 hupcl \$1;
#X text 386 179 hang-up-on-close 1=on 0=off;
#X connect 0 1 78 0; #X connect 0 1 78 0;
#X connect 1 0 0 0; #X connect 1 0 0 0;
#X connect 2 0 0 0; #X connect 2 0 0 0;
@ -207,3 +211,5 @@ open ports;
#X connect 78 9 76 0; #X connect 78 9 76 0;
#X connect 78 10 79 0; #X connect 78 10 79 0;
#X connect 78 11 49 0; #X connect 78 11 49 0;
#X connect 82 0 83 0;
#X connect 83 0 18 0;

View file

@ -66,6 +66,7 @@ typedef struct comport
t_float stop_bits; /* holds the current number of stop bits */ t_float stop_bits; /* holds the current number of stop bits */
int xonxoff; /* nonzero if xonxoff handshaking is on */ int xonxoff; /* nonzero if xonxoff handshaking is on */
int ctsrts; /* nonzero if ctsrts handshaking is on */ int ctsrts; /* nonzero if ctsrts handshaking is on */
int hupcl; /* nonzero if hang-up on close is on */
short rxerrors; /* holds the rx line errors */ short rxerrors; /* holds the rx line errors */
t_clock *x_clock; t_clock *x_clock;
int x_hit; int x_hit;
@ -228,6 +229,7 @@ static int set_ctsrts(t_comport *x, int nr);
static int set_dtr(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_rts(t_comport *x, int nr);
static int set_xonxoff(t_comport *x, int nr); static int set_xonxoff(t_comport *x, int nr);
static int set_hupcl(t_comport *x, int nr);
static int set_serial(t_comport *x); static int set_serial(t_comport *x);
static int write_serial(t_comport *x, unsigned char serial_byte); static int write_serial(t_comport *x, unsigned char serial_byte);
static int comport_get_dsr(t_comport *x); static int comport_get_dsr(t_comport *x);
@ -253,6 +255,7 @@ static void comport_rtscts(t_comport *x,t_floatarg f);
static void comport_dtr(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_rts(t_comport *x,t_floatarg f);
static void comport_xonxoff(t_comport *x,t_floatarg f); static void comport_xonxoff(t_comport *x,t_floatarg f);
static void comport_hupcl(t_comport *x,t_floatarg f);
static void comport_close(t_comport *x); static void comport_close(t_comport *x);
static void comport_open(t_comport *x, t_floatarg f); 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);
@ -267,6 +270,7 @@ static void comport_output_stop_bits(t_comport *x);
static void comport_output_data_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_rtscts(t_comport *x);
static void comport_output_xonxoff(t_comport *x); static void comport_output_xonxoff(t_comport *x);
static void comport_output_hupcl(t_comport *x);
static void comport_enum(t_comport *x); static void comport_enum(t_comport *x);
static void comport_info(t_comport *x); static void comport_info(t_comport *x);
static void comport_devices(t_comport *x); static void comport_devices(t_comport *x);
@ -464,7 +468,7 @@ static HANDLE open_serial(unsigned int com_num, t_comport *x)
errStr = " "; errStr = " ";
break; break;
} }
post("** ERROR ** could not open device %s:\n failure(%d) %s\n", pd_error(x, "[comport]: could not open device %s:\n failure(%d) %s\n",
&x->serial_device->s_name[4], dw, errStr); &x->serial_device->s_name[4], dw, errStr);
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
@ -473,7 +477,7 @@ static HANDLE open_serial(unsigned int com_num, t_comport *x)
if (!GetCommState(fd, &(x->dcb_old))) if (!GetCommState(fd, &(x->dcb_old)))
{ {
post("** ERROR ** could not get old dcb of device %s\n", pd_error(x, "[comport]: could not get old dcb of device %s\n",
&x->serial_device->s_name[4]); &x->serial_device->s_name[4]);
CloseHandle(fd); CloseHandle(fd);
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
@ -483,7 +487,7 @@ static HANDLE open_serial(unsigned int com_num, t_comport *x)
if (!GetCommState(fd, &(x->dcb))) if (!GetCommState(fd, &(x->dcb)))
{ {
post("** ERROR ** could not get new dcb of device %s\n", pd_error(x, "[comport]: could not get new dcb of device %s\n",
&x->serial_device->s_name[4]); &x->serial_device->s_name[4]);
CloseHandle(fd); CloseHandle(fd);
@ -529,7 +533,7 @@ static HANDLE open_serial(unsigned int com_num, t_comport *x)
} }
else else
{ {
error("[comport] ** ERROR ** could not set params to control dcb of device %s\n", pd_error(x, "[comport] could not set params to control dcb of device %s\n",
&x->serial_device->s_name[4]); &x->serial_device->s_name[4]);
CloseHandle(fd); CloseHandle(fd);
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
@ -662,6 +666,9 @@ static long get_baud_ratebits(t_float *baud)
while(i < BAUDRATETABLE_LEN && baudratetable[i] > *baud) i++; 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 */ /* nearest Baudrate finding */
if(i==BAUDRATETABLE_LEN || baudspeedbittable[i] < 0) if(i==BAUDRATETABLE_LEN || baudspeedbittable[i] < 0)
{ {
@ -669,6 +676,7 @@ static long get_baud_ratebits(t_float *baud)
i = 8; i = 8;
} }
*baud = baudratetable[i]; *baud = baudratetable[i];
post("get_baud_ratebits: %f", *baud);
return baudspeedbittable[i]; return baudspeedbittable[i];
} }
@ -677,9 +685,11 @@ static float set_baudrate(t_comport *x, t_float baud)
{ {
struct termios *tio = &(x->com_termio); struct termios *tio = &(x->com_termio);
speed_t baudbits = get_baud_ratebits(&baud); speed_t baudbits = get_baud_ratebits(&baud);
post("set_baudrate baudbits: %d", baudbits);
cfsetispeed(tio, baudbits); if( cfsetispeed(tio, baudbits) != 0 )
cfsetospeed(tio, baudbits); 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; return baud;
} }
@ -794,6 +804,30 @@ static int set_xonxoff(t_comport *x, int nr)
return 0; return 0;
} }
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) static int open_serial(unsigned int com_num, t_comport *x)
{ {
int fd; int fd;
@ -1021,6 +1055,7 @@ static void comport_tick(t_comport *x)
{ {
unsigned char serial_byte; unsigned char serial_byte;
fd_set com_rfds; fd_set com_rfds;
int count = 0;
FD_ZERO(&com_rfds); FD_ZERO(&com_rfds);
FD_SET(fd,&com_rfds); FD_SET(fd,&com_rfds);
@ -1030,7 +1065,10 @@ 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_data_outlet, (t_float) serial_byte); outlet_float(x->x_data_outlet, (t_float) serial_byte);
++count;
} }
// if( count > 0)
// post("--- %d", count);
} }
#endif #endif
@ -1091,6 +1129,7 @@ that allows COM port numbers to be specified.
test.stop_bits = 0;/* default 1 stop bit */ test.stop_bits = 0;/* default 1 stop bit */
test.ctsrts = 0; /* default no hardware handshaking */ test.ctsrts = 0; /* default no hardware handshaking */
test.xonxoff = 0; /* default no software handshaking */ test.xonxoff = 0; /* default no software handshaking */
test.hupcl = 1; /* default hangup on close */
fd = open_serial((unsigned int)com_num, &test); fd = open_serial((unsigned int)com_num, &test);
/* Now nothing really bad could happen so we create the class */ /* Now nothing really bad could happen so we create the class */
@ -1106,6 +1145,7 @@ that allows COM port numbers to be specified.
x->stop_bits = test.stop_bits; x->stop_bits = test.stop_bits;
x->ctsrts = test.ctsrts; x->ctsrts = test.ctsrts;
x->xonxoff = test.xonxoff; x->xonxoff = test.xonxoff;
x->hupcl = test.hupcl;
x->comhandle = fd; /* holds the comport handle */ x->comhandle = fd; /* holds the comport handle */
if(fd == INVALID_HANDLE_VALUE ) if(fd == INVALID_HANDLE_VALUE )
@ -1361,6 +1401,14 @@ static void comport_xonxoff(t_comport *x,t_floatarg f)
x->xonxoff = f; x->xonxoff = f;
} }
static void comport_hupcl(t_comport *x,t_floatarg f)
{
//#ifdef _WIN32
//#else
set_hupcl(x,f);
//#endif
}
static void comport_close(t_comport *x) static void comport_close(t_comport *x)
{ {
clock_unset(x->x_clock); clock_unset(x->x_clock);
@ -1607,6 +1655,11 @@ static void comport_output_xonxoff(t_comport *x)
comport_output_status(x, gensym("xonxoff"), x->xonxoff); comport_output_status(x, gensym("xonxoff"), x->xonxoff);
} }
static void comport_output_hupcl(t_comport *x)
{
comport_output_status(x, gensym("hupcl"), x->hupcl);
}
static void comport_output_open_status(t_comport *x) static void comport_output_open_status(t_comport *x)
{ {
if(x->comhandle == INVALID_HANDLE_VALUE) if(x->comhandle == INVALID_HANDLE_VALUE)
@ -1632,6 +1685,7 @@ static void comport_info(t_comport *x)
comport_output_data_bits(x); comport_output_data_bits(x);
comport_output_rtscts(x); comport_output_rtscts(x);
comport_output_xonxoff(x); comport_output_xonxoff(x);
comport_output_hupcl(x);
} }
/* ---------------- HELPER ------------------------- */ /* ---------------- HELPER ------------------------- */
@ -1662,6 +1716,7 @@ static void comport_help(t_comport *x)
" xonxoff <0|1> ... set xon/xoff off|on\n" " xonxoff <0|1> ... set xon/xoff off|on\n"
" dtr <0|1> ... set dtr off|on\n" " dtr <0|1> ... set dtr off|on\n"
" rts <0|1> ... set rts off|on\n" " rts <0|1> ... set rts off|on\n"
" hupcl <0|1> ... set hang-up on close off|on\n"
" close ... close device\n" " close ... close device\n"
" open <num> ... open device number num\n" " open <num> ... open device number num\n"
" devicename <d> ... set device name to d (eg. /dev/ttyS8)\n" " devicename <d> ... set device name to d (eg. /dev/ttyS8)\n"
@ -1695,6 +1750,7 @@ void comport_setup(void)
class_addmethod(comport_class, (t_method)comport_rts, gensym("rts"), A_FLOAT, 0); class_addmethod(comport_class, (t_method)comport_rts, gensym("rts"), A_FLOAT, 0);
class_addmethod(comport_class, (t_method)comport_parity, gensym("parity"), A_FLOAT, 0); class_addmethod(comport_class, (t_method)comport_parity, gensym("parity"), A_FLOAT, 0);
class_addmethod(comport_class, (t_method)comport_xonxoff, gensym("xonxoff"), A_FLOAT, 0); class_addmethod(comport_class, (t_method)comport_xonxoff, gensym("xonxoff"), A_FLOAT, 0);
class_addmethod(comport_class, (t_method)comport_hupcl, gensym("hupcl"), A_FLOAT, 0);
class_addmethod(comport_class, (t_method)comport_close, gensym("close"), 0); class_addmethod(comport_class, (t_method)comport_close, gensym("close"), 0);
class_addmethod(comport_class, (t_method)comport_open, gensym("open"), A_FLOAT, 0); class_addmethod(comport_class, (t_method)comport_open, gensym("open"), A_FLOAT, 0);
class_addmethod(comport_class, (t_method)comport_devicename, gensym("devicename"), A_SYMBOL, 0); class_addmethod(comport_class, (t_method)comport_devicename, gensym("devicename"), A_SYMBOL, 0);