Removed the exception-code and manually copy the string so it supports null-characters
This commit is contained in:
parent
14d0806319
commit
542dc57237
1 changed files with 34 additions and 36 deletions
|
@ -15,52 +15,50 @@ using namespace TelldusCore;
|
||||||
* Send message to the USB dongle
|
* Send message to the USB dongle
|
||||||
*/
|
*/
|
||||||
int Device::send(const std::string &strMessage){
|
int Device::send(const std::string &strMessage){
|
||||||
|
std::string msgBack;
|
||||||
|
ULONG bytesWritten, bytesRead;
|
||||||
|
char *tempMessage;
|
||||||
|
int intDongleIndex;
|
||||||
|
char in;
|
||||||
|
|
||||||
try{
|
FT_STATUS ftStatus = FT_OK;
|
||||||
FT_STATUS ftStatus = FT_OK;
|
FT_HANDLE fthHandle = 0;
|
||||||
FT_HANDLE fthHandle = 0;
|
|
||||||
char in;
|
|
||||||
|
|
||||||
int intDongleIndex = getDongleIndex();
|
intDongleIndex = getDongleIndex();
|
||||||
if (intDongleIndex < 0) {
|
if (intDongleIndex < 0) {
|
||||||
return TELLSTICK_ERROR_NOT_FOUND;
|
return TELLSTICK_ERROR_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
ftStatus = FT_Open(intDongleIndex, &fthHandle);
|
ftStatus = FT_Open(intDongleIndex, &fthHandle);
|
||||||
int intBaudRate = 9600; //always 9600
|
ftStatus = FT_SetBaudRate(fthHandle, 9600); //always 9600
|
||||||
ftStatus = FT_SetBaudRate(fthHandle, intBaudRate);
|
FT_SetTimeouts(fthHandle,5000,0);
|
||||||
FT_SetTimeouts(fthHandle,5000,0);
|
|
||||||
ULONG bytesWritten, bytesRead;
|
|
||||||
|
|
||||||
char *tempMessage = (char *)malloc(sizeof(char) * (strMessage.size()+1));
|
tempMessage = (char *)malloc(sizeof(char) * (strMessage.size()));
|
||||||
strcpy(tempMessage, strMessage.c_str());
|
for(unsigned int i = 0; i < strMessage.size(); ++i) {
|
||||||
ftStatus = FT_Write(fthHandle, tempMessage, (DWORD)strMessage.length(), &bytesWritten);
|
tempMessage[i] = strMessage[i];
|
||||||
free(tempMessage);
|
}
|
||||||
|
ftStatus = FT_Write(fthHandle, tempMessage, (DWORD)strMessage.length(), &bytesWritten);
|
||||||
|
free(tempMessage);
|
||||||
|
|
||||||
bool c = true;
|
bool c = true;
|
||||||
while(c) {
|
while(c) {
|
||||||
ftStatus = FT_Read(fthHandle,&in,1,&bytesRead);
|
ftStatus = FT_Read(fthHandle,&in,1,&bytesRead);
|
||||||
if (ftStatus == FT_OK) {
|
if (ftStatus == FT_OK) {
|
||||||
if (bytesRead == 1) {
|
if (bytesRead == 1) {
|
||||||
if (in == '\n') {
|
msgBack.append(1, in);
|
||||||
break;
|
if (in == '\n') {
|
||||||
}
|
break;
|
||||||
} else { //Timeout
|
|
||||||
c = false;
|
|
||||||
}
|
}
|
||||||
} else { //Error
|
} else { //Timeout
|
||||||
c = false;
|
c = false;
|
||||||
}
|
}
|
||||||
}
|
} else { //Error
|
||||||
|
c = false;
|
||||||
ftStatus = FT_Close(fthHandle);
|
|
||||||
|
|
||||||
if (!c) {
|
|
||||||
return TELLSTICK_ERROR_COMMUNICATION;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(...){
|
ftStatus = FT_Close(fthHandle);
|
||||||
throw;
|
if (!c) {
|
||||||
|
return TELLSTICK_ERROR_COMMUNICATION;
|
||||||
}
|
}
|
||||||
return TELLSTICK_SUCCESS;
|
return TELLSTICK_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue