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));
int vrpn_write_characters(int comm, const unsigned char *buffer, size_t bytes)
Write the buffer to the serial port.
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...
void vrpn_SleepMsecs(double dMsecs)
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.
vrpn_int32 request_channels_m_id
const vrpn_uint32 vrpn_CONNECTION_RELIABLE
Classes of service for messages, specify multiple by ORing them together Priority of satisfying these...
int vrpn_flush_input_buffer(int comm)
Throw out any characters within the input buffer.
Header containing macros formerly duplicated in a lot of implementation files.
vrpn_Serial: Pulls all the serial port routines into one file to make porting to new operating system...
vrpn_float64 o_channel[vrpn_CHANNEL_MAX]
static int VRPN_CALLBACK handle_connect_message(void *userdata, vrpn_HANDLERPARAM p)
Responds to a connection request with a report of the values.
vrpn_int32 d_ping_message_id
Ask the server if they are there.
Generic connection class not specific to the transport mechanism.
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 ...
#define VRPN_MSG_WARNING(msg)
All types of client/server/peer objects in VRPN should be derived from the vrpn_BaseClass type descri...
int vrpn_read_available_characters(int comm, unsigned char *buffer, size_t bytes)
static int VRPN_CALLBACK handle_request_message(void *userdata, vrpn_HANDLERPARAM p)
Responds to a request to change one of the values by setting the channel to that value.
int register_autodeleted_handler(vrpn_int32 type, vrpn_MESSAGEHANDLER handler, void *userdata, vrpn_int32 sender=vrpn_ANY_SENDER)
Registers a handler with the connection, and remembers to delete at destruction.
vrpn_Connection * d_connection
Connection that this object talks to.
This structure is what is passed to a vrpn_Connection message callback.
static int VRPN_CALLBACK handle_request_channels_message(void *userdata, vrpn_HANDLERPARAM p)
Responds to a request to change multiple channels at once.
#define vrpn_gettimeofday
vrpn_int32 d_sender_id
Sender ID registered with the connection.
unsigned long vrpn_TimevalDuration(struct timeval endT, struct timeval startT)
Return number of microseconds between startT and endT.
#define VRPN_MSG_ERROR(msg)