-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSerialConnection.cpp
90 lines (71 loc) · 2.41 KB
/
SerialConnection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "SerialConnection.h"
#include <time.h>
#include <iostream>
//klass förr seriell kommunikation med mikrokontroller
using namespace std;
SerialConnection::SerialConnection(char* arg):Serial(arg){
readMutex=CreateMutex(NULL,false,_T("readMutex"));
writeMutex=CreateMutex(NULL,false,_T("readMutex"));
inBufLength=0;
outBufLength=0;
inBuf;//=new char[256];
outBuf=new char[256];
mode="c";
serialThreadHandle=(HANDLE)_beginthreadex(NULL,0,serialThread,this,0,NULL);// skapar tråden som driver detta objekt och startar den
}
SerialConnection::~SerialConnection(){
Serial::~Serial();
TerminateThread(serialThreadHandle,0);
}
void SerialConnection::communicate(){
//WaitForSingleObject(readWriteMutex,INFINITE);
char* defaultMode="c";//command
while(true){
/*mode="d";*/
WaitForSingleObject(writeMutex,INFINITE); //Sychronized section
WriteData(mode,1);//skriv vilken typ av meddelande som skickas
//cout<<mode<<endl;
//mode=defaultMode;
WriteData(inBuf,inBufLength);//skriv meddelande som fås av recieverThread
//inBuf;
inBufLength=0;
ReleaseMutex(readMutex);//utanför syncning
Sleep(10);//väntar på att mikrokontrollern ska färdigställa sin cykel, se Rapport, mikrokontroller
WaitForSingleObject(readMutex,INFINITE); //Sychronized section
outBufLength=ReadData(outBuf,MAXBUF);//läser in meddelande från mikrokontroller
if(outBufLength<0){
outBufLength=0;
}
/*if(outBufLength<0){
cout<<"could not read Serial"<<endl;
}*/
ReleaseMutex(readMutex); //
}
}
void SerialConnection::write(char *com, int length, char *_mode){//sätter vd som skall skickas till mikrokontrollern
// DEBUG
for(int i = 0; i < length; i++){
cout << (int)(i % 5 == 3 ? (signed char)com[i] : (unsigned char)com[i]) << "\t";
}
cout<<endl;
WaitForSingleObject(writeMutex, INFINITE); //Sychronized section
mode = _mode; //
memcpy(inBuf, com, length); //
inBufLength = length; //
ReleaseMutex(writeMutex); //
}
int SerialConnection::read(char* com){//läser den senste informationen från mikrokontrollern
WaitForSingleObject(readMutex,INFINITE);
memcpy(com, outBuf,outBufLength);
int temp=outBufLength;
ReleaseMutex(readMutex);
return temp;
}
bool SerialConnection::isAvailable(){
return IsConnected();
}
unsigned _stdcall SerialConnection::serialThread(void* param){
SerialConnection* serial=(SerialConnection*)param;
serial->communicate();
return NULL;
}