enum errorType {
Success = 0,
PORTISNOTAVALIBLE = 1,
OTHERERROR = 2,
CANTGETPARMS = 3,
CANTSETPARMS = 4
};
class Arduino
{
private:
HANDLE handler;
bool connected;
COMSTAT status;
errorType Errorer;
DWORD errors;
public:
Arduino() {
connected = false;
}
Arduino(string &PortName,unsigned int BoudRate) {
handler = CreateFileA(static_cast<LPCSTR>(PortName.c_str()),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (handler == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
Errorer = PORTISNOTAVALIBLE;
}
else {
Errorer = OTHERERROR;
}
}
else {
DCB SerialParam = { 0 };
if (!GetCommState(handler, &SerialParam)) {
Errorer = CANTGETPARMS;
}
else {
SerialParam.BaudRate = BoudRate;
SerialParam.ByteSize = 8;
SerialParam.StopBits = ONESTOPBIT;
SerialParam.Parity = NOPARITY;
SerialParam.fDtrControl = DTR_CONTROL_ENABLE;
if (!SetCommState(handler, &SerialParam))
{
Errorer = CANTSETPARMS;
}
else {
connected = true;
PurgeComm(this->handler, PURGE_RXCLEAR | PURGE_TXCLEAR);
}
}
}
}
Arduino(const char *PortName, unsigned int BoudRate) {
handler = CreateFileA(static_cast<LPCSTR>(PortName),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (handler == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
Errorer = PORTISNOTAVALIBLE;
}
else {
Errorer = OTHERERROR;
}
}
else {
DCB SerialParam = { 0 };
if (!GetCommState(handler, &SerialParam)) {
Errorer = CANTGETPARMS;
}
else {
SerialParam.BaudRate = BoudRate;
SerialParam.ByteSize = 8;
SerialParam.StopBits = ONESTOPBIT;
SerialParam.Parity = NOPARITY;
SerialParam.fDtrControl = DTR_CONTROL_ENABLE;
if (!SetCommState(handler, &SerialParam))
{
Errorer = CANTSETPARMS;
}
else {
connected = true;
PurgeComm(this->handler, PURGE_RXCLEAR | PURGE_TXCLEAR);
}
}
}
}
bool TryConnect(string &PortName, unsigned int BoudRate) {
handler = CreateFileA(static_cast<LPCSTR>(PortName.c_str()),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (handler == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
Errorer = PORTISNOTAVALIBLE;
}
else {
Errorer = OTHERERROR;
}
return false;
}
else {
DCB SerialParam = { 0 };
if (!GetCommState(handler, &SerialParam)) {
Errorer = CANTGETPARMS;
return false;
}
else {
SerialParam.BaudRate = BoudRate;
SerialParam.ByteSize = 8;
SerialParam.StopBits = ONESTOPBIT;
SerialParam.Parity = NOPARITY;
SerialParam.fDtrControl = DTR_CONTROL_ENABLE;
if (!SetCommState(handler, &SerialParam))
{
Errorer = CANTSETPARMS;
return false;
}
else {
connected = true;
PurgeComm(this->handler, PURGE_RXCLEAR | PURGE_TXCLEAR);
return true;
}
}
}
}
bool TryConnect(const char *PortName, unsigned int BoudRate) {
handler = CreateFileA(static_cast<LPCSTR>(PortName),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (handler == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
Errorer = PORTISNOTAVALIBLE;
}
else {
Errorer = OTHERERROR;
}
return false;
}
else {
DCB SerialParam = { 0 };
if (!GetCommState(handler, &SerialParam)) {
Errorer = CANTGETPARMS;
return false;
}
else {
SerialParam.BaudRate = BoudRate;
SerialParam.ByteSize = 8;
SerialParam.StopBits = ONESTOPBIT;
SerialParam.Parity = NOPARITY;
SerialParam.fDtrControl = DTR_CONTROL_ENABLE;
if (!SetCommState(handler, &SerialParam))
{
Errorer = CANTSETPARMS;
return false;
}
else {
connected = true;
PurgeComm(this->handler, PURGE_RXCLEAR | PURGE_TXCLEAR);
return true;
}
}
}
}
errorType Error() {
return Errorer;
}
~Arduino() {
if (connected) {
connected = false;
CloseHandle(handler);
}
}
bool isConnected()
{
if (!ClearCommError(handler, &errors, &status))
connected = false;
return connected;
}
string TryRead(){
}
string ReadUntil (char EndSing) {
ClearCommError(this->handler, &errors, &this->status);
char buff = 0;
DWORD READED;
string out;
int ReadedSize = 0;
while(buff != EndSing) {
while (!(status.cbInQue > 0)) {
Sleep(5);
ClearCommError(this->handler, &errors, &this->status);
}
ReadFile(handler, &buff, 1, &READED, NULL);
out.append(1, buff);
ReadedSize++;
}
return out;
}
};
Dzień dobry. Wpadłem dziś na pomysł by zmusić Cpp do porozumiewania się z arduino. i pisałem sobie powyższą klasę w oparciu o materiały z internetu - i mam teraz takie pytanie: Czy dało by się lepiej zrobić funkcję ReadUntil i czy ktoś obeznany z ta biblioteką mógłby dać mi kilka wskazówek dotyczących sprawnego wykorzystywania tych narzędzi.