25 #if defined(_WIN32) || defined(_WIN64)
34 #define LATENCY_TIMER 16
47 : serial_handle_(INVALID_HANDLE_VALUE),
48 baudrate_(DEFAULT_BAUDRATE_),
49 packet_start_time_(0.0),
51 tx_time_per_byte_(0.0)
56 sprintf_s(buffer,
sizeof(buffer),
"\\\\.\\%s", port_name);
60 bool PortHandlerWindows::openPort()
67 if (serial_handle_ != INVALID_HANDLE_VALUE)
69 CloseHandle(serial_handle_);
70 serial_handle_ = INVALID_HANDLE_VALUE;
76 PurgeComm(serial_handle_, PURGE_RXABORT | PURGE_RXCLEAR);
81 strcpy_s(port_name_,
sizeof(port_name_), port_name);
94 return setupPort(baudrate);
105 BOOL res = DeviceIoControl(serial_handle_, GENERIC_READ | GENERIC_WRITE, NULL, 0, 0, 0, &retbyte, (LPOVERLAPPED)NULL);
107 printf(
"%d", (
int)res);
115 if (ReadFile(serial_handle_, packet, (DWORD)length, &dwRead, NULL) == FALSE)
125 if (WriteFile(serial_handle_, packet, (DWORD)length, &dwWrite, NULL) == FALSE)
133 packet_start_time_ = getCurrentTime();
134 packet_timeout_ = (tx_time_per_byte_ * (double)packet_length) + (LATENCY_TIMER * 2.0) + 2.0;
139 packet_start_time_ = getCurrentTime();
140 packet_timeout_ = msec;
145 if (getTimeSinceStart() > packet_timeout_)
153 double PortHandlerWindows::getCurrentTime()
155 QueryPerformanceCounter(&counter_);
156 QueryPerformanceFrequency(&freq_);
157 return (
double)counter_.QuadPart / (double)freq_.QuadPart * 1000.0;
160 double PortHandlerWindows::getTimeSinceStart()
164 time = getCurrentTime() - packet_start_time_;
165 if (time < 0.0) packet_start_time_ = getCurrentTime();
170 bool PortHandlerWindows::setupPort(
int baudrate)
173 COMMTIMEOUTS timeouts;
178 serial_handle_ = CreateFileA(port_name_, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
179 if (serial_handle_ == INVALID_HANDLE_VALUE)
181 printf(
"[PortHandlerWindows::SetupPort] Error opening serial port!\n");
185 dcb.DCBlength =
sizeof(DCB);
186 if (GetCommState(serial_handle_, &dcb) == FALSE)
187 goto MCY_HAL_OPEN_ERROR;
190 dcb.BaudRate = (DWORD)baudrate;
192 dcb.Parity = NOPARITY;
193 dcb.StopBits = ONESTOPBIT;
194 dcb.fParity = NOPARITY;
197 dcb.fAbortOnError = 0;
203 dcb.fDtrControl = DTR_CONTROL_DISABLE;
204 dcb.fRtsControl = RTS_CONTROL_DISABLE;
205 dcb.fDsrSensitivity = 0;
206 dcb.fOutxDsrFlow = 0;
207 dcb.fOutxCtsFlow = 0;
209 if (SetCommState(serial_handle_, &dcb) == FALSE)
210 goto MCY_HAL_OPEN_ERROR;
212 if (SetCommMask(serial_handle_, 0) == FALSE)
213 goto MCY_HAL_OPEN_ERROR;
214 if (SetupComm(serial_handle_, 4096, 4096) == FALSE)
215 goto MCY_HAL_OPEN_ERROR;
216 if (PurgeComm(serial_handle_, PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_RXCLEAR) == FALSE)
217 goto MCY_HAL_OPEN_ERROR;
218 if (ClearCommError(serial_handle_, &dwError, NULL) == FALSE)
219 goto MCY_HAL_OPEN_ERROR;
221 if (GetCommTimeouts(serial_handle_, &timeouts) == FALSE)
222 goto MCY_HAL_OPEN_ERROR;
225 timeouts.ReadIntervalTimeout = 0;
226 timeouts.ReadTotalTimeoutMultiplier = 0;
227 timeouts.ReadTotalTimeoutConstant = 1;
228 timeouts.WriteTotalTimeoutMultiplier = 0;
229 timeouts.WriteTotalTimeoutConstant = 0;
230 if (SetCommTimeouts(serial_handle_, &timeouts) == FALSE)
231 goto MCY_HAL_OPEN_ERROR;
233 tx_time_per_byte_ = (1000.0 / (double)baudrate_) * 10.0;
void clearPort()
The function that clears the port @description The function clears the port.
bool setBaudRate(const int baudrate)
The function that sets baudrate into the port handler @description The function sets baudrate into th...
int getBaudRate()
The function that returns current baudrate set into the port handler @description The function return...
int writePort(uint8_t *packet, int length)
The function that writes bytes on the port buffer @description The function writes bytes on the port ...
void closePort()
The function that closes the port @description The function closes the port.
int getBytesAvailable()
The function that checks how much bytes are able to be read from the port buffer @description The fun...
PortHandlerWindows(const char *port_name)
The function that initializes instance of PortHandler and gets port_name @description The function in...
void setPacketTimeout(uint16_t packet_length)
The function that sets and starts stopwatch for watching packet timeout @description The function set...
void setPortName(const char *port_name)
The function that sets port name into the port handler @description The function sets port name into ...
bool isPacketTimeout()
The function that checks whether packet timeout is occurred @description The function checks whether ...
char * getPortName()
The function that returns port name set into the port handler @description The function returns curre...
int readPort(uint8_t *packet, int length)
The function that reads bytes from the port buffer @description The function gets bytes from the port...