Line 1... |
Line 1... |
/**
|
/*
|
* @file
|
* Copyright 2018 Sergey Khabarov, sergeykhbr@gmail.com
|
* @copyright Copyright 2016 GNSS Sensor Ltd. All right reserved.
|
*
|
* @author Sergey Khabarov - sergeykhbr@gmail.com
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
* @brief Serial port implementation for Windows.
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
*/
|
*/
|
|
|
#include "api_types.h"
|
#include "api_types.h"
|
#include "api_core.h"
|
#include "api_core.h"
|
#include "attribute.h"
|
#include "attribute.h"
|
Line 12... |
Line 21... |
#include <winspool.h>
|
#include <winspool.h>
|
#include <cstdlib>
|
#include <cstdlib>
|
|
|
namespace debugger {
|
namespace debugger {
|
|
|
void ComPortService::getSerialPortList(AttributeType *list) {
|
void ComPortService::getListOfPorts(AttributeType *list) {
|
list->make_list(0);
|
list->make_list(0);
|
|
|
AttributeType portInfo;
|
AttributeType portInfo;
|
DWORD Ports_MemSize = 0;
|
DWORD Ports_MemSize = 0;
|
DWORD Ports_Count = 0;
|
DWORD Ports_Count = 0;
|
Line 89... |
Line 98... |
}
|
}
|
|
|
delete [] lpPorts;
|
delete [] lpPorts;
|
}
|
}
|
|
|
int ComPortService::openSerialPort(const char *port, int baud, void *hdl) {
|
int ComPortService::openPort(const char *port, AttributeType settings) {
|
char chCom[20];
|
char chCom[20];
|
char chConfig[64];
|
char chConfig[64];
|
HANDLE hFile;
|
HANDLE hFile;
|
COMMPROP CommProp;
|
COMMPROP CommProp;
|
DCB dcb;
|
DCB dcb;
|
Line 102... |
Line 111... |
DWORD Errors;
|
DWORD Errors;
|
COMSTAT Stat;
|
COMSTAT Stat;
|
|
|
RISCV_sprintf(chCom, sizeof(chCom), "\\\\.\\%s", port);
|
RISCV_sprintf(chCom, sizeof(chCom), "\\\\.\\%s", port);
|
RISCV_sprintf(chConfig, sizeof(chConfig),
|
RISCV_sprintf(chConfig, sizeof(chConfig),
|
"baud=%d parity=N data=8 stop=1", baud);
|
"baud=%d parity=N data=8 stop=1", settings[0u].to_int());
|
|
|
hFile = CreateFile(chCom,
|
hFile = CreateFile(chCom,
|
GENERIC_READ|GENERIC_WRITE,
|
GENERIC_READ|GENERIC_WRITE,
|
0,//FILE_SHARE_READ|FILE_SHARE_WRITE,
|
0,//FILE_SHARE_READ|FILE_SHARE_WRITE,
|
NULL,
|
NULL,
|
//OPEN_ALWAYS,
|
//OPEN_ALWAYS,
|
OPEN_EXISTING,
|
OPEN_EXISTING,
|
FILE_ATTRIBUTE_NORMAL,
|
FILE_ATTRIBUTE_NORMAL,
|
NULL);
|
NULL);
|
|
|
*static_cast<HANDLE *>(hdl) = hFile;
|
prtHandler_ = hFile;
|
if (hFile == INVALID_HANDLE_VALUE) {
|
if (hFile == INVALID_HANDLE_VALUE) {
|
if (GetLastError() == ERROR_ACCESS_DENIED) {
|
if (GetLastError() == ERROR_ACCESS_DENIED) {
|
RISCV_error("%s is locked by another device", chCom);
|
RISCV_error("%s is locked by another device", chCom);
|
} else {
|
} else {
|
RISCV_error("Can't open port %s", chCom);
|
RISCV_error("Can't open port %s", chCom);
|
Line 187... |
Line 196... |
PurgeComm(hFile, PURGE_RXABORT | PURGE_TXABORT);
|
PurgeComm(hFile, PURGE_RXABORT | PURGE_TXABORT);
|
|
|
return 0;
|
return 0;
|
}
|
}
|
|
|
void ComPortService::closeSerialPort(void *hdl) {
|
void ComPortService::closePort() {
|
CloseHandle(*static_cast<HANDLE *>(hdl));
|
if (prtHandler_) {
|
|
CloseHandle(*static_cast<HANDLE *>(prtHandler_));
|
|
}
|
|
prtHandler_ = 0;
|
}
|
}
|
|
|
int ComPortService::readSerialPort(void *hdl, char *buf, int bufsz) {
|
int ComPortService::readSerialPort(void *hdl, char *buf, int bufsz) {
|
HANDLE hFile = *static_cast<HANDLE *>(hdl);
|
HANDLE hFile = *static_cast<HANDLE *>(hdl);
|
DWORD dwBytesRead;
|
DWORD dwBytesRead;
|
BOOL success = ReadFile(hFile, buf, bufsz, &dwBytesRead, NULL);
|
BOOL success = ReadFile(hFile, buf, bufsz, &dwBytesRead, NULL);
|
if (!success) {
|
if (!success) {
|
return -1;
|
return -1;
|
}
|
}
|
|
buf[dwBytesRead] = 0;
|
return static_cast<int>(dwBytesRead);
|
return static_cast<int>(dwBytesRead);
|
}
|
}
|
|
|
int ComPortService::writeSerialPort(void *hdl, char *buf, int bufsz) {
|
int ComPortService::writeSerialPort(void *hdl, char *buf, int bufsz) {
|
DWORD lpdwBytesWrittens;
|
DWORD lpdwBytesWrittens;
|