51 #elif defined(_WIN32)||defined(WIN32) 65 #define REGISTER_SELECT_OUTPUT 410 66 #define REGISTER_SERIAL_NUMBER 510 67 #define REGISTER_PRODUCTION_YEAR 514 68 #define REGISTER_FIRMWARE_VERSION 500 69 #define RQ_COM_MAX_STR_LENGTH 20 70 #define RQ_COM_JAM_SIGNAL_CHAR 0xff 71 #define RQ_COM_JAM_SIGNAL_LENGTH 50 72 #define RQ_COM_TIMER_FOR_STREAM_DETECTION_MAX_VALUE 20 //20 * 4ms = 80ms without bytes means that the stream is stopped 73 #define RQ_COM_TIMER_FOR_VALID_STREAM_MAX_VALUE 40 //40 * 4ms = 160ms without any valid message means that the communication is not working well 125 #ifdef __unix__ //For Unix 126 static INT_32 fd_connexion = -1;
127 static INT_8 set_com_attribs (
INT_32 fd, speed_t speed);
128 #elif defined(_WIN32)||defined(WIN32) //For Windows 147 if (device_found == 0)
156 #ifdef __unix__ //For Unix 159 struct dirent *entrydirectory = NULL;
163 if ((dir = opendir(
"/sys/class/tty/")) == NULL)
169 while ((entrydirectory = readdir(dir)) != NULL && device_found == 0)
172 if (strstr(entrydirectory->d_name,
"ttyS") ||
173 strstr(entrydirectory->d_name,
"ttyUSB"))
181 if (device_found == 0)
185 #elif defined(_WIN32)||defined(WIN32) //For Windows 189 for(i = 0;i < 256; i++){
190 sprintf(port,
"\\\\.\\COM%d",i);
191 hSerial = CreateFileA(port, GENERIC_READ | GENERIC_WRITE,
192 0,NULL,OPEN_EXISTING,0,NULL);
193 if(hSerial != INVALID_HANDLE_VALUE){
194 dcb.DCBlength =
sizeof(DCB);
195 if (!GetCommState(hSerial, &dcb)){
196 CloseHandle(hSerial);
197 hSerial = INVALID_HANDLE_VALUE;
200 dcb.BaudRate = CBR_19200;
202 dcb.StopBits = ONESTOPBIT;
203 dcb.Parity = NOPARITY;
207 dcb.fTXContinueOnXoff = TRUE;
216 dcb.fAbortOnError = FALSE;
219 if(!SetCommState(hSerial, &dcb)){
220 CloseHandle(hSerial);
223 COMMTIMEOUTS timeouts={0};
224 timeouts.ReadIntervalTimeout=0;
225 timeouts.ReadTotalTimeoutConstant=1;
226 timeouts.ReadTotalTimeoutMultiplier=0;
227 timeouts.WriteTotalTimeoutConstant=1;
228 timeouts.WriteTotalTimeoutMultiplier=0;
229 if(!SetCommTimeouts(hSerial, &timeouts)){
230 CloseHandle(hSerial);
231 hSerial = INVALID_HANDLE_VALUE;
237 CloseHandle(hSerial);
277 if (firmware_version[0] >> 8 ==
'F')
293 #ifdef __unix__ //For Unix 294 static INT_8 set_com_attribs (
INT_32 fd, speed_t speed)
297 memset (&tty, 0,
sizeof (
struct termios));
299 if (tcgetattr (fd, &tty) != 0)
304 cfsetospeed (&tty, speed);
305 cfsetispeed (&tty, speed);
307 tty.c_cflag |= (CLOCAL | CREAD);
308 tty.c_cflag &= ~CSIZE;
310 tty.c_cflag &= ~CSTOPB;
311 tty.c_cflag &= ~PARENB;
312 tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
313 tty.c_iflag &= ~ICRNL;
314 tty.c_iflag &= ~INPCK;
315 tty.c_iflag &= ~(IXON | IXOFF | IXANY);
319 if (tcsetattr (fd, TCSANOW, &tty) != 0)
321 printf(
"error %d from tcsetattr", errno);
335 static INT_32 last_byte = 0;
336 static INT_32 new_message = 0;
346 for(i = 0; i < 6; i++)
424 for(j = 0; j < 3; j++)
429 for(j = 3; j < 6; j++)
484 static INT_32 counter = 0;
565 while (retries < 100 && bytes_read == 0)
577 for (i = 0; i < n/2; i++ )
579 data[i] = (data_request[cpt++] * 256);
580 data[i] = data[i] + data_request[cpt++];
595 INT_8 valid_answer = 0;
606 for (i = 0; i < n; i++ )
610 data_request[i] = (data[(i/2)] >> 8);
614 data_request[i] = (data[(i/2)] & 0xFF);
620 while (retries < 100 && valid_answer == 0)
627 if(valid_answer == 1)
655 registers[0] >> 8, registers[0] & 0xFF, registers[1] >> 8,
656 registers[1] & 0xFF, registers[2] >> 8, registers[2] & 0xFF);
670 serial_number = (
UINT_64) ((registers[3] >> 8) * 256 * 256 * 256
671 + (registers[3] & 0xFF) * 256 * 256 + (registers[2] >> 8) * 256
672 + (registers[2] & 0xFF));
674 if (serial_number == 0)
681 registers[0] & 0xFF, registers[1] >> 8, serial_number);
695 #ifdef __unix__ //For Unix 696 return read(fd_connexion, buf, buf_len);
697 #elif defined(_WIN32)||defined(WIN32) //For Windows 698 DWORD myBytesRead = 0;
699 ReadFile(hSerial,buf,buf_len,&myBytesRead,NULL);
719 #ifdef __unix__ //For Unix 720 return write(fd_connexion, buf, buf_len);
721 #elif defined(_WIN32)||defined(WIN32) //For Windows 723 return (WriteFile(hSerial, buf, buf_len, &n_bytes, NULL)) ? n_bytes: -1;
751 CRC_calc ^= *adr & 0xFF;
765 if (CRC_calc & 0x0001)
767 CRC_calc = (CRC_calc >> 1)^ 0xA001;
806 reg[0] = (
UINT_8)(base >> 8);
807 reg[1] = (
UINT_8)(base & 0x00FF);
808 words[0] = (
UINT_8)((n/2) >> 8);
809 words[1] = (
UINT_8)((n/2) & 0x00FF);
814 buf[length++] = reg[0];
815 buf[length++] = reg[1];
816 buf[length++] = words[0];
817 buf[length++] = words[1];
823 buf[length++] = (
UINT_8)(CRC & 0x00FF);
824 buf[length++] = (
UINT_8)(CRC >> 8);
840 static INT_32 old_length = 0;
841 static INT_32 counter_no_new_data = 0;
849 length = length + ret;
853 if(length == old_length)
855 if(counter_no_new_data < 5)
857 counter_no_new_data++;
866 counter_no_new_data = 0;
897 if(CRC != (
UINT_16)((buf[length - 1] * 256) + (buf[length - 2])))
909 for(j = 0; j < n; j++)
911 data[j] = buf[j + 3];
959 reg[0] = (
UINT_8)(base >> 8);
960 reg[1] = (
UINT_8)(base & 0x00FF);
961 words[0] = (
UINT_8)((n2/2) >> 8);
962 words[1] = (
UINT_8)((n2/2) & 0x00FF);
968 buf[length++] = reg[0];
969 buf[length++] = reg[1];
970 buf[length++] = words[0];
971 buf[length++] = words[1];
975 for(i = 0; i < n; i++)
977 buf[length++] = data[i];
988 buf[length++] = (
UINT_8)(CRC & 0x00FF);
989 buf[length++] = (
UINT_8)(CRC >> 8);
1003 static INT_32 length = 0;
1004 static INT_32 old_length = 0;
1005 static INT_32 counter_no_new_data = 0;
1011 if(length == old_length)
1013 if(counter_no_new_data < 5)
1015 counter_no_new_data++;
1024 counter_no_new_data = 0;
1027 old_length = length;
1046 if(CRC != (
UINT_16)((buf[length - 1] * 256) + (buf[length - 2])))
1126 if(i >= 0 && i <= 5)
1164 #if defined(_WIN32)||defined(WIN32) //For Windows 1165 CloseHandle(hSerial);
1166 hSerial = INVALID_HANDLE_VALUE;
1178 INT_8 dirParent[20] = {0};
1179 INT_8 port_com[15] = {0};
1181 strcpy(dirParent,
"/dev/");
1182 strcat(dirParent, d_name);
1183 strcpy(port_com, dirParent);
1184 fd_connexion = open(port_com, O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL);
1187 if (fd_connexion != -1)
1189 if(set_com_attribs(fd_connexion,B19200) != -1)
1199 close(fd_connexion);
void rq_com_get_str_production_year(INT_8 *production_year)
Retrieves the sensor firmware version.
#define RQ_COM_TIMER_FOR_STREAM_DETECTION_MAX_VALUE
static void rq_com_send_jam_signal(void)
Send a signal that interrupts the streaming.
static UINT_32 rq_com_msg_received
#define RQ_COM_MAX_STR_LENGTH
static INT_32 rq_com_zero_force_flag
bool rq_com_get_stream_detected()
retrieves the sensor firmware version
static INT_8 rq_com_send_fc_16(INT_32 base, INT_32 n, UINT_16 const *const data)
void rq_sensor_com_read_info_high_lvl(void)
Reads and stores high level information from the sensor. These include the firmware version...
static bool rq_com_new_message
static INT_32 rq_com_rcv_len
static float rq_com_received_data_offset[6]
static INT_32 rq_com_write_port(UINT_8 const *const buf, UINT_32 buf_len)
static void rq_com_stop_stream_after_boot(void)
Send a jam signal to stop the sensor stream and retry until the stream stops.
#define REGISTER_SELECT_OUTPUT
#define RQ_COM_TIMER_FOR_VALID_STREAM_MAX_VALUE
static void rq_com_send_fc_16_request(INT_32 base, INT_32 n, UINT_8 const *const data)
static INT_32 rq_com_wait_for_fc_16_echo(void)
Reads the response to a fc16 write query.
INT_8 rq_com_start_stream(void)
Starts the sensor streaming mode.
static INT_32 rq_com_timer_for_valid_stream
static INT_32 rq_com_rcv_len2
#define REGISTER_FIRMWARE_VERSION
INT_8 rq_sensor_com(const std::string &ftdi_id)
static UINT_8 rq_com_rcv_buff[MP_BUFF_SIZE]
static bool rq_com_valid_stream
static UINT_8 rq_com_rcv_buff2[MP_BUFF_SIZE]
static INT_32 rq_state_sensor_watchdog
#define RQ_COM_JAM_SIGNAL_CHAR
bool rq_com_got_new_message()
Returns true if a new valid stream message has been decoded and is available.
static float rq_com_received_data[6]
void rq_com_get_str_serial_number(INT_8 *serial_number)
Retrieves the sensor serial number.
#define REGISTER_SERIAL_NUMBER
static INT_8 rq_com_send_fc_03(UINT_16 base, UINT_16 n, UINT_16 *const data)
Sends a read request.
static INT_8 rq_com_str_sensor_firmware_version[RQ_COM_MAX_STR_LENGTH]
static UINT_8 rq_com_identify_device(INT_8 const *const d_name)
try to discover a com port by polling each serial port
static INT_8 rq_com_str_sensor_production_year[RQ_COM_MAX_STR_LENGTH]
#define RQ_COM_JAM_SIGNAL_LENGTH
void rq_com_listen_stream(void)
Listens and decode a valid stream input.
static UINT_16 rq_com_crc
void stop_connection()
close the serial port.
void rq_com_do_zero_force_flag()
Set the "zero sensor" flag to 1. When the next stream message will be decoded, the effort values will...
#define REGISTER_PRODUCTION_YEAR
static UINT_16 rq_com_compute_crc(UINT_8 const *adr, INT_32 length)
static UINT_16 rq_com_computed_crc
float rq_com_get_received_data(UINT_8 i)
Return an effort component.
static INT_32 rq_com_wait_for_fc_03_echo(UINT_8 *const data)
bool rq_com_get_valid_stream()
returns if the stream message is valid
void rq_com_get_str_firmware_version(INT_8 *firmware_version)
Retrieves the sensor firmware version.
static void rq_com_send_fc_03_request(UINT_16 base, UINT_16 n)
Compute and send the fc03 request on the com port.
static INT_32 rq_com_read_port(UINT_8 *const buf, UINT_32 buf_len)
Reads incomming data on the com port.
static INT_8 rq_com_tentative_connexion(void)
Tries connecting to the sensor.
static INT_8 rq_com_str_sensor_serial_number[RQ_COM_MAX_STR_LENGTH]
static UINT_8 rq_com_snd_buff[MP_BUFF_SIZE]
static INT_32 rq_com_timer_for_stream_detection
static bool rq_com_stream_detected