19.12.2010 - variables and structs, fixed consoleand interfaces
This commit is contained in:
35
piserial.cpp
35
piserial.cpp
@@ -19,22 +19,32 @@ PISerial::PISerial(PIString name, void * data_, SerialFunc slot): PIThread() {
|
||||
|
||||
|
||||
PISerial::~PISerial() {
|
||||
stop();
|
||||
pthread_cancel(thread);
|
||||
terminate();
|
||||
delete buffer;
|
||||
delete sbuffer;
|
||||
}
|
||||
|
||||
|
||||
void PISerial::terminate() {
|
||||
if (!initialized()) return;
|
||||
if (isRunning()) {
|
||||
stop();
|
||||
pthread_cancel(thread);
|
||||
}
|
||||
#ifdef WINDOWS
|
||||
if (fd != -1) {
|
||||
SetCommState(hCom, &sdesc);
|
||||
SetCommMask(hCom, mask);
|
||||
CloseHandle(hCom);
|
||||
fd = -1;
|
||||
}
|
||||
#else
|
||||
if (fd != -1) {
|
||||
tcsetattr(fd, TCSANOW, &sdesc);
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
#endif
|
||||
delete buffer;
|
||||
delete sbuffer;
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +62,8 @@ void PISerial::begin() {
|
||||
void PISerial::run() {
|
||||
if (dataSize == 0) return;
|
||||
char b;
|
||||
int i = 0, j;
|
||||
uint i = 0;
|
||||
int j;
|
||||
#ifdef WINDOWS
|
||||
WaitCommEvent(hCom, 0, 0);
|
||||
ReadFile(hCom, buffer, dataSize, &readed, 0);
|
||||
@@ -128,7 +139,7 @@ void PISerial::run() {
|
||||
if (!tryagain) i++;
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < readed; ++i) {
|
||||
for (uint i = 0; i < readed; ++i) {
|
||||
b = buffer[i];
|
||||
sbuffer[sbuffIndex] = b;
|
||||
if (sbuffIndex == dataSize - 1) {
|
||||
@@ -142,23 +153,15 @@ void PISerial::run() {
|
||||
|
||||
|
||||
void PISerial::end() {
|
||||
terminate();
|
||||
delete pbuffer;
|
||||
delete hbuffer;
|
||||
if (fd == -1) return;
|
||||
#ifdef WINDOWS
|
||||
SetCommState(hCom, &sdesc);
|
||||
CloseHandle(hCom);
|
||||
#else
|
||||
tcsetattr(fd, TCSANOW, &sdesc);
|
||||
close(fd);
|
||||
#endif
|
||||
fd = -1;
|
||||
}
|
||||
|
||||
|
||||
bool PISerial::init() {
|
||||
#ifdef WINDOWS
|
||||
hCom = CreateFile(devName.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0);
|
||||
hCom = CreateFile(devName.stdString().c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0);
|
||||
if(hCom == INVALID_HANDLE_VALUE) {
|
||||
cout << "[PISerial] Unable to open " << devName << endl;
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user