added serial port device name globbing for UNIX; added Piotr Majdak's Win32 improvements, they compile, but aren't tested yet; still need to update [devicename( message handling

svn path=/trunk/externals/iem/comport/; revision=5102
This commit is contained in:
Hans-Christoph Steiner 2006-05-21 13:35:20 +00:00
parent c022f3ec08
commit df122aae6a
2 changed files with 236 additions and 214 deletions

View file

@ -1,8 +1,7 @@
#N canvas 199 244 693 475 10; #N canvas 199 244 697 479 10;
#X obj 97 231 comport 1 9600; #X obj 97 231 comport 1 9600;
#X msg 13 213 66; #X msg 13 213 66;
#X obj 88 361 print; #X obj 88 361 print;
#X floatatom 195 190 4 0 0 0 - - -;
#X msg 12 171 64; #X msg 12 171 64;
#X text 12 191 point; #X text 12 191 point;
#X text 11 150 stream; #X text 11 150 stream;
@ -16,66 +15,62 @@
#X obj 90 325 spigot; #X obj 90 325 spigot;
#X msg 109 298 1; #X msg 109 298 1;
#X msg 143 299 0; #X msg 143 299 0;
#X msg 314 48 bits 8; #X msg 313 60 bits 8;
#X msg 313 72 stopbit 0; #X msg 312 84 stopbit 0;
#X msg 312 100 parity 0; #X msg 311 112 parity 0;
#X msg 313 129 xonxoff 1; #X msg 312 141 xonxoff 1;
#X msg 313 155 rtscts 0; #X msg 312 167 rtscts 0;
#X text 392 103 parity 1=even \, -1=odd \, 0=off; #X text 391 115 parity 1=even \, -1=odd \, 0=off;
#X text 392 74 extra stopbit 1=on \, 0=off; #X text 391 86 extra stopbit 1=on \, 0=off;
#X text 394 49 databits 5 \, 6 \, 7 \, 8; #X text 393 61 databits 5 \, 6 \, 7 \, 8;
#X text 393 20 use exact or higher baudrate; #X text 392 32 use exact or higher baudrate;
#X obj 94 200 r comctl; #X obj 115 196 r comctl;
#X obj 271 294 s comctl; #X obj 219 389 s comctl;
#X text 394 128 use handshake xon/off 1=on 0=off; #X text 393 140 use handshake xon/off 1=on 0=off;
#X text 392 154 cts/rts hardwarehandshake 1=on 0=off; #X text 391 166 cts/rts hardwarehandshake 1=on 0=off;
#X msg 318 201 pollintervall 1; #X msg 317 213 pollintervall 1;
#X text 430 191 set pollintervall for read in ms; #X text 429 203 set pollintervall for read in ms;
#X text 430 205 (default is 1 tick 1ms); #X text 429 217 (default is 1 tick 1ms);
#X msg 317 233 close; #X msg 316 245 close;
#X msg 317 259 open 1; #X msg 316 271 open 1;
#X msg 314 23 baud 10000; #X msg 313 35 baud 10000;
#X text 383 230 Close Serial port; #X text 382 242 Close Serial port;
#X text 381 263 Open seriel board Nr (0=COM1 \, 1=COM2 \, ...); #X msg 310 311 devicename /dev/ttyS1;
#X msg 263 354 devicename /dev/ttyS1; #X text 256 411 Danger !!! you can open every file in your system and
#X text 257 375 Danger !!! you can open every file in your system and
maybe if suid is root damage the system.; maybe if suid is root damage the system.;
#X text 258 336 set devicename for actuell port \, then close and open #X msg 308 12 baud 300;
again;
#X obj 195 165 spigot;
#X msg 214 138 1;
#X msg 247 143 0;
#X obj 190 112 key;
#X text 260 324 never should be needed except for sys admins (only
unix);
#X msg 309 0 baud 300;
#X text 3 -10 (C) 1998-2005 IEM Winfried Ritsch GPL (see LICENSE.txt) #X text 3 -10 (C) 1998-2005 IEM Winfried Ritsch GPL (see LICENSE.txt)
; ;
#X text 14 388; #X text 14 388 comment;
#X text 1 41 please improve...; #X obj 84 135 clip 0 255;
#X obj 87 89 hsl 128 15 0 255 0 0 empty empty empty -2 -6 0 8 -262144
-1 -1 11300 1;
#X floatatom 111 116 5 0 0 0 - - -;
#X text 14 69 you can send bytes directly (0-255):;
#X text 379 271 Open serial port by number;
#X text 466 307 open the serial port by name;
#X connect 0 0 12 0;
#X connect 0 0 13 0; #X connect 0 0 13 0;
#X connect 0 0 14 0;
#X connect 1 0 0 0; #X connect 1 0 0 0;
#X connect 3 0 0 0; #X connect 3 0 0 0;
#X connect 4 0 0 0; #X connect 6 0 0 0;
#X connect 7 0 0 0; #X connect 8 0 0 0;
#X connect 9 0 0 0; #X connect 9 0 0 0;
#X connect 10 0 0 0; #X connect 13 0 2 0;
#X connect 14 0 2 0; #X connect 14 0 13 1;
#X connect 15 0 14 1; #X connect 15 0 13 1;
#X connect 16 0 14 1; #X connect 16 0 26 0;
#X connect 17 0 27 0; #X connect 17 0 26 0;
#X connect 18 0 27 0; #X connect 18 0 26 0;
#X connect 19 0 27 0; #X connect 19 0 26 0;
#X connect 20 0 27 0; #X connect 20 0 26 0;
#X connect 21 0 27 0; #X connect 25 0 0 0;
#X connect 26 0 0 0; #X connect 29 0 26 0;
#X connect 30 0 27 0; #X connect 32 0 26 0;
#X connect 33 0 27 0; #X connect 33 0 26 0;
#X connect 34 0 27 0; #X connect 34 0 26 0;
#X connect 35 0 27 0; #X connect 36 0 26 0;
#X connect 41 0 3 0; #X connect 38 0 26 0;
#X connect 42 0 41 1; #X connect 41 0 0 0;
#X connect 43 0 41 1; #X connect 42 0 43 0;
#X connect 44 0 41 0; #X connect 42 0 41 0;
#X connect 46 0 27 0;

View file

@ -22,12 +22,14 @@
#include <fcntl.h> #include <fcntl.h>
#include <termios.h> /* for TERMIO ioctl calls */ #include <termios.h> /* for TERMIO ioctl calls */
#include <unistd.h> #include <unistd.h>
#include <glob.h>
#define HANDLE int #define HANDLE int
#define INVALID_HANDLE_VALUE -1 #define INVALID_HANDLE_VALUE -1
#endif /* _WIN32 */ #endif /* _WIN32 */
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <stdio.h>
typedef struct comport typedef struct comport
@ -39,16 +41,18 @@ typedef struct comport
HANDLE comhandle; /* holds the comport handle */ HANDLE comhandle; /* holds the comport handle */
#ifdef _WIN32 #ifdef _WIN32
DCB dcb; /* holds the comm pars */ DCB dcb; /* holds the comm pars */
DCB dcb_old; /* holds the comm pars */ DCB dcb_old; /* holds the comm pars */
COMMTIMEOUTS old_timeouts; COMMTIMEOUTS old_timeouts;
#else #else
struct termios oldcom_termio; /* save the old com config */ struct termios oldcom_termio; /* save the old com config */
struct termios com_termio; /* for the new com config */ struct termios com_termio; /* for the new com config */
#endif #endif
t_symbol *serial_device;
char serial_device_name[FILENAME_MAX];
short comport; /* holds the comport # */ short comport; /* holds the comport # */
float baud; /* holds the current baud rate */ t_float baud; /* holds the current baud rate */
short rxerrors; /* holds the rx line errors */ short rxerrors; /* holds the rx line errors */
@ -78,23 +82,9 @@ typedef struct comport
#define RXBUFOVERRUN -4 #define RXBUFOVERRUN -4
#define TXBUFOVERRUN -5 #define TXBUFOVERRUN -5
#define COMPORT_MAX 99
#ifdef _WIN32 #ifdef _WIN32
#define COMPORT_MAX 40
static char *sys_com_port[COMPORT_MAX] =
{
"COM1", "COM2", "COM3", "COM4",
"COM5", "COM6", "COM7", "COM8",
"COM9", "COM10", "COM11", "COM12",
"COM13", "COM14", "COM15", "COM16",
"COM17", "COM18", "COM19", "COM20",
"COM21", "COM22", "COM23", "COM24",
"COM25", "COM26", "COM27", "COM28",
"COM29", "COM30", "COM31", "COM32",
"COM33", "COM34", "COM35", "COM36",
"COM37", "COM38", "COM39", "COM40"
};
static static
long baudspeedbittable[] = long baudspeedbittable[] =
{ {
@ -118,11 +108,6 @@ long baudspeedbittable[] =
#else /* _WIN32 */ #else /* _WIN32 */
#ifdef IRIX #ifdef IRIX
#define COMPORT_MAX 2
static char *sys_com_port[COMPORT_MAX] =
{
"/dev/ttyd1", "/dev/ttyd2"
};
#define OPENPARAMS (O_RDWR|O_NDELAY|O_NOCTTY) #define OPENPARAMS (O_RDWR|O_NDELAY|O_NOCTTY)
#define TIONREAD FIONREAD /* re map the IOCTL function */ #define TIONREAD FIONREAD /* re map the IOCTL function */
#define BAUDRATE_256000 -1 #define BAUDRATE_256000 -1
@ -133,14 +118,6 @@ static char *sys_com_port[COMPORT_MAX] =
#define BAUDRATE_38400 B38400 #define BAUDRATE_38400 B38400
#define BAUDRATE_14400 B19200 /* 14400 gibts nicht */ #define BAUDRATE_14400 B19200 /* 14400 gibts nicht */
#else /* IRIX */ #else /* IRIX */
#define COMPORT_MAX 16
static char *sys_com_port[COMPORT_MAX] =
{
"/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3",
"/dev/ttyS4", "/dev/ttyS5", "/dev/ttyS6", "/dev/ttyS7",
"/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2", "/dev/ttyUSB3",
"/dev/ttyUSB4", "/dev/ttyUSB5", "/dev/ttyUSB6", "/dev/ttyUSB7"
};
#define OPENPARAMS (O_RDWR|O_NDELAY|O_NOCTTY) #define OPENPARAMS (O_RDWR|O_NDELAY|O_NOCTTY)
#define BAUDRATE_256000 -1 #define BAUDRATE_256000 -1
#define BAUDRATE_128000 -1 #define BAUDRATE_128000 -1
@ -149,7 +126,6 @@ static char *sys_com_port[COMPORT_MAX] =
#define BAUDRATE_56000 B57600 /* 56000 gibts nicht */ #define BAUDRATE_56000 B57600 /* 56000 gibts nicht */
#define BAUDRATE_38400 B38400 #define BAUDRATE_38400 B38400
#define BAUDRATE_14400 B19200 /* 14400 gibts nicht */ #define BAUDRATE_14400 B19200 /* 14400 gibts nicht */
#endif /* else IRIX */ #endif /* else IRIX */
static static
@ -322,39 +298,41 @@ static int set_serial(t_comport *x)
return 0; return 0;
} }
static HANDLE open_serial(int com_nr, t_comport *x) static HANDLE open_serial(unsigned int com_num, t_comport *x)
{ {
HANDLE fd; HANDLE fd;
COMMTIMEOUTS timeouts; COMMTIMEOUTS timeouts;
char buffer[MAX_PATH];
float *baud = &(x->baud); float *baud = &(x->baud);
if(com_nr < 0 || com_nr >= COMPORT_MAX) { if(com_num < 1 || com_num >= COMPORT_MAX) {
post("comport open %d, baud %d not valid (args: [portnum] [baud])",com_nr,*baud); post("comport open %d, baud %d not valid (args: [portnum] [baud])",com_num,*baud);
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
fd = CreateFile( sys_com_port[com_nr], 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, GENERIC_READ | GENERIC_WRITE,
0, 0,
0, 0,
OPEN_EXISTING, OPEN_EXISTING,
0, FILE_FLAG_OVERLAPPED,
0); 0);
if(fd == INVALID_HANDLE_VALUE) if(fd == INVALID_HANDLE_VALUE)
{ {
post("** ERROR ** could not open device %s:\n failure(%d): %s\n", post("** ERROR ** could not open device %s:\n failure(%d): %s\n",
sys_com_port[com_nr],errno,strerror(errno)); x->serial_device->s_name,errno,strerror(errno));
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
/* Save the Current Port Configuration */ /* Save the Current Port Configuration */
if (!GetCommState(fd, &(x->dcb_old))){ if (!GetCommState(fd, &(x->dcb_old))){
post("** ERROR ** could not get old dcb of device %s\n", post("** ERROR ** could not get old dcb of device %s\n",
sys_com_port[com_nr]); x->serial_device->s_name);
CloseHandle(fd); CloseHandle(fd);
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
@ -363,8 +341,8 @@ static HANDLE open_serial(int com_nr, t_comport *x)
memset(&(x->dcb), sizeof(DCB), 0); memset(&(x->dcb), sizeof(DCB), 0);
if (!GetCommState(fd, &(x->dcb))){ if (!GetCommState(fd, &(x->dcb))){
post("** ERROR ** could not get new dcb of device %s\n", post("** ERROR ** could not get new dcb of device %s\n",
sys_com_port[com_nr]); x->serial_device->s_name);
CloseHandle(fd); CloseHandle(fd);
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
@ -392,19 +370,20 @@ static HANDLE open_serial(int com_nr, t_comport *x)
set_bits(x,8); /* CS8 */ set_bits(x,8); /* CS8 */
set_stopflag(x,0); /* ~CSTOPB */ set_stopflag(x,0); /* ~CSTOPB */
set_ctsrts(x,0); /* ~CRTSCTS;*/ set_ctsrts(x,0); /* ~CRTSCTS;*/
set_xonxoff(x,1); /* (IXON | IXOFF | IXANY) */ set_xonxoff(x,0); /* (IXON | IXOFF | IXANY) */
set_baudrate(x,*baud); set_baudrate(x,*baud);
x->comhandle = fd; x->comhandle = fd;
if(set_serial(x)) if(set_serial(x))
{ {
post("Opened serial line device %s\n",sys_com_port[com_nr]); post("[comport] opened serial line device %d (%s)\n",
} com_num,x->serial_device->s_name);
}
else else
{ {
post("** ERROR ** could not set params to control dcb of device %s\n", error("[comport] ** ERROR ** could not set params to control dcb of device %s\n",
sys_com_port[com_nr]); x->serial_device->s_name);
CloseHandle(fd); CloseHandle(fd);
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
@ -412,7 +391,7 @@ static HANDLE open_serial(int com_nr, t_comport *x)
if (!GetCommTimeouts(fd, &(x->old_timeouts))){ if (!GetCommTimeouts(fd, &(x->old_timeouts))){
post("Couldnt get old timeouts for serial device"); post("[comport] Couldn't get old timeouts for serial device");
}; };
/* setting new timeouts for read to immidiatly return */ /* setting new timeouts for read to immidiatly return */
@ -438,7 +417,7 @@ 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("** ERROR ** could not reset params to DCB of device %s\n",
sys_com_port[x->comport]); x->serial_device->s_name);
} }
if (!SetCommTimeouts(x->comhandle, &(x->old_timeouts))){ if (!SetCommTimeouts(x->comhandle, &(x->old_timeouts))){
@ -451,19 +430,33 @@ static HANDLE close_serial(t_comport *x)
} }
static int write_serial(t_comport *x, unsigned char chr) static int write_serial(t_comport *x, unsigned char serial_byte)
{ {
OVERLAPPED osWrite = {0}; OVERLAPPED osWrite = {0};
DWORD dwWritten; DWORD dwWritten;
DWORD dwRes; DWORD dwToWrite = 1;
DWORD dwErr;
/* post("open send %d",chr); */ char cErr[100];
if (!WriteFile(x->comhandle, &chr, 1, &dwWritten, &osWrite)) { osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (GetLastError() != ERROR_IO_PENDING) if (osWrite.hEvent == NULL)
post("WriteFile failed, but isn't delayed on serialdevice"); {
return 0; post("Couldn't create event. Transmission aborted.");
} return 0;
}
if (!WriteFile(x->comhandle, &serial_byte, dwToWrite, &dwWritten, &osWrite))
{
dwErr = GetLastError();
if (dwErr != ERROR_IO_PENDING)
{
sprintf(cErr, "WriteFile error: %d", (int)dwErr);
post(cErr);
return 0;
}
}
CloseHandle(osWrite.hEvent);
return 1; return 1;
} }
@ -556,22 +549,49 @@ static int set_xonxoff(t_comport *x, int nr)
return 0; return 0;
} }
static int open_serial(int com_nr, t_comport *x) static int open_serial(unsigned int com_num, t_comport *x)
{ {
HANDLE fd; HANDLE fd;
struct termios *old = &(x->oldcom_termio); struct termios *old = &(x->oldcom_termio);
struct termios *new = &(x->com_termio); struct termios *new = &(x->com_termio);
float *baud = &(x->baud); float *baud = &(x->baud);
glob_t glob_buffer;
if(com_nr < 0 || com_nr >= COMPORT_MAX) { if(com_num >= COMPORT_MAX) {
post("comport open %d, baud %d not valid (args: [portnum] [baud])"); post("[comport] ** WARNING ** port %d not valid, must be between 0 and %d",
return INVALID_HANDLE_VALUE; com_num, COMPORT_MAX - 1);
return INVALID_HANDLE_VALUE;
} }
// post("[comport] globbing %s",x->serial_device_name);
/* get the device path based on the com# 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;
case GLOB_ABORTED:
error("[comport] aborted \"%s\"",x->serial_device_name);
break;
case GLOB_NOMATCH:
error("[comport] no serial devices found for \"%s\"",x->serial_device_name);
break;
}
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(sys_com_port[com_nr], OPENPARAMS)) == INVALID_HANDLE_VALUE) if((fd = open(x->serial_device->s_name, OPENPARAMS)) == INVALID_HANDLE_VALUE)
{ {
post("** ERROR ** could not open device %s:\n failure(%d): %s\n", error("[comport] ** ERROR ** could not open device %s:\n failure(%d): %s\n",
sys_com_port[com_nr],errno,strerror(errno)); x->serial_device->s_name,errno,strerror(errno));
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
@ -580,8 +600,8 @@ static int open_serial(int com_nr, t_comport *x)
/* Save the Current Port Configuration */ /* Save the Current Port Configuration */
if(tcgetattr(fd, old) == -1 || tcgetattr(fd, new) == -1){ if(tcgetattr(fd, old) == -1 || tcgetattr(fd, new) == -1){
post("** ERROR ** could not get termios-structure of device %s\n", error("[comport] ** ERROR ** could not get termios-structure of device %s\n",
sys_com_port[com_nr]); x->serial_device->s_name);
close(fd); close(fd);
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
@ -613,17 +633,18 @@ static int open_serial(int com_nr, t_comport *x)
set_bits(x,8); /* CS8 */ set_bits(x,8); /* CS8 */
set_stopflag(x,0); /* ~CSTOPB */ set_stopflag(x,0); /* ~CSTOPB */
set_ctsrts(x,0); /* ~CRTSCTS;*/ set_ctsrts(x,0); /* ~CRTSCTS;*/
set_xonxoff(x,1); /* (IXON | IXOFF | IXANY) */ set_xonxoff(x,0); /* (IXON | IXOFF | IXANY) */
set_baudrate(x,*baud); set_baudrate(x,*baud);
if(tcsetattr(fd, TCSAFLUSH, new) != -1) if(tcsetattr(fd, TCSAFLUSH, new) != -1)
{ {
post("Opened serial line device %s\n",sys_com_port[com_nr]); post("[comport] opened serial line device %d (%s)\n",
com_num,x->serial_device->s_name);
} }
else else
{ {
post("** ERROR ** could not set params to ioctl of device %s\n", error("[comport] ** ERROR ** could not set params to ioctl of device %s\n",
sys_com_port[com_nr]); x->serial_device->s_name);
close(fd); close(fd);
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
@ -652,10 +673,10 @@ static int set_serial(t_comport *x)
return 1; return 1;
} }
static int write_serial(t_comport *x, unsigned char chr) static int write_serial(t_comport *x, unsigned char serial_byte)
{ {
return write(x->comhandle,(char *) &chr,1); return write(x->comhandle,(char *) &serial_byte,1);
/* flush pending I/O chars */ /* flush pending I/O chars */
/* but nowadays discards them ;-( /* but nowadays discards them ;-(
@ -678,7 +699,6 @@ static void comport_pollintervall(t_comport *x, t_floatarg g)
static void comport_tick(t_comport *x) static void comport_tick(t_comport *x)
{ {
unsigned char chr;
int err; int err;
HANDLE fd = x->comhandle; HANDLE fd = x->comhandle;
@ -690,49 +710,43 @@ static void comport_tick(t_comport *x)
/* while there are bytes, read them and send them out, ignore errors */ /* while there are bytes, read them and send them out, ignore errors */
#ifdef _WIN32 #ifdef _WIN32
{ {
DWORD dwCommEvent; unsigned char serial_byte[1000];
DWORD dwRead; DWORD dwRead;
OVERLAPPED osReader = {0};
DWORD dwX;
err = 0; err = 0;
/* osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!SetCommMask(x->comhandle, EV_RXCHAR)) if(ReadFile(x->comhandle, serial_byte, 1000, &dwRead, &osReader))
post(" Error setting communications event mask for serial device"); {
if(dwRead > 0)
for ( ; ; ) { {
if (WaitCommEvent(x->comhandle, &dwCommEvent,NULL)) { for(dwX=0;dwX<dwRead;dwX++)
*/ {
do { outlet_float(x->x_obj.ob_outlet, (t_float) serial_byte[dwX]);
}
if(ReadFile(x->comhandle, &chr, 1, &dwRead, NULL)) }
if(dwRead > 0)
outlet_float(x->x_obj.ob_outlet, (t_float) chr);
else{
err = -1;
break;
}
} while (dwRead);
/*
} }
else{ else
post("serial dev: Error in WaitCommEvent"); {
break; err = -1;
} }
} CloseHandle(osReader.hEvent);
*/
} }
#else #else
{ {
unsigned char serial_byte;
fd_set com_rfds; fd_set com_rfds;
FD_ZERO(&com_rfds); FD_ZERO(&com_rfds);
FD_SET(fd,&com_rfds); FD_SET(fd,&com_rfds);
while((err=select(fd+1,&com_rfds,NULL,NULL,&null_tv)) > 0) { while((err=select(fd+1,&com_rfds,NULL,NULL,&null_tv)) > 0) {
err = read(fd,(char *) &chr,1); err = read(fd,(char *) &serial_byte,1);
/* while( (err = read(fd,(char *) &chr,1)) > 0){ */ /* while( (err = read(fd,(char *) &serial_byte,1)) > 0){ */
outlet_float(x->x_obj.ob_outlet, (t_float) chr); outlet_float(x->x_obj.ob_outlet, (t_float) serial_byte);
}; };
} }
@ -749,41 +763,54 @@ static void comport_tick(t_comport *x)
static void comport_float(t_comport *x, t_float f) static void comport_float(t_comport *x, t_float f)
{ {
unsigned char chr = ((int) f) & 0xFF; /* brutal conv */ unsigned char serial_byte = ((int) f) & 0xFF; /* brutal conv */
if (write_serial(x,chr) != 1) if (write_serial(x,serial_byte) != 1)
{ {
post("Write error, maybe TX-OVERRUNS on serial line"); post("Write error, maybe TX-OVERRUNS on serial line");
} }
} }
static void *comport_new(t_floatarg comnr, t_floatarg fbaud) { static void *comport_new(t_floatarg com_num, t_floatarg fbaud)
{
t_comport test; t_comport test;
t_comport *x; t_comport *x;
int com_nr = comnr;
HANDLE fd; HANDLE fd;
/* Open the Comport for RD and WR and get a handle */ /* for UNIX, this is a glob pattern for matching devices */
test.baud = fbaud; #ifdef _WIN32
fd = open_serial(com_nr,&test); 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__ */
#endif /* _WIN32 */
/* Open the Comport for RD and WR and get a handle */
strncpy(test.serial_device_name,serial_device_name,strlen(serial_device_name)+1);
test.baud = fbaud;
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 */
x = (t_comport *)pd_new(comport_class); x = (t_comport *)pd_new(comport_class);
x->comport = (short)com_num;
strncpy(x->serial_device_name,serial_device_name,strlen(serial_device_name)+1);
x->baud = test.baud;
x->comhandle = fd; /* holds the comport handle */
if(fd == INVALID_HANDLE_VALUE ){ if(fd == INVALID_HANDLE_VALUE ){
/* postings in open routine */ /* postings in open routine */
x->comport = com_nr; post("[comport] invalid handle for %s",x->serial_device_name);
x->baud = test.baud;
x->comhandle = fd; /* holds the comport handle */
} }
else { else {
x->comport = com_nr;
x->baud = test.baud;
x->comhandle = fd; /* holds the comport handle */
#ifdef _WIN32 #ifdef _WIN32
memcpy(&(test.dcb_old),&(x->dcb_old),sizeof(DCB)); /* save the old com config */ memcpy(&(test.dcb_old),&(x->dcb_old),sizeof(DCB)); /* save the old com config */
memcpy(&(test.dcb),&(x->dcb),sizeof(DCB)); /* for the new com config */ memcpy(&(test.dcb),&(x->dcb),sizeof(DCB)); /* for the new com config */
@ -834,10 +861,10 @@ static void comport_baud(t_comport *x,t_floatarg f)
if(x->comhandle == INVALID_HANDLE_VALUE)return; if(x->comhandle == INVALID_HANDLE_VALUE)return;
if(set_serial(x) == 0){ if(set_serial(x) == 0){
post("** ERROR ** could not set baudrate of device %s\n", error("[comport] ** ERROR ** could not set baudrate of device %s\n",
sys_com_port[x->comport]); x->serial_device->s_name);
} }
else post("set baudrate of %s to %f\n",sys_com_port[x->comport],x->baud); else post("set baudrate of %s to %f\n",x->serial_device->s_name,x->baud);
} }
static void comport_bits(t_comport *x,t_floatarg f) static void comport_bits(t_comport *x,t_floatarg f)
@ -847,10 +874,10 @@ static void comport_bits(t_comport *x,t_floatarg f)
if(x->comhandle == INVALID_HANDLE_VALUE)return; if(x->comhandle == INVALID_HANDLE_VALUE)return;
if(set_serial(x) == 0){ if(set_serial(x) == 0){
post("** ERROR ** could not set bits of device %s\n", error("[comport] ** ERROR ** could not set bits of device %s\n",
sys_com_port[x->comport]); x->serial_device->s_name);
} }
else post("set bits of %s to %f\n",sys_com_port[x->comport],f); else post("set bits of %s to %f\n",x->serial_device->s_name,f);
} }
@ -861,10 +888,10 @@ static void comport_parity(t_comport *x,t_floatarg f)
if(x->comhandle == INVALID_HANDLE_VALUE)return; if(x->comhandle == INVALID_HANDLE_VALUE)return;
if(set_serial(x) == 0){ if(set_serial(x) == 0){
post("** ERROR ** could not set extra paritybit of device %s\n", error("[comport] ** ERROR ** could not set extra paritybit of device %s\n",
sys_com_port[x->comport]); x->serial_device->s_name);
} }
else post("set extra paritybit of %s to %f\n",sys_com_port[x->comport],f); else post("[comport] set extra paritybit of %s to %f\n",x->serial_device->s_name,f);
} }
static void comport_stopbit(t_comport *x,t_floatarg f) static void comport_stopbit(t_comport *x,t_floatarg f)
@ -874,10 +901,10 @@ static void comport_stopbit(t_comport *x,t_floatarg f)
if(x->comhandle == INVALID_HANDLE_VALUE)return; if(x->comhandle == INVALID_HANDLE_VALUE)return;
if(set_serial(x) == 0){ if(set_serial(x) == 0){
post("** ERROR ** could not set extra stopbit of device %s\n", error("[comport] ** ERROR ** could not set extra stopbit of device %s\n",
sys_com_port[x->comport]); x->serial_device->s_name);
} }
else post("set extra stopbit of %s to %f\n",sys_com_port[x->comport],f); else post("[comport] set extra stopbit of %s to %f\n",x->serial_device->s_name,f);
} }
static void comport_rtscts(t_comport *x,t_floatarg f) static void comport_rtscts(t_comport *x,t_floatarg f)
@ -887,10 +914,10 @@ static void comport_rtscts(t_comport *x,t_floatarg f)
if(x->comhandle == INVALID_HANDLE_VALUE)return; if(x->comhandle == INVALID_HANDLE_VALUE)return;
if(set_serial(x) == 0){ if(set_serial(x) == 0){
post("** ERROR ** could not set rts_cts of device %s\n", error("[comport] ** ERROR ** could not set rts_cts of device %s\n",
sys_com_port[x->comport]); x->serial_device->s_name);
} }
else post("set rts-cts of %s to %f\n",sys_com_port[x->comport],f); else post("[comport] set rts-cts of %s to %f\n",x->serial_device->s_name,f);
} }
static void comport_xonxoff(t_comport *x,t_floatarg f) static void comport_xonxoff(t_comport *x,t_floatarg f)
@ -900,11 +927,11 @@ static void comport_xonxoff(t_comport *x,t_floatarg f)
if(x->comhandle == INVALID_HANDLE_VALUE)return; if(x->comhandle == INVALID_HANDLE_VALUE)return;
if(set_serial(x) == 0){ if(set_serial(x) == 0){
post("** ERROR ** could not set xonxoff of device %s\n", error("[comport] ** ERROR ** could not set xonxoff of device %s\n",
sys_com_port[x->comport]); x->serial_device->s_name);
} }
else if(x->verbose > 0) else if(x->verbose > 0)
post("set xonxoff of %s to %f\n",sys_com_port[x->comport],f); post("[comport] set xonxoff of %s to %f\n",x->serial_device->s_name,f);
} }
static void comport_close(t_comport *x) static void comport_close(t_comport *x)
@ -933,12 +960,12 @@ 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){ if(x->comport >= 0 && x->comport < COMPORT_MAX){
sys_com_port[x->comport] = s->s_name; x->serial_device->s_name = s->s_name;
if(x->verbose > 0) if(x->verbose > 0)
post("comport %d: set devicename %s",x->comport,sys_com_port[x->comport]); post("[comport] %d: set devicename %s",x->comport,x->serial_device->s_name);
} }
else if(x->verbose > 0) else if(x->verbose > 0)
post("comport %d: could not set devicename %s",x->comport,s->s_name); post("[comport] %d: could not set devicename %s",x->comport,s->s_name);
} }
@ -962,15 +989,15 @@ static void comport_verbose(t_comport *x, t_floatarg f)
{ {
x->verbose = f; x->verbose = f;
if(f > 0) if(f > 0)
post("Comport Verbose is on: %d", (int) f); post("[comport] verbose is on: %d", (int) f);
} }
static void comport_help(t_comport *x) static void comport_help(t_comport *x)
{ {
post("Comport %d (baud %f):",x->comport,x->baud); post("[comport] serial port %d (baud %f):",x->comport,x->baud);
if(x->comport >= 0 && x->comport < COMPORT_MAX){ if(x->comport >= 0 && x->comport < COMPORT_MAX){
post("devicename: %s",sys_com_port[x->comport]); post("\tdevicename: %s",x->serial_device->s_name);
} }
post(" Methods:"); post(" Methods:");