75#if defined(VRPN_USE_MODBUS) && defined(VRPN_USE_WINSOCK2)
80#define STATUS_RESETTING (-1)
81#define STATUS_SYNCING (0)
82#define STATUS_READING (1)
84#define TIMEOUT_TIME_INTERVAL (2000000L)
91vrpn_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;
110 o_channel[0] = temp1;
111 o_channel[1] = temp2;
112 o_channel[2] = control_on;
119 if (d_connection != NULL) {
120 if (register_autodeleted_handler(request_m_id, handle_request_message,
121 this, d_sender_id)) {
122 fprintf(stderr,
"vrpn_OmegaTemperature: can't register handler\n");
125 if (register_autodeleted_handler(request_channels_m_id, handle_request_channels_message,
126 this, d_sender_id)) {
127 fprintf(stderr,
"vrpn_OmegaTemperature: can't register handler\n");
130 if (register_autodeleted_handler(d_ping_message_id, handle_connect_message,
131 this, d_sender_id)) {
132 fprintf(stderr,
"vrpn_OmegaTemperature: can't register handler\n");
136 fprintf(stderr,
"vrpn_OmegaTemperature: Can't get connection!\n");
148bool 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));
167bool 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));
190bool 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));
208float 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') ) {
232int 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");
277int 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);
379bool 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);
399int 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);
426int 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]);
454int vrpn_OmegaTemperature::handle_connect_message(
void *userdata,
vrpn_HANDLERPARAM)
456 vrpn_OmegaTemperature *me = (vrpn_OmegaTemperature *)userdata;
462void vrpn_OmegaTemperature::report_changes(vrpn_uint32 class_of_service)
469void vrpn_OmegaTemperature::report(vrpn_uint32 class_of_service)
483void 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));
virtual void report(vrpn_uint32 class_of_service=vrpn_CONNECTION_LOW_LATENCY, const struct timeval time=vrpn_ANALOG_NOW)
Send a report whether something has changed or not (for servers) Optionally, tell what time to stamp ...
virtual void report_changes(vrpn_uint32 class_of_service=vrpn_CONNECTION_LOW_LATENCY, const struct timeval time=vrpn_ANALOG_NOW)
Send a report only if something has changed (for servers) Optionally, tell what time to stamp the val...
Generic connection class not specific to the transport mechanism.
This structure is what is passed to a vrpn_Connection message callback.
All types of client/server/peer objects in VRPN should be derived from the vrpn_BaseClass type descri...
const vrpn_uint32 vrpn_CONNECTION_RELIABLE
Classes of service for messages, specify multiple by ORing them together Priority of satisfying these...
Header containing macros formerly duplicated in a lot of implementation files.
#define VRPN_MSG_ERROR(msg)
#define VRPN_MSG_WARNING(msg)
int vrpn_write_characters(int comm, const unsigned char *buffer, size_t bytes)
Write the buffer to the serial port.
int vrpn_flush_input_buffer(int comm)
Throw out any characters within the input buffer.
int vrpn_read_available_characters(int comm, unsigned char *buffer, size_t bytes)
vrpn_Serial: Pulls all the serial port routines into one file to make porting to new operating system...
VRPN_API int vrpn_unbuffer(const char **buffer, timeval *t)
Utility routine for taking a struct timeval from a buffer that was sent as a message.
unsigned long vrpn_TimevalDuration(struct timeval endT, struct timeval startT)
Return number of microseconds between startT and endT.
void vrpn_SleepMsecs(double dMilliSecs)
#define vrpn_gettimeofday