use pd_error or comport_verbose instead of post

and rename the old comport_verbose() function to comport_set_verbose()

svn path=/trunk/externals/iem/comport/; revision=17564
This commit is contained in:
IOhannes m zmölnig 2015-09-16 08:57:56 +00:00
parent e27a36e4f7
commit 2766f264eb

View file

@ -46,6 +46,7 @@ MP 20100201 use a buffer for writes, write takes place during clock callback com
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#define comport_verbose if(x->verbose > 0)post
typedef struct comport typedef struct comport
{ {
@ -293,7 +294,7 @@ 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);
static void comport_ports(t_comport *x); static void comport_ports(t_comport *x);
static void comport_verbose(t_comport *x, t_floatarg f); static void comport_set_verbose(t_comport *x, t_floatarg f);
static void comport_help(t_comport *x); static void comport_help(t_comport *x);
void comport_setup(void); void comport_setup(void);
@ -365,7 +366,7 @@ static float set_stopflag(t_comport *x, t_float nr)
x->dcb.StopBits = 1; /* ONE5STOPBITS = 1 */ x->dcb.StopBits = 1; /* ONE5STOPBITS = 1 */
return nr; return nr;
} }
post("comport stopbit number (%g) out of range (0, 1.5, 2)", nr); pd_error(x, "comport stopbit number (%g) out of range (0, 1.5, 2)", nr);
return 0; return 0;
} }
@ -472,7 +473,7 @@ static HANDLE open_serial(unsigned int com_num, t_comport *x)
{ {
if(com_num < 1 || com_num >= COMPORT_MAX) if(com_num < 1 || com_num >= COMPORT_MAX)
{ {
post("comport number %d out of range (1-%d)", com_num, COMPORT_MAX); pd_error(x, "comport number %d out of range (1-%d)", com_num, COMPORT_MAX);
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
#ifdef _MSC_VER #ifdef _MSC_VER
@ -491,7 +492,7 @@ static HANDLE open_serial(unsigned int com_num, t_comport *x)
#endif #endif
x->serial_device = gensym(buffer); x->serial_device = gensym(buffer);
} }
post("Opening %s", &x->serial_device->s_name[4]);/* skip slashes and dot */ comport_verbose("[comport] Opening %s", &x->serial_device->s_name[4]);/* skip slashes and dot */
fd = CreateFileA( x->serial_device->s_name, fd = CreateFileA( x->serial_device->s_name,
GENERIC_READ | GENERIC_WRITE, GENERIC_READ | GENERIC_WRITE,
0, 0,
@ -584,7 +585,7 @@ static HANDLE open_serial(unsigned int com_num, t_comport *x)
} }
if(set_serial(x)) if(set_serial(x))
{ {
post("[comport] opened serial line device %d (%s)\n", comport_verbose("[comport] opened serial line device %d (%s)\n",
com_num, &x->serial_device->s_name[4]); com_num, &x->serial_device->s_name[4]);
} }
else else
@ -597,7 +598,7 @@ static HANDLE open_serial(unsigned int com_num, t_comport *x)
if (!GetCommTimeouts(fd, &(x->old_timeouts))) if (!GetCommTimeouts(fd, &(x->old_timeouts)))
{ {
post("[comport] Couldn't get old timeouts for serial device (%d)", GetLastError()); pd_error(x, "[comport] Couldn't get old timeouts for serial device (%d)", GetLastError());
} }
/* setting new timeouts for read to immediately return */ /* setting new timeouts for read to immediately return */
@ -609,12 +610,12 @@ static HANDLE open_serial(unsigned int com_num, t_comport *x)
if (!SetCommTimeouts(fd, &timeouts)) if (!SetCommTimeouts(fd, &timeouts))
{ {
post("Couldn't set timeouts for serial device (%d)", GetLastError()); pd_error(x, "Couldn't set timeouts for serial device (%d)", GetLastError());
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
if (!SetupComm(x->comhandle, 4096L, 4096L))/* try to get big buffers to avoid overruns*/ if (!SetupComm(x->comhandle, 4096L, 4096L))/* try to get big buffers to avoid overruns*/
{ {
post("[comport] Couldn't do SetupComm (%d)", GetLastError()); pd_error(x, "[comport] Couldn't do SetupComm (%d)", GetLastError());
} }
x->comport = com_num;/* output on next tick */ x->comport = com_num;/* output on next tick */
return fd; return fd;
@ -626,15 +627,15 @@ static HANDLE close_serial(t_comport *x)
{ {
if (!SetCommState(x->comhandle, &(x->dcb_old))) if (!SetCommState(x->comhandle, &(x->dcb_old)))
{ {
post("[comport] ** ERROR ** couldn't reset params to DCB of device %s\n", pd_error(x, "[comport] ** ERROR ** couldn't reset params to DCB of device %s\n",
&x->serial_device->s_name[4]); &x->serial_device->s_name[4]);
} }
if (!SetCommTimeouts(x->comhandle, &(x->old_timeouts))) if (!SetCommTimeouts(x->comhandle, &(x->old_timeouts)))
{ {
post("[comport] Couldn't reset old_timeouts for serial device"); pd_error(x, "[comport] Couldn't reset old_timeouts for serial device");
} }
CloseHandle(x->comhandle); CloseHandle(x->comhandle);
post("[comport] closed %s", &x->serial_device->s_name[4]); comport_verbose("[comport] closed %s", &x->serial_device->s_name[4]);
} }
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
@ -690,12 +691,12 @@ 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) if(baudratetable[i] != *baud)
post("[comport]: %d not valid, using closest value: ", *baud, baudratetable[i]); error("[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)
{ {
post("*Warning* The baud rate %d is not supported or out of range, using 9600\n",*baud); error("*Warning* The baud rate %d is not supported or out of range, using 9600\n",*baud);
i = 8; i = 8;
} }
*baud = baudratetable[i]; *baud = baudratetable[i];
@ -708,12 +709,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);
if (x->verbose > 0) comport_verbose("[comport] set_baudrate: Setting baud rate to %g with baudbits 0x%X", baud, baudbits);
post("[comport] set_baudrate: Setting baud rate to %g with baudbits 0x%X", baud, baudbits);
if( cfsetispeed(tio, baudbits) != 0 ) if( cfsetispeed(tio, baudbits) != 0 )
post("[comport]: ERROR failed to set bitrate: %d", baudbits); pd_error(x, "[comport]: ERROR failed to set bitrate: %d", baudbits);
if( cfsetospeed(tio, baudbits) != 0 ) if( cfsetospeed(tio, baudbits) != 0 )
post("[comport]: ERROR failed to set bitrate: %d", baudbits); pd_error(x, "[comport]: ERROR failed to set bitrate: %d", baudbits);
return baud; return baud;
} }
@ -880,7 +880,7 @@ static int open_serial(unsigned int com_num, t_comport *x)
/* if com_num == USE_DEVICENAME, use device name directly, else try port # */ /* if com_num == USE_DEVICENAME, use device name directly, else try port # */
if((com_num != USE_DEVICENAME)&&(com_num >= COMPORT_MAX)) if((com_num != USE_DEVICENAME)&&(com_num >= COMPORT_MAX))
{ {
post("[comport] ** WARNING ** port %d not valid, must be between 0 and %d", pd_error(x, "[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;
} }
@ -918,7 +918,7 @@ static int open_serial(unsigned int com_num, t_comport *x)
x->serial_device = gensym(glob_buffer.gl_pathv[com_num]); x->serial_device = gensym(glob_buffer.gl_pathv[com_num]);
else else
{ {
post("[comport] ** WARNING ** port #%d does not exist! (max == %d)", pd_error(x, "[comport] ** WARNING ** port #%d does not exist! (max == %d)",
com_num,glob_buffer.gl_pathc - 1); com_num,glob_buffer.gl_pathc - 1);
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
@ -975,7 +975,7 @@ static int open_serial(unsigned int com_num, t_comport *x)
if(tcsetattr(fd, TCSAFLUSH, new) != -1) if(tcsetattr(fd, TCSAFLUSH, new) != -1)
{ {
post("[comport] opened serial line device %d (%s)\n", comport_verbose("[comport] opened serial line device %d (%s)\n",
com_num,x->serial_device->s_name); com_num,x->serial_device->s_name);
} }
else else
@ -998,7 +998,7 @@ static int close_serial(t_comport *x)
{ {
tcsetattr(fd, TCSANOW, tios); tcsetattr(fd, TCSANOW, tios);
close(fd); close(fd);
post("[comport] closed port %i (%s)", x->comport, x->serial_device->s_name); comport_verbose("[comport] closed port %i (%s)", x->comport, x->serial_device->s_name);
} }
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
@ -1154,7 +1154,7 @@ static void comport_tick(t_comport *x)
if(err < 0) if(err < 0)
{ /* if a read error detected */ { /* if a read error detected */
if(x->rxerrors < 10) /* ten times max */ if(x->rxerrors < 10) /* ten times max */
post("[comport]: RXERRORS on serial line (%d)\n", whicherr); pd_error(x, "[comport]: RXERRORS on serial line (%d)\n", whicherr);
x->rxerrors++; /* remember */ x->rxerrors++; /* remember */
} }
/* now if anything to send, send the output buffer */ /* now if anything to send, send the output buffer */
@ -1178,7 +1178,7 @@ static void comport_tick(t_comport *x)
osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osWrite.hEvent == NULL) if (osWrite.hEvent == NULL)
{ {
post("[comport]: Couldn't create event. Transmission aborted."); pd_error(x, "[comport]: Couldn't create event. Transmission aborted.");
goto endsendevent; goto endsendevent;
} }
@ -1187,14 +1187,14 @@ static void comport_tick(t_comport *x)
dwErr = GetLastError(); dwErr = GetLastError();
if (dwErr != ERROR_IO_PENDING) if (dwErr != ERROR_IO_PENDING)
{ {
post("[comport]: WriteFile error: %d", (int)dwErr); pd_error(x, "[comport]: WriteFile error: %d", (int)dwErr);
goto endsendevent; goto endsendevent;
} }
} }
if (!GetOverlappedResult(x->comhandle, &osWrite, &numTransferred, TRUE)) if (!GetOverlappedResult(x->comhandle, &osWrite, &numTransferred, TRUE))
{/* wait for the character(s) to be sent */ {/* wait for the character(s) to be sent */
dwErr = GetLastError(); dwErr = GetLastError();
post("[comport]: WriteFile:GetOverlappedResult error: %d", (int)dwErr); pd_error(x, "[comport]: WriteFile:GetOverlappedResult error: %d", (int)dwErr);
} }
endsendevent: endsendevent:
CloseHandle(osWrite.hEvent); CloseHandle(osWrite.hEvent);
@ -1215,7 +1215,7 @@ static int write_serial(t_comport *x, unsigned char serial_byte)
{ {
if(x->comhandle == INVALID_HANDLE_VALUE) if(x->comhandle == INVALID_HANDLE_VALUE)
{ {
post ("[comport]: Serial port is not open"); comport_verbose ("[comport]: Serial port is not open");
return 0; return 0;
} }
else if(x->x_outbuf_wr_index < x->x_outbuf_len) else if(x->x_outbuf_wr_index < x->x_outbuf_len)
@ -1224,7 +1224,7 @@ static int write_serial(t_comport *x, unsigned char serial_byte)
return 1; return 1;
} }
/* handle overrun error */ /* handle overrun error */
post ("[comport]: buffer is full"); pd_error (x, "[comport]: buffer is full");
return 0; return 0;
} }
@ -1233,12 +1233,12 @@ static int write_serials(t_comport *x, unsigned char *serial_buf, int buf_length
int i; int i;
if(x->comhandle == INVALID_HANDLE_VALUE) if(x->comhandle == INVALID_HANDLE_VALUE)
{ {
post ("[comport]: Serial port is not open"); pd_error (x, "[comport]: Serial port is not open");
return 0; return 0;
} }
for (i = 0; ((i < buf_length) && (x->x_outbuf_wr_index < x->x_outbuf_len)); ++x->x_outbuf_wr_index, ++i) for (i = 0; ((i < buf_length) && (x->x_outbuf_wr_index < x->x_outbuf_len)); ++x->x_outbuf_wr_index, ++i)
x->x_outbuf[x->x_outbuf_wr_index] = serial_buf[i]; x->x_outbuf[x->x_outbuf_wr_index] = serial_buf[i];
if (i != buf_length) post ("[comport]: buffer is full"); if (i != buf_length) pd_error (x, "[comport]: buffer is full");
return i; return i;
} }
@ -1248,7 +1248,7 @@ static void comport_float(t_comport *x, t_float f)
if (write_serial(x,serial_byte) != 1) if (write_serial(x,serial_byte) != 1)
{ {
post("Write error, maybe TX-OVERRUNS on serial line"); pd_error(x, "Write error, maybe TX-OVERRUNS on serial line");
} }
} }
@ -1261,7 +1261,7 @@ static void comport_list(t_comport *x, t_symbol *s, int argc, t_atom *argv)
count = argc; count = argc;
if (argc > COMPORT_BUF_SIZE) if (argc > COMPORT_BUF_SIZE)
{ {
post ("[comport] truncated list of %d elements to %d", argc, count); pd_error (x, "[comport] truncated list of %d elements to %d", argc, count);
count = COMPORT_BUF_SIZE; count = COMPORT_BUF_SIZE;
} }
for(i = 0; i < count; i++) for(i = 0; i < count; i++)
@ -1392,7 +1392,7 @@ allows COM port numbers to be specified. */
static void comport_free(t_comport *x) static void comport_free(t_comport *x)
{ {
post("[comport] free serial..."); comport_verbose("[comport] free serial...");
clock_unset(x->x_clock); clock_unset(x->x_clock);
clock_free(x->x_clock); clock_free(x->x_clock);
x->comhandle = close_serial(x); x->comhandle = close_serial(x);
@ -1406,7 +1406,7 @@ static void comport_baud(t_comport *x,t_floatarg f)
{ {
if(f == x->baud) if(f == x->baud)
{ {
post("baudrate already %g\n",x->baud); comport_verbose("[comport] baudrate already %g\n",x->baud);
return; return;
} }
@ -1423,8 +1423,7 @@ static void comport_baud(t_comport *x,t_floatarg f)
x->serial_device->s_name); x->serial_device->s_name);
#endif #endif
} }
else if(x->verbose > 0) else comport_verbose("[comport] set baudrate of %s to %g\n",
post("set baudrate of %s to %g\n",
#ifdef _WIN32 #ifdef _WIN32
&x->serial_device->s_name[4], x->baud); &x->serial_device->s_name[4], x->baud);
#else #else
@ -1448,8 +1447,7 @@ static void comport_bits(t_comport *x,t_floatarg f)
#endif #endif
return; return;
} }
else if(x->verbose > 0) else comport_verbose("[comport] set bits of %s to %g\n",
post("set bits of %s to %g\n",
#ifdef _WIN32 #ifdef _WIN32
&x->serial_device->s_name[4], f); &x->serial_device->s_name[4], f);
#else #else
@ -1475,8 +1473,7 @@ static void comport_parity(t_comport *x,t_floatarg f)
#endif #endif
return; return;
} }
else if(x->verbose > 0) else comport_verbose("[comport] set extra paritybit of %s to %g\n",
post("[comport] set extra paritybit of %s to %g\n",
#ifdef _WIN32 #ifdef _WIN32
&x->serial_device->s_name[4], f); &x->serial_device->s_name[4], f);
#else #else
@ -1502,12 +1499,12 @@ static void comport_stopbit(t_comport *x, t_floatarg f)
#endif #endif
return; return;
} }
else if(x->verbose > 0) else comport_verbose
#ifdef _WIN32 #ifdef _WIN32
post("[comport] set stopbits of %s to %g\n", ("[comport] set stopbits of %s to %g\n",
&x->serial_device->s_name[4], f); &x->serial_device->s_name[4], f);
#else #else
post("[comport] set extra stopbit of %s to %g\n", ("[comport] set extra stopbit of %s to %g\n",
x->serial_device->s_name, f); x->serial_device->s_name, f);
#endif #endif
x->stop_bits = f; x->stop_bits = f;
@ -1529,8 +1526,7 @@ static void comport_rtscts(t_comport *x,t_floatarg f)
#endif #endif
return; return;
} }
else if(x->verbose > 0) else comport_verbose("[comport] set rts-cts of %s to %g\n",
post("[comport] set rts-cts of %s to %g\n",
#ifdef _WIN32 #ifdef _WIN32
&x->serial_device->s_name[4], f); &x->serial_device->s_name[4], f);
#else #else
@ -1554,8 +1550,7 @@ static void comport_dtr(t_comport *x,t_floatarg f)
x->serial_device->s_name); x->serial_device->s_name);
#endif #endif
} }
else if(x->verbose > 0) else comport_verbose("[comport] set dtr of %s to %g\n",
post("[comport] set dtr of %s to %g\n",
#ifdef _WIN32 #ifdef _WIN32
&x->serial_device->s_name[4], f); &x->serial_device->s_name[4], f);
#else #else
@ -1578,8 +1573,7 @@ static void comport_rts(t_comport *x,t_floatarg f)
x->serial_device->s_name); x->serial_device->s_name);
#endif #endif
} }
else if(x->verbose > 0) else comport_verbose("[comport] set rts of %s to %g\n",
post("[comport] set rts of %s to %g\n",
#ifdef _WIN32 #ifdef _WIN32
&x->serial_device->s_name[4], f); &x->serial_device->s_name[4], f);
#else #else
@ -1602,8 +1596,7 @@ static void comport_break(t_comport *x,t_floatarg f)
x->serial_device->s_name); x->serial_device->s_name);
#endif #endif
} }
else if(x->verbose > 0) else comport_verbose("[comport] set break of %s to %g\n",
post("[comport] set break of %s to %g\n",
#ifdef _WIN32 #ifdef _WIN32
&x->serial_device->s_name[4], f); &x->serial_device->s_name[4], f);
#else #else
@ -1627,8 +1620,7 @@ static void comport_xonxoff(t_comport *x,t_floatarg f)
#endif #endif
return; return;
} }
else if(x->verbose > 0) else comport_verbose("[comport] set xonxoff of %s to %g\n",
post("[comport] set xonxoff of %s to %g\n",
#ifdef _WIN32 #ifdef _WIN32
&x->serial_device->s_name[4], f); &x->serial_device->s_name[4], f);
#else #else
@ -1724,7 +1716,7 @@ static void comport_enum(t_comport *x)
else else
CloseHandle(fd); CloseHandle(fd);
if (dw == 0)post("\t%d - COM%d (free)", i, i); if (dw == 0)post("\t%d - COM%d (free)", i, i);
else if (dw == ERROR_ACCESS_DENIED)post("\t%d - COM%d (in use)", i, i); else if (dw == ERROR_ACCESS_DENIED)pd_error(x, "\t%d - COM%d (in use)", i, i);
} }
#else #else
unsigned int i; unsigned int i;
@ -1942,10 +1934,10 @@ static void comport_info(t_comport *x)
} }
/* ---------------- HELPER ------------------------- */ /* ---------------- HELPER ------------------------- */
static void comport_verbose(t_comport *x, t_floatarg f) static void comport_set_verbose(t_comport *x, t_floatarg f)
{ {
x->verbose = f; x->verbose = f;
if(f > 0) post("[comport] verbose is on: %d", (int) f); comport_verbose("[comport] verbose is on: %d", (int) f);
} }
static void comport_help(t_comport *x) static void comport_help(t_comport *x)
@ -2011,7 +2003,7 @@ void comport_setup(void)
class_addmethod(comport_class, (t_method)comport_print, gensym("print"), A_GIMME, 0); class_addmethod(comport_class, (t_method)comport_print, gensym("print"), A_GIMME, 0);
class_addmethod(comport_class, (t_method)comport_pollintervall, gensym("pollintervall"), A_FLOAT, 0); class_addmethod(comport_class, (t_method)comport_pollintervall, gensym("pollintervall"), A_FLOAT, 0);
class_addmethod(comport_class, (t_method)comport_retries, gensym("retries"), A_FLOAT, 0); class_addmethod(comport_class, (t_method)comport_retries, gensym("retries"), A_FLOAT, 0);
class_addmethod(comport_class, (t_method)comport_verbose, gensym("verbose"), A_FLOAT, 0); class_addmethod(comport_class, (t_method)comport_set_verbose, gensym("verbose"), A_FLOAT, 0);
class_addmethod(comport_class, (t_method)comport_help, gensym("help"), 0); class_addmethod(comport_class, (t_method)comport_help, gensym("help"), 0);
class_addmethod(comport_class, (t_method)comport_info, gensym("info"), 0); class_addmethod(comport_class, (t_method)comport_info, gensym("info"), 0);
class_addmethod(comport_class, (t_method)comport_devices, gensym("devices"), 0); class_addmethod(comport_class, (t_method)comport_devices, gensym("devices"), 0);