75 #if defined(VRPN_USE_MODBUS) && defined(VRPN_USE_WINSOCK2)
80 #define STATUS_RESETTING (-1) // Resetting the device
81 #define STATUS_SYNCING (0) // Looking for the first character of report
82 #define STATUS_READING (1) // Looking for the rest of the report
84 #define TIMEOUT_TIME_INTERVAL (2000000L) // max time between reports (usec)
91 vrpn_OmegaTemperature::vrpn_OmegaTemperature (
const char * name,
vrpn_Connection * c,
92 const char * port,
float temp1,
float temp2,
bool control_on):
101 d_modbus = modbus_new_rtu(port, baud, parity, 8, stop_bits);
107 buttons[0] = control_on;
122 fprintf(stderr,
"vrpn_OmegaTemperature: can't register handler\n");
127 fprintf(stderr,
"vrpn_OmegaTemperature: can't register handler\n");
132 fprintf(stderr,
"vrpn_OmegaTemperature: can't register handler\n");
136 fprintf(stderr,
"vrpn_OmegaTemperature: Can't get connection!\n");
148 bool vrpn_OmegaTemperature::set_reference_temperature(
unsigned channel,
float value)
155 int whole =
static_cast<int>(value);
156 int dec =
static_cast<int>(value*10) - whole*10;
157 sprintf(command,
"S%d %03d%d\r", channel+1, whole,dec);
160 return (
vrpn_write_characters(serial_fd, (
unsigned char *)(command), strlen(command)) == strlen(command));
167 bool vrpn_OmegaTemperature::set_control_status(
bool on)
172 sprintf(command,
"ON\r");
174 sprintf(command,
"OFF\r");
178 return (
vrpn_write_characters(serial_fd, (
unsigned char *)(command), strlen(command)) == strlen(command));
190 bool vrpn_OmegaTemperature::request_temperature(
unsigned channel)
194 sprintf(command,
"T%d\r", channel+1);
196 printf(
"Sending command: %s", command);
200 return (
vrpn_write_characters(serial_fd, (
unsigned char *)(command), strlen(command)) == strlen(command));
208 float vrpn_OmegaTemperature::convert_bytes_to_reading(
const char *buf)
214 if (*buf ==
'-') { buf++; }
217 if (sscanf(buf,
"%f%c", &val, &c) != 2) {
224 if ( (c !=
'E') && (c !=
'C') && (c !=
'\r') ) {
232 int vrpn_OmegaTemperature::reset(
void)
243 if (!set_reference_temperature(0,
static_cast<float>(o_channel[0]))) {
244 fprintf(stderr,
"vrpn_OmegaTemperature::reset(): Cannot send set ref temp 0, trying again\n");
247 if (!set_reference_temperature(1,
static_cast<float>(o_channel[1]))) {
248 fprintf(stderr,
"vrpn_OmegaTemperature::reset(): Cannot send set ref temp 1, trying again\n");
251 if (!set_control_status(o_channel[0] != 0)) {
252 fprintf(stderr,
"vrpn_OmegaTemperature::reset(): Cannot send set control status, trying again\n");
259 d_next_channel_to_read = 0;
260 if (!request_temperature(d_next_channel_to_read)) {
261 fprintf(stderr,
"vrpn_OmegaTemperature::reset(): Cannot request temperature, trying again\n");
277 int vrpn_OmegaTemperature::get_report(
void)
305 for (i = 1; i <
sizeof(d_buffer); i++) {
309 printf(
"... Got the 1st char\n");
327 if (ret != 0) printf(
"... got %d total characters\n", d_bufcount);
329 if (d_buffer[d_bufcount-1] !=
'\r') {
341 printf(
" Complete report: \n%s\n",d_buffer);
343 float value = convert_bytes_to_reading(d_buffer);
344 if (value == -1000) {
346 sprintf(msg,
"Invalid report, channel %d, resetting", d_next_channel_to_read);
350 channel[d_next_channel_to_read] = value;
353 printf(
"got a complete report (%d chars)!\n", d_bufcount);
360 d_next_channel_to_read = (d_next_channel_to_read + 1) % 6;
361 if (!request_temperature(d_next_channel_to_read)) {
363 sprintf(msg,
"Can't request reading, channel %d, resetting", d_next_channel_to_read);
379 bool vrpn_OmegaTemperature::set_specified_channel(
unsigned channel, vrpn_float64 value)
385 set_reference_temperature(channel,
static_cast<float>(value));
386 o_channel[channel] = value;
389 o_channel[2] = value;
390 buttons[0] = ( value != 0 );
391 set_control_status( value != 0);
399 int vrpn_OmegaTemperature::handle_request_message(
void *userdata,
vrpn_HANDLERPARAM p)
401 const char *bufptr = p.
buffer;
405 vrpn_OmegaTemperature *me = (vrpn_OmegaTemperature *)userdata;
414 if ( (chan_num < 0) || (chan_num >= me->o_num_channel) ) {
416 sprintf(msg,
"vrpn_OmegaTemperature::handle_request_message(): Index out of bounds (%d of %d), value %lg\n",
417 chan_num, me->num_channel, value);
422 me->set_specified_channel(chan_num, value);
426 int vrpn_OmegaTemperature::handle_request_channels_message(
void* userdata,
vrpn_HANDLERPARAM p)
429 const char* bufptr = p.
buffer;
432 vrpn_OmegaTemperature* me = (vrpn_OmegaTemperature *)userdata;
437 if (num > me->o_num_channel) {
439 sprintf(msg,
"vrpn_OmegaTemperature::handle_request_channels_message(): Index out of bounds (%d of %d), clipping\n",
440 num, me->o_num_channel);
442 num = me->o_num_channel;
444 for (i = 0; i < num; i++) {
446 me->set_specified_channel(i, me->o_channel[i]);
454 int vrpn_OmegaTemperature::handle_connect_message(
void *userdata,
vrpn_HANDLERPARAM)
456 vrpn_OmegaTemperature *me = (vrpn_OmegaTemperature *)userdata;
462 void vrpn_OmegaTemperature::report_changes(vrpn_uint32 class_of_service)
469 void vrpn_OmegaTemperature::report(vrpn_uint32 class_of_service)
483 void vrpn_OmegaTemperature::mainloop()
506 while (get_report()) {};
508 struct timeval current_time;
511 sprintf(errmsg,
"Timeout... current_time=%ld:%ld, timestamp=%ld:%ld",
512 current_time.tv_sec,
static_cast<long>(current_time.tv_usec),
513 timestamp.tv_sec,
static_cast<long>(timestamp.tv_usec));