52 #if (defined(_WIN32) || defined(_WIN64)) 56 #if !(defined(_WIN32) || defined(_WIN64)) 61 #include <sys/ioctl.h> 67 #if !(defined(_WIN32) || defined(_WIN64)) && !(defined(__APPLE__)) 68 #include <linux/serial.h> 71 #if (defined(__APPLE__)) 72 #include <IOKit/IOKitLib.h> 73 #include <IOKit/serial/IOSerialKeys.h> 74 #include <IOKit/serial/ioss.h> 75 #include <IOKit/IOBSD.h> 86 #if (defined(_WIN32) || defined(_WIN64)) 89 #define usleep(X) Sleep((X) / 1000) 90 #elif (defined(__APPLE__)) 97 #define BUFFER_SIZE 500 109 #define HEXDUMP_COLS 8 112 void hexdump(
void *mem,
unsigned int len)
116 for(i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++) {
118 if(i % HEXDUMP_COLS == 0) {
119 printf(
"0x%06x: ", i);
124 printf(
"%02x ", 0xFF & ((
char*)mem)[i]);
131 if(i % HEXDUMP_COLS == (HEXDUMP_COLS - 1)) {
132 for(j = i - (HEXDUMP_COLS - 1); j <= i; j++) {
136 }
else if(isprint(((
char*)mem)[j])) {
138 putchar(0xFF & ((
char*)mem)[j]);
156 #if (defined(_WIN32) || defined(_WIN64)) 160 char aux_string[255];
161 char aux_port_str[255];
165 for(i = 1; i < 20; ++i) {
166 strcpy(list_of_ports[i],
"");
167 sprintf(aux_string,
"\\\\.\\COM%d", i);
168 port = CreateFile(aux_string, GENERIC_WRITE|GENERIC_READ,
169 0, NULL, OPEN_EXISTING, 0, NULL);
172 sscanf(aux_string,
"\\\\.\\%s", aux_port_str);
173 strcpy(list_of_ports[h], aux_port_str);
185 struct dirent *directory_p;
188 directory = opendir(
"/dev");
190 while ( ( directory_p = readdir(directory) ) && i < 10 ) {
191 if (strstr(directory_p->d_name,
"tty.usbserial") || strstr(directory_p->d_name,
"ttyUSB")) {
192 strcpy(list_of_ports[i],
"/dev/" );
193 strcat(list_of_ports[i], directory_p->d_name);
198 (void) closedir(directory);
216 #if (defined(_WIN32) || defined(_WIN64)) 224 sprintf(my_port,
"\\\\.\\%s", port_s);
228 GENERIC_WRITE|GENERIC_READ,
229 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT, NULL);
237 dcb.DCBlength =
sizeof (DCB);
240 dcb.BaudRate = BAUD_RATE;
241 dcb.Parity = NOPARITY;
242 dcb.StopBits = ONESTOPBIT;
244 dcb.fOutxCtsFlow = FALSE;
245 dcb.fOutxDsrFlow = FALSE;
246 dcb.fDtrControl = FALSE;
248 dcb.fDsrSensitivity = FALSE;
249 dcb.fTXContinueOnXoff = FALSE;
252 dcb.fErrorChar = FALSE;
254 dcb.fRtsControl = RTS_CONTROL_DISABLE;
255 dcb.fAbortOnError = FALSE;
259 dcb.DCBlength =
sizeof(DCB);
266 GetCommTimeouts(comm_settings_t->
file_handle, &cts);
267 cts.ReadIntervalTimeout = 0;
269 cts.ReadTotalTimeoutMultiplier = 0;
270 cts.ReadTotalTimeoutConstant = 100;
272 cts.WriteTotalTimeoutConstant = 100;
273 cts.WriteTotalTimeoutMultiplier = 0;
274 SetCommTimeouts(comm_settings_t->
file_handle, &cts);
287 struct termios options;
289 #if (defined __APPLE__) 290 speed_t custom_baudrate = BAUD_RATE;
294 open(port_s, O_RDWR | O_NOCTTY | O_NONBLOCK);
301 if (ioctl(comm_settings_t->
file_handle, TIOCEXCL) == -1) {
307 if(fcntl(comm_settings_t->
file_handle, F_SETFL, 0) == -1) {
311 if (tcgetattr(comm_settings_t->
file_handle, &options) == -1) {
315 #if (defined __APPLE__) 318 if (BAUD_RATE > 460800){
321 options.c_cflag |= (CLOCAL | CREAD);
324 options.c_cflag &= ~PARENB;
326 options.c_cflag &= ~CSIZE;
327 options.c_cflag |= CS8;
330 options.c_cflag &= ~CRTSCTS;
331 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
332 options.c_oflag &= ~OPOST;
333 options.c_iflag &= ~(IXON | IXOFF | IXANY | INLCR);
335 options.c_cc[VMIN] = 0;
336 options.c_cc[VTIME] = 0;
341 cfsetispeed(&options, BAUD_RATE);
342 cfsetospeed(&options, BAUD_RATE);
345 options.c_cflag |= (CLOCAL | CREAD);
348 options.c_cflag &= ~PARENB;
350 options.c_cflag &= ~CSIZE;
351 options.c_cflag |= CS8;
354 options.c_cflag &= ~CRTSCTS;
355 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
356 options.c_oflag &= ~OPOST;
357 options.c_iflag &= ~(IXON | IXOFF | IXANY | INLCR);
359 options.c_cc[VMIN] = 0;
360 options.c_cc[VTIME] = 0;
367 cfsetispeed(&options, BAUD_RATE);
368 cfsetospeed(&options, BAUD_RATE);
372 options.c_cc[VMIN] = 0;
373 options.c_cc[VTIME] = 0;
375 struct serial_struct serinfo;
377 ioctl(comm_settings_t->
file_handle, TIOCGSERIAL, &serinfo);
378 serinfo.flags |= ASYNC_LOW_LATENCY;
379 ioctl(comm_settings_t->
file_handle, TIOCSSERIAL, &serinfo);
384 if (tcsetattr(comm_settings_t->
file_handle, TCSANOW, &options) == -1) {
388 #if (defined __APPLE__) 390 if(ioctl(comm_settings_t->
file_handle, IOSSIOSPEED, &custom_baudrate, 1)){
416 #if (defined(_WIN32) || defined(_WIN64)) 427 long timevaldiff(
struct timeval *starttime,
struct timeval *finishtime)
430 usec = (finishtime->tv_sec - starttime->tv_sec) * 1000000;
431 usec += (finishtime->tv_usec - starttime->tv_usec);
445 unsigned char data_in[
BUFFER_SIZE] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
446 unsigned int package_size = 6;
448 memcpy(package, data_in, package_size);
451 #if (defined(_WIN32) || defined(_WIN64)) 452 DWORD data_in_bytes = 0;
454 if (!ReadFile(comm_settings_t->
file_handle, data_in, 4, &data_in_bytes, NULL))
458 if ((
id != 0) && (data_in[2] !=
id)) {
462 package_size = data_in[3];
464 if (!ReadFile(comm_settings_t->
file_handle, data_in, package_size, &data_in_bytes, NULL))
470 struct timeval start, now;
472 gettimeofday(&start, NULL);
473 gettimeofday(&now, NULL);
475 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
478 gettimeofday(&now, NULL);
479 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
482 if (!read(comm_settings_t->
file_handle, data_in, 4)) {
488 if ((
id != 0) && (data_in[2] !=
id)) {
493 package_size = data_in[3];
495 gettimeofday(&start, NULL);
496 gettimeofday(&now, NULL);
498 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
501 gettimeofday(&now, NULL);
502 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
505 if (!read(comm_settings_t->
file_handle, data_in, package_size)) {
512 if (
checksum ( (
char *) data_in, package_size - 1) != (
char) data_in[package_size - 1]) {
517 printf(
"Received package size: %d \n", package_size);
520 memcpy(package, data_in, package_size);
536 #if (defined(_WIN32) || defined(_WIN64)) 537 DWORD package_size_out;
542 COMMTIMEOUTS cts_old, cts;
545 GetCommTimeouts(comm_settings_t->
file_handle, &cts_old);
547 memcpy(&cts, &cts_old,
sizeof(COMMTIMEOUTS));
549 cts.ReadIntervalTimeout = 0;
551 cts.ReadTotalTimeoutMultiplier = 0;
552 cts.ReadTotalTimeoutConstant = 5;
554 cts.WriteTotalTimeoutConstant = 5;
555 cts.WriteTotalTimeoutMultiplier = 0;
557 SetCommTimeouts(comm_settings_t->
file_handle, &cts);
561 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
563 read(comm_settings_t->
file_handle, package_in, n_bytes);
566 for(
id = 1;
id < 255; ++id) {
571 data_out[2] = (
unsigned char)
id;
577 #if (defined(_WIN32) || defined(_WIN64)) 578 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
580 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
582 read(comm_settings_t->
file_handle, package_in, n_bytes);
588 #if (defined(_WIN32) || defined(_WIN64)) 595 ReadFile( comm_settings_t->
file_handle, package_out, 1, &n_bytes_in, NULL);
596 aux_int |= n_bytes_in;
598 memcpy(package_in + z, package_out, n_bytes_in);
603 list_of_ids[h] = package_in[2];
610 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
613 read(comm_settings_t->
file_handle, package_in, n_bytes);
614 list_of_ids[h] = package_in[2];
621 #if (defined(_WIN32) || defined(_WIN64)) 622 SetCommTimeouts(comm_settings_t->
file_handle, &cts_old);
638 unsigned char auxstring[3];
640 #if (defined(_WIN32) || defined(_WIN64)) 646 const int size = 512;
649 char aux_buffer[size];
656 #if (defined(_WIN32) || defined(_WIN64)) 657 WriteFile(comm_settings_t->
file_handle, auxstring, 3, &n_bytes_out, NULL);
664 ReadFile(comm_settings_t->
file_handle, &aux, 1, &n_bytes_in, NULL);
675 if(ioctl(comm_settings_t->
file_handle, FIONREAD, &bytes) < 0)
683 read(comm_settings_t->
file_handle, aux_buffer, bytes);
685 strncpy(buffer + count, aux_buffer, bytes);
690 strcpy(buffer + count,
"\0");
705 #if (defined(_WIN32) || defined(_WIN64)) 706 DWORD package_size_out;
713 package_out[0] =
':';
714 package_out[1] =
':';
715 package_out[2] = (
unsigned char)
id;
721 #if (defined(_WIN32) || defined(_WIN64)) 722 WriteFile(comm_settings_t->
file_handle, package_out, 6, &package_size_out, NULL);
724 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
726 read(comm_settings_t->
file_handle, package_in, n_bytes);
728 write(comm_settings_t->
file_handle, package_out, 6);
731 package_in_size =
RS485read(comm_settings_t,
id, package_in);
732 if ((package_in_size < 0) || (package_in[1] !=
CMD_PING))
733 return package_in_size;
750 #if (defined(_WIN32) || defined(_WIN64)) 751 DWORD package_size_out;
759 data_out[2] = (
unsigned char)
id;
762 data_out[5] = activate ? 3 : 0;
763 data_out[6] =
checksum(data_out + 4, 2);
765 #if (defined(_WIN32) || defined(_WIN64)) 766 WriteFile(comm_settings_t->
file_handle, data_out, 7, &package_size_out, NULL);
768 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
770 read(comm_settings_t->
file_handle, package_in, n_bytes);
789 #if (defined(_WIN32) || defined(_WIN64)) 790 DWORD package_size_out;
798 data_out[2] = (
unsigned char)
id;
802 data_out[6] =
checksum(data_out + 4, 2);
804 #if (defined(_WIN32) || defined(_WIN64)) 805 WriteFile(comm_settings_t->
file_handle, data_out, 7, &package_size_out, NULL);
807 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
809 read(comm_settings_t->
file_handle, package_in, n_bytes);
827 #if (defined(_WIN32) || defined(_WIN64)) 828 DWORD package_size_out;
836 data_out[2] = (
unsigned char)
id;
839 data_out[5] = (wdt / 2);
840 data_out[6] =
checksum(data_out + 4, 2);
842 #if (defined(_WIN32) || defined(_WIN64)) 843 WriteFile(comm_settings_t->
file_handle, data_out, 7, &package_size_out, NULL);
845 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
847 read(comm_settings_t->
file_handle, package_in, n_bytes);
867 #if (defined(_WIN32) || defined(_WIN64)) 868 DWORD package_size_out;
878 data_out[2] = (
unsigned char)
id;
883 #if (defined(_WIN32) || defined(_WIN64)) 884 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
886 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
888 read(comm_settings_t->
file_handle, package_in, n_bytes);
894 package_in_size =
RS485read(comm_settings_t,
id, package_in);
895 if (package_in_size < 0)
896 return package_in_size;
900 *activate = package_in[1];
917 #if (defined(_WIN32) || defined(_WIN64)) 918 DWORD package_size_out;
926 data_out[2] = (
unsigned char)
id;
930 data_out[5] = ((
char *) &inputs[0])[1];
931 data_out[6] = ((
char *) &inputs[0])[0];
932 data_out[7] = ((
char *) &inputs[1])[1];
933 data_out[8] = ((
char *) &inputs[1])[0];
934 data_out[9] =
checksum(data_out + 4, 5);
936 #if (defined(_WIN32) || defined(_WIN64)) 937 WriteFile(comm_settings_t->
file_handle, data_out, 10, &package_size_out, NULL);
939 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
941 read(comm_settings_t->
file_handle, package_in, n_bytes);
946 package_in_size =
RS485read(comm_settings_t,
id, package_in);
947 if (package_in_size < 0)
948 return package_in_size;
964 #if (defined(_WIN32) || defined(_WIN64)) 965 DWORD package_size_out;
973 data_out[2] = (
unsigned char)
id;
977 data_out[5] = ((
char *) &inputs[0])[1];
978 data_out[6] = ((
char *) &inputs[0])[0];
979 data_out[7] = ((
char *) &inputs[1])[1];
980 data_out[8] = ((
char *) &inputs[1])[0];
981 data_out[9] =
checksum(data_out + 4, 5);
983 #if (defined(_WIN32) || defined(_WIN64)) 984 WriteFile(comm_settings_t->
file_handle, data_out, 10, &package_size_out, NULL);
986 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
988 read(comm_settings_t->
file_handle, package_in, n_bytes);
1006 #if (defined(_WIN32) || defined(_WIN64)) 1007 DWORD package_size_out;
1016 data_out[2] = (
unsigned char)
id;
1020 data_out[5] = ((
char *) &inputs[0])[1];
1021 data_out[6] = ((
char *) &inputs[0])[0];
1022 data_out[7] = ((
char *) &inputs[1])[1];
1023 data_out[8] = ((
char *) &inputs[1])[0];
1024 data_out[9] =
checksum(data_out + 4, 5);
1026 #if (defined(_WIN32) || defined(_WIN64)) 1027 WriteFile(comm_settings_t->
file_handle, data_out, 10, &package_size_out, NULL);
1029 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
1031 read(comm_settings_t->
file_handle, package_in, n_bytes);
1033 write(comm_settings_t->
file_handle, data_out, 10);
1048 int package_in_size;
1051 #if (defined(_WIN32) || defined(_WIN64)) 1052 DWORD package_size_out;
1061 data_out[2] = (
unsigned char)
id;
1066 #if (defined(_WIN32) || defined(_WIN64)) 1067 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
1069 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
1071 read(comm_settings_t->
file_handle, package_in, n_bytes);
1078 package_in_size =
RS485read(comm_settings_t,
id, package_in);
1079 if (package_in_size < 0)
1080 return package_in_size;
1084 ((
char *) &inputs[0])[0] = package_in[2];
1085 ((
char *) &inputs[0])[1] = package_in[1];
1087 ((
char *) &inputs[1])[0] = package_in[4];
1088 ((
char *) &inputs[1])[1] = package_in[3];
1103 int package_in_size;
1105 #if (defined(_WIN32) || defined(_WIN64)) 1106 DWORD package_size_out;
1115 data_out[2] = (
unsigned char)
id;
1120 #if (defined(_WIN32) || defined(_WIN64)) 1121 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
1123 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
1125 read(comm_settings_t->
file_handle, package_in, n_bytes);
1130 package_in_size =
RS485read(comm_settings_t,
id, package_in);
1131 if (package_in_size < 0)
1132 return package_in_size;
1136 switch ((package_in_size - 2) >> 1) {
1138 ((
char *) &measurements[0])[0] = package_in[2];
1139 ((
char *) &measurements[0])[1] = package_in[1];
1142 ((
char *) &measurements[0])[0] = package_in[2];
1143 ((
char *) &measurements[0])[1] = package_in[1];
1144 ((
char *) &measurements[1])[0] = package_in[4];
1145 ((
char *) &measurements[1])[1] = package_in[3];
1148 ((
char *) &measurements[0])[0] = package_in[2];
1149 ((
char *) &measurements[0])[1] = package_in[1];
1150 ((
char *) &measurements[1])[0] = package_in[4];
1151 ((
char *) &measurements[1])[1] = package_in[3];
1152 ((
char *) &measurements[2])[0] = package_in[6];
1153 ((
char *) &measurements[2])[1] = package_in[5];
1156 ((int8_t *) &measurements[0])[0] = package_in[2];
1157 ((int8_t *) &measurements[0])[1] = package_in[1];
1158 ((int8_t *) &measurements[1])[0] = package_in[4];
1159 ((int8_t *) &measurements[1])[1] = package_in[3];
1160 ((int8_t *) &measurements[2])[0] = package_in[6];
1161 ((int8_t *) &measurements[2])[1] = package_in[5];
1162 ((int8_t *) &measurements[3])[0] = package_in[8];
1163 ((int8_t *) &measurements[3])[1] = package_in[7];
1166 printf(
"Number of sensors not supported.\n");
1171 return ((package_in_size - 2) >> 1);
1184 int package_in_size;
1186 #if (defined(_WIN32) || defined(_WIN64)) 1187 DWORD package_size_out;
1196 data_out[2] = (
unsigned char)
id;
1201 #if (defined(_WIN32) || defined(_WIN64)) 1202 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
1204 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
1206 read(comm_settings_t->
file_handle, package_in, n_bytes);
1211 package_in_size =
RS485read(comm_settings_t,
id, package_in);
1212 if (package_in_size < 0)
1213 return package_in_size;
1217 ((
char *) &counters[0])[0] = package_in[2];
1218 ((
char *) &counters[0])[1] = package_in[1];
1219 ((
char *) &counters[1])[0] = package_in[4];
1220 ((
char *) &counters[1])[1] = package_in[3];
1221 ((
char *) &counters[2])[0] = package_in[6];
1222 ((
char *) &counters[2])[1] = package_in[5];
1223 ((
char *) &counters[3])[0] = package_in[8];
1224 ((
char *) &counters[3])[1] = package_in[7];
1226 ((
char *) &counters[4])[0] = package_in[10];
1227 ((
char *) &counters[4])[1] = package_in[9];
1228 ((
char *) &counters[5])[0] = package_in[12];
1229 ((
char *) &counters[5])[1] = package_in[11];
1230 ((
char *) &counters[6])[0] = package_in[14];
1231 ((
char *) &counters[6])[1] = package_in[13];
1232 ((
char *) &counters[7])[0] = package_in[16];
1233 ((
char *) &counters[7])[1] = package_in[15];
1235 ((
char *) &counters[8])[0] = package_in[18];
1236 ((
char *) &counters[8])[1] = package_in[17];
1237 ((
char *) &counters[9])[0] = package_in[20];
1238 ((
char *) &counters[9])[1] = package_in[19];
1239 ((
char *) &counters[10])[0] = package_in[22];
1240 ((
char *) &counters[10])[1] = package_in[21];
1241 ((
char *) &counters[11])[0] = package_in[24];
1242 ((
char *) &counters[11])[1] = package_in[23];
1244 ((
char *) &counters[12])[0] = package_in[26];
1245 ((
char *) &counters[12])[1] = package_in[25];
1246 ((
char *) &counters[13])[0] = package_in[28];
1247 ((
char *) &counters[13])[1] = package_in[27];
1248 ((
char *) &counters[14])[0] = package_in[30];
1249 ((
char *) &counters[14])[1] = package_in[29];
1250 ((
char *) &counters[15])[0] = package_in[32];
1251 ((
char *) &counters[15])[1] = package_in[31];
1253 ((
char *) &counters[16])[0] = package_in[34];
1254 ((
char *) &counters[16])[1] = package_in[33];
1255 ((
char *) &counters[17])[0] = package_in[36];
1256 ((
char *) &counters[17])[1] = package_in[35];
1257 ((
char *) &counters[18])[0] = package_in[38];
1258 ((
char *) &counters[18])[1] = package_in[37];
1259 ((
char *) &counters[19])[0] = package_in[40];
1260 ((
char *) &counters[19])[1] = package_in[39];
1275 int package_in_size;
1277 #if (defined(_WIN32) || defined(_WIN64)) 1278 DWORD package_size_out;
1287 data_out[2] = (
unsigned char)
id;
1292 #if (defined(_WIN32) || defined(_WIN64)) 1293 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
1295 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
1297 read(comm_settings_t->
file_handle, package_in, n_bytes);
1303 package_in_size =
RS485read(comm_settings_t,
id, package_in);
1304 if (package_in_size < 0)
1305 return package_in_size;
1308 ((
char *) ¤ts[0])[0] = package_in[2];
1309 ((
char *) ¤ts[0])[1] = package_in[1];
1311 ((
char *) ¤ts[1])[0] = package_in[4];
1312 ((
char *) ¤ts[1])[1] = package_in[3];
1328 int package_in_size;
1330 #if (defined(_WIN32) || defined(_WIN64)) 1331 DWORD package_size_out;
1340 data_out[2] = (
unsigned char)
id;
1345 #if (defined(_WIN32) || defined(_WIN64)) 1346 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
1348 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
1350 read(comm_settings_t->
file_handle, package_in, n_bytes);
1356 package_in_size =
RS485read(comm_settings_t,
id, package_in);
1357 if (package_in_size < 0)
1358 return package_in_size;
1362 ((
char *) &emg[0])[0] = package_in[2];
1363 ((
char *) &emg[0])[1] = package_in[1];
1365 ((
char *) &emg[1])[0] = package_in[4];
1366 ((
char *) &emg[1])[1] = package_in[3];
1379 short int *values) {
1383 int package_in_size;
1385 #if (defined(_WIN32) || defined(_WIN64)) 1386 DWORD package_size_out;
1395 data_out[2] = (
unsigned char)
id;
1401 #if (defined(_WIN32) || defined(_WIN64)) 1402 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
1404 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
1406 read(comm_settings_t->
file_handle, package_in, n_bytes);
1411 package_in_size =
RS485read(comm_settings_t,
id, package_in);
1412 if (package_in_size < 0)
1413 return package_in_size;
1418 ((
char *) &values[0])[0] = package_in[2];
1419 ((
char *) &values[0])[1] = package_in[1];
1421 ((
char *) &values[1])[0] = package_in[4];
1422 ((
char *) &values[1])[1] = package_in[3];
1425 ((
char *) &values[2])[0] = package_in[6];
1426 ((
char *) &values[2])[1] = package_in[5];
1428 ((
char *) &values[3])[0] = package_in[8];
1429 ((
char *) &values[3])[1] = package_in[7];
1431 ((
char *) &values[4])[0] = package_in[10];
1432 ((
char *) &values[4])[1] = package_in[9];
1447 int package_in_size;
1449 #if (defined(_WIN32) || defined(_WIN64)) 1450 DWORD package_size_out;
1459 data_out[2] = (
unsigned char)
id;
1464 #if (defined(_WIN32) || defined(_WIN64)) 1465 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
1467 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
1469 read(comm_settings_t->
file_handle, package_in, n_bytes);
1474 package_in_size =
RS485read(comm_settings_t,
id, package_in);
1475 if (package_in_size < 0)
1476 return package_in_size;
1480 switch ((package_in_size - 2) >> 1) {
1482 ((
char *) &measurements[0])[0] = package_in[2];
1483 ((
char *) &measurements[0])[1] = package_in[1];
1486 ((
char *) &measurements[0])[0] = package_in[2];
1487 ((
char *) &measurements[0])[1] = package_in[1];
1488 ((
char *) &measurements[1])[0] = package_in[4];
1489 ((
char *) &measurements[1])[1] = package_in[3];
1492 ((
char *) &measurements[0])[0] = package_in[2];
1493 ((
char *) &measurements[0])[1] = package_in[1];
1494 ((
char *) &measurements[1])[0] = package_in[4];
1495 ((
char *) &measurements[1])[1] = package_in[3];
1496 ((
char *) &measurements[2])[0] = package_in[6];
1497 ((
char *) &measurements[2])[1] = package_in[5];
1500 ((
char *) &measurements[0])[0] = package_in[2];
1501 ((
char *) &measurements[0])[1] = package_in[1];
1502 ((
char *) &measurements[1])[0] = package_in[4];
1503 ((
char *) &measurements[1])[1] = package_in[3];
1504 ((
char *) &measurements[2])[0] = package_in[6];
1505 ((
char *) &measurements[2])[1] = package_in[5];
1506 ((
char *) &measurements[3])[0] = package_in[8];
1507 ((
char *) &measurements[3])[1] = package_in[7];
1510 printf(
"Number of sensors not supported.\n");
1515 return ((package_in_size - 2) >> 1);
1529 int package_in_size;
1531 #if (defined(_WIN32) || defined(_WIN64)) 1532 DWORD package_size_out;
1541 data_out[2] = (
unsigned char)
id;
1546 #if (defined(_WIN32) || defined(_WIN64)) 1547 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
1549 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
1551 read(comm_settings_t->
file_handle, package_in, n_bytes);
1556 package_in_size =
RS485read(comm_settings_t,
id, package_in);
1557 if (package_in_size < 0)
1558 return package_in_size;
1562 switch ((package_in_size - 2) >> 1) {
1564 ((
char *) &measurements[0])[0] = package_in[2];
1565 ((
char *) &measurements[0])[1] = package_in[1];
1568 ((
char *) &measurements[0])[0] = package_in[2];
1569 ((
char *) &measurements[0])[1] = package_in[1];
1570 ((
char *) &measurements[1])[0] = package_in[4];
1571 ((
char *) &measurements[1])[1] = package_in[3];
1574 ((
char *) &measurements[0])[0] = package_in[2];
1575 ((
char *) &measurements[0])[1] = package_in[1];
1576 ((
char *) &measurements[1])[0] = package_in[4];
1577 ((
char *) &measurements[1])[1] = package_in[3];
1578 ((
char *) &measurements[2])[0] = package_in[6];
1579 ((
char *) &measurements[2])[1] = package_in[5];
1582 ((
char *) &measurements[0])[0] = package_in[2];
1583 ((
char *) &measurements[0])[1] = package_in[1];
1584 ((
char *) &measurements[1])[0] = package_in[4];
1585 ((
char *) &measurements[1])[1] = package_in[3];
1586 ((
char *) &measurements[2])[0] = package_in[6];
1587 ((
char *) &measurements[2])[1] = package_in[5];
1588 ((
char *) &measurements[3])[0] = package_in[8];
1589 ((
char *) &measurements[3])[1] = package_in[7];
1592 printf(
"Number of sensors not supported.\n");
1597 return ((package_in_size - 2) >> 1);
1611 #if (defined(_WIN32) || defined(_WIN64)) 1612 DWORD package_size_out;
1613 DWORD n_bytes_in = 0;
1619 const int size = 512;
1620 char aux_buffer[size];
1629 data_out[2] = (
unsigned char)
id;
1632 data_out[5] = ((
unsigned char *) &info_type)[1];
1633 data_out[6] = ((
unsigned char *) &info_type)[0];
1634 data_out[7] =
checksum(data_out + 4, 3);
1637 #if (defined(_WIN32) || defined(_WIN64)) 1638 WriteFile(comm_settings_t->
file_handle, data_out, 8, &package_size_out, NULL);
1645 ReadFile(comm_settings_t->
file_handle, &aux, 1, &n_bytes_in, NULL);
1660 if(ioctl(comm_settings_t->
file_handle, FIONREAD, &bytes) < 0)
1669 read(comm_settings_t->
file_handle, aux_buffer, bytes);
1671 strncpy(buffer + count, aux_buffer, bytes);
1676 strcpy(buffer + count,
"\0");
1695 int package_in_size;
1697 #if (defined(_WIN32) || defined(_WIN64)) 1698 DWORD package_size_out;
1705 data_out[2] = (
unsigned char)
id;
1711 #if (defined(_WIN32) || defined(_WIN64)) 1712 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
1714 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
1716 read(comm_settings_t->
file_handle, package_in, n_bytes);
1721 package_in_size =
RS485read(comm_settings_t,
id, package_in);
1722 if (package_in_size < 0)
1723 return package_in_size;
1740 int package_in_size;
1742 #if (defined(_WIN32) || defined(_WIN64)) 1743 DWORD package_size_out;
1750 data_out[2] = (
unsigned char)
id;
1755 #if (defined(_WIN32) || defined(_WIN64)) 1756 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
1758 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
1760 read(comm_settings_t->
file_handle, package_in, n_bytes);
1765 package_in_size =
RS485read(comm_settings_t,
id, package_in);
1766 if (package_in_size < 0)
1767 return package_in_size;
1783 int package_in_size;
1785 #if (defined(_WIN32) || defined(_WIN64)) 1786 DWORD package_size_out;
1793 data_out[2] = (
unsigned char)
id;
1796 data_out[5] = ((
char *) &speed)[1];
1797 data_out[6] = ((
char *) &speed)[0];
1798 data_out[7] = ((
char *) &repetitions)[1];
1799 data_out[8] = ((
char *) &repetitions)[0];
1800 data_out[9] =
checksum(data_out + 4, 5);
1802 #if (defined(_WIN32) || defined(_WIN64)) 1803 WriteFile(comm_settings_t->
file_handle, data_out, 10, &package_size_out, NULL);
1805 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
1807 read(comm_settings_t->
file_handle, package_in, n_bytes);
1809 write(comm_settings_t->
file_handle, data_out, 10);
1812 package_in_size =
RS485read(comm_settings_t,
id, package_in);
1813 if (package_in_size < 0)
1814 return package_in_size;
1828 unsigned short num_of_values ) {
1832 int package_in_size;
1833 unsigned short int value_size, i, h;
1836 #if (defined(_WIN32) || defined(_WIN64)) 1837 DWORD package_size_out;
1842 value = (
unsigned int *) values;
1847 data_out[2] = (
unsigned char)
id;
1848 data_out[3] = 2 + num_of_values * value_size;
1852 for(h = 0; h < num_of_values; ++h) {
1853 for(i = 0; i < value_size; ++i) {
1854 data_out[ h * value_size + 5 + i ] =
1855 ((
char *) value)[ h * value_size + value_size - i - 1 ];
1859 data_out[ 5 + num_of_values * value_size ] =
1860 checksum( data_out + 4, 1 + num_of_values * value_size );
1863 #if (defined(_WIN32) || defined(_WIN64)) 1864 WriteFile(comm_settings_t->
file_handle, data_out, 6 + num_of_values * value_size, &package_size_out, NULL);
1866 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
1868 read(comm_settings_t->
file_handle, package_in, n_bytes);
1870 write(comm_settings_t->
file_handle, data_out, 6 + num_of_values * value_size);
1873 package_in_size =
RS485read(comm_settings_t,
id, package_in);
1875 if ( (package_in_size < 0) || (package_in[0] ==
ACK_ERROR) ) {
1876 return package_in_size;
1879 if (package_in[0] ==
ACK_OK) {
1893 void *values,
unsigned short value_size,
unsigned short num_of_values,
1894 uint8_t *param_buffer) {
1898 int package_in_size;
1900 #if (defined(_WIN32) || defined(_WIN64)) 1901 DWORD package_size_out;
1902 DWORD n_bytes_in = 0;
1908 const int size = 512;
1909 int8_t aux_buffer[size];
1916 data_out[2] = (
unsigned char)
id;
1917 data_out[3] = 4 + num_of_values * value_size;
1920 data_out[5] = ((
char *) &index)[1];
1921 data_out[6] = ((
char *) &index)[0];
1923 for(
int h = 0; h < num_of_values; ++h) {
1924 for(
int i = 0; i < value_size; ++i) {
1925 data_out[ h * value_size + 7 + i ] =
1926 ((
char *) values)[ h * value_size + value_size - i - 1 ];
1930 data_out[ 7 + num_of_values * value_size ] =
1931 checksum( data_out + 4, 3 + num_of_values * value_size );
1936 #if (defined(_WIN32) || defined(_WIN64)) 1937 WriteFile(comm_settings_t->
file_handle, data_out, 8, &package_size_out, NULL);
1944 ReadFile(comm_settings_t->
file_handle, &aux, 1, &n_bytes_in, NULL);
1946 param_buffer[i] = aux;
1957 if(ioctl(comm_settings_t->
file_handle, FIONREAD, &bytes) < 0) {
1966 read(comm_settings_t->
file_handle, aux_buffer, bytes);
1968 memcpy(param_buffer + count, aux_buffer, bytes);
1978 #if (defined(_WIN32) || defined(_WIN64)) 1979 WriteFile(comm_settings_t->
file_handle, data_out, 8 + num_of_values * value_size, &package_size_out, NULL);
1981 ioctl(comm_settings_t->
file_handle, FIONREAD, &bytes);
1983 read(comm_settings_t->
file_handle, package_in, bytes);
1985 write(comm_settings_t->
file_handle, data_out, 8 + num_of_values * value_size);
1988 package_in_size =
RS485read(comm_settings_t,
id, package_in);
1990 if ( (package_in_size < 0) || (package_in[0] ==
ACK_ERROR) ) {
1991 return package_in_size;
1994 if (package_in[0] ==
ACK_OK) {
2012 int package_in_size;
2014 #if (defined(_WIN32) || defined(_WIN64)) 2015 DWORD package_size_out;
2022 data_out[2] = (
unsigned char)
id;
2027 #if (defined(_WIN32) || defined(_WIN64)) 2028 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
2030 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
2032 read(comm_settings_t->
file_handle, package_in, n_bytes);
2038 package_in_size =
RS485read(comm_settings_t,
id, package_in);
2040 if ( (package_in_size < 0) || (package_in[0] ==
ACK_ERROR) ) {
2041 printf(
"package_in_size: %d\n", package_in_size);
2042 return package_in_size;
2045 if (package_in[0] ==
ACK_OK) {
2060 int package_in_size;
2062 #if (defined(_WIN32) || defined(_WIN64)) 2063 DWORD package_size_out;
2070 data_out[2] = (
unsigned char)
id;
2075 #if (defined(_WIN32) || defined(_WIN64)) 2076 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
2078 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
2080 read(comm_settings_t->
file_handle, package_in, n_bytes);
2086 package_in_size =
RS485read(comm_settings_t,
id, package_in);
2088 if (package_in_size < 0) {
2089 return package_in_size;
2103 int package_in_size;
2105 #if (defined(_WIN32) || defined(_WIN64)) 2106 DWORD package_size_out;
2113 data_out[2] = (
unsigned char)
id;
2119 #if (defined(_WIN32) || defined(_WIN64)) 2120 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
2122 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
2124 read(comm_settings_t->
file_handle, package_in, n_bytes);
2130 package_in_size =
RS485read(comm_settings_t,
id, package_in);
2132 if (package_in_size < 0) {
2133 return package_in_size;
2147 int package_in_size;
2149 #if (defined(_WIN32) || defined(_WIN64)) 2150 DWORD package_size_out;
2157 data_out[2] = (
unsigned char)
id;
2163 #if (defined(_WIN32) || defined(_WIN64)) 2164 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
2166 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
2168 read(comm_settings_t->
file_handle, package_in, n_bytes);
2174 package_in_size =
RS485read(comm_settings_t,
id, package_in);
2176 if (package_in_size < 0) {
2177 return package_in_size;
2193 int package_in_size;
2195 #if (defined(_WIN32) || defined(_WIN64)) 2196 DWORD package_size_out;
2204 data_out[2] = (
unsigned char)
id;
2207 data_out[5] = ext_input ? 3 : 0;
2208 data_out[6] =
checksum(data_out + 4, 2);
2210 #if (defined(_WIN32) || defined(_WIN64)) 2211 WriteFile(comm_settings_t->
file_handle, data_out, 7, &package_size_out, NULL);
2213 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
2216 read(comm_settings_t->
file_handle, package_in, n_bytes);
2222 package_in_size =
RS485read(comm_settings_t,
id, package_in);
2223 if (package_in_size < 0)
2224 return package_in_size;
2240 #if (defined(_WIN32) || defined(_WIN64)) 2241 DWORD package_size_out;
2250 data_out[2] = (
unsigned char)
id;
2254 data_out[5] = flag ? 1 : 0;
2255 data_out[6] =
checksum(data_out + 4, 2);
2257 #if (defined(_WIN32) || defined(_WIN64)) 2258 WriteFile(comm_settings_t->
file_handle, data_out, 7, &package_size_out, NULL);
2260 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
2262 read(comm_settings_t->
file_handle, package_in, n_bytes);
2279 int package_in_size;
2281 #if (defined(_WIN32) || defined(_WIN64)) 2282 DWORD package_size_out;
2291 data_out[2] = (
unsigned char)
id;
2296 #if (defined(_WIN32) || defined(_WIN64)) 2297 WriteFile(comm_settings_t->
file_handle, data_out, 6, &package_size_out, NULL);
2299 ioctl(comm_settings_t->
file_handle, FIONREAD, &n_bytes);
2301 read(comm_settings_t->
file_handle, package_in, n_bytes);
2306 package_in_size =
RS485read(comm_settings_t,
id, package_in);
2307 if (package_in_size < 0)
2308 return package_in_size;
2312 ((
char *) &joystick[0])[0] = package_in[2];
2313 ((
char *) &joystick[0])[1] = package_in[1];
2315 ((
char *) &joystick[1])[0] = package_in[4];
2316 ((
char *) &joystick[1])[1] = package_in[3];
2329 char checksum (
char * data_buffer,
int data_length ) {
2334 for(i = 0; i < data_length; ++i) {
2335 checksum = checksum ^ data_buffer[i];
int commGetCurrents(comm_settings *comm_settings_t, int id, short int currents[2])
This function gets currents from a qbMove or a qbHand connected to the serial port.
int commSetZeros(comm_settings *comm_settings_t, int id, void *values, unsigned short num_of_values)
This function sets the encoders's zero positon value that remains stored in the qbMove or qbHand memo...
void openRS485(comm_settings *comm_settings_t, const char *port_s, int BAUD_RATE=B2000000)
This function is used to open a serial port for using with the qbMove or the qbHand.
Command for asking device's current measurements.
Starts a series of opening and closures of the hand.
Command for setting reference inputs.
int RS485ListDevices(comm_settings *comm_settings_t, char list_of_ids[255])
This function is used to list the number of devices connected to the serial port and get their relati...
int commPing(comm_settings *comm_settings_t, int id)
This function is used to ping the qbMove or the qbHand.
void closeRS485(comm_settings *comm_settings_t)
This function is used to close a serial port being used with the qbMove or an qbHand.
Command to set the actual measurements as inputs to another device (Only for Armslider device) ...
char checksum(char *data_buffer, int data_length)
This functions returns an 8 bit LCR checksum over the lenght of a buffer.
Starts the stiffness calibration of the qbMove.
Command to set the device inputs and return an acknowledgment signal (needed for less comm...
Command to get the joystick measurements (Only for devices driven by a joystick)
int commGetParamList(comm_settings *comm_settings_t, int id, unsigned short index, void *values, unsigned short value_size, unsigned short num_of_values, uint8_t *buffer)
This function gets all the parameters that are stored in the qbMove or qbHand memory and sets one of ...
int commGetInfo(comm_settings *comm_settings_t, int id, short int info_type, char *info)
This function is used to ping the qbMove or the qbHand and get information about the device...
Command for asking device's emg sensors measurements.
int commGetCurrAndMeas(comm_settings *comm_settings_t, int id, short int *values)
This function gets currents and position measurements from a qbMove or a qbHand connected to the seri...
void commSetWatchDog(comm_settings *comm_settings_t, int id, short int wdt)
This function sets watchdog timer of a qbMove or a qbHand.
Store current parameters as factory parameters.
int commGetVelocities(comm_settings *comm_settings_t, int id, short int measurements[])
This function gets velocities of the two motors and the shaft from a qbMove connected to a serial por...
int commGetEmg(comm_settings *comm_settings_t, int id, short int emg[2])
This function gets measurements from electomyographics sensors connected to the qbHand.
Restore default factory parameters.
int commStoreDefaultParams(comm_settings *comm_settings_t, int id)
This function stores the factory default parameters.
Command for activating/deactivating the device.
void commSetCuffInputs(comm_settings *comm_settings_t, int id, int flag)
This function send reference inputs to a qbMove board connected to the serial port.
int commBootloader(comm_settings *comm_settings_t, int id)
This function sends the board in bootloader modality in order to update the firmware on the board...
int commStoreParams(comm_settings *comm_settings_t, int id)
This function stores all parameters that were set in the qbMove or the qbHand memory.
Command used to set Cuff device inputs (Only for Cuff device)
void commSetInputs(comm_settings *comm_settings_t, int id, short int inputs[])
This function send reference inputs to a qbMove or a qbHand connected to the serial port...
int commExtDrive(comm_settings *comm_settings_t, int id, char ext_input)
This function is used with the armslider device.
int commRestoreParams(comm_settings *comm_settings_t, int id)
This function restores the factory default parameters.
Library of functions for SERIAL PORT communication with a qbMove.
Command for setting baudrate of communication.
Command for asking device's measurements and currents.
int commCalibrate(comm_settings *comm_settings_t, int id)
This function is used to calibrate the maximum stiffness value of the qbMove.
Command for getting device activation state.
Definitions for qbMove or qbHand commands, parameters and packages.
void commSetBaudRate(comm_settings *comm_settings_t, int id, short int baudrate)
This function sets the baudrate of communication.
int commInitMem(comm_settings *comm_settings_t, int id)
This function initialize the EEPROM memory of the board by loading the default factory parameters...
int commGetCounters(comm_settings *comm_settings_t, int id, short unsigned int counters[20])
This function gets counters values from a qbMove connected to the serial port.
void commSetPosStiff(comm_settings *comm_settings_t, int id, short int inputs[])
This function send reference inputs to a qbMove connected to the serial port.
Stores all parameters in memory and loads them.
Not used in the softhand firmware.
#define BAUD_RATE_T_2000000
int RS485listPorts(char list_of_ports[10][255])
This function is used to return a list of available serial ports.
Command for setting the encoders zero position.
Initialize the memory with the defalut values.
Command for getting reference inputs.
int commGetActivate(comm_settings *comm_settings_t, int id, char *activate)
This function gets the activation status of a qbMove or a qbHand connected to the serial port...
Command for asking device's velocity measurements.
int RS485read(comm_settings *comm_settings_t, int id, char *package)
This function is used to read a package from the device.
Sets the bootloader modality to update the firmware.
Command for asking device's position measurements.
Asks for a string of information about.
int commGetAccelerations(comm_settings *comm_settings_t, int id, short int measurements[])
This function gets the acceleration of the qbHand motor.
Command for asking device's acceleration measurements.
Command to get the parameters list or to set a defined value chosen by the use.
int commGetJoystick(comm_settings *comm_settings_t, int id, short int joystick[2])
This function gets joystick measurementes from a softhand connected to the serial port...
long timevaldiff(struct timeval *starttime, struct timeval *finishtime)
This functions returns a difference between two timeval structures in order to obtain time elapsed be...
void RS485GetInfo(comm_settings *comm_settings_t, char *buffer)
This function is used to ping the serial port for a qbMove or a qbHand and to get information about t...
#define BUFFER_SIZE
Size of buffers that store communication packets.
int commGetMeasurements(comm_settings *comm_settings_t, int id, short int measurements[3])
This function gets position measurements from a qbMove or a qbHand connected to the serial port...
int commSetInputsAck(comm_settings *comm_settings_t, int id, short int inputs[])
This function send reference inputs to a qbMove or a qbHand connected to the serial port and expects ...
Command for asking device's counters (mostly used for debugging sent commands)
int commHandCalibrate(comm_settings *comm_settings_t, int id, short int speed, short int repetitions)
This function is used to make a series of opening and closures of the qbHand.
Command for setting watchdog timer or disable it.
int commGetInputs(comm_settings *comm_settings_t, int id, short int inputs[2])
This function gets input references from a qbMove or a qbHand connected to the serial port...
#define INVALID_HANDLE_VALUE
void commActivate(comm_settings *comm_settings_t, int id, char activate)
This function activates or deactivates a qbMove or a qbHand connected to the serial port...