Removed the exception-code and manually copy the string so it supports null-characters

This commit is contained in:
Micke Prag 2010-06-23 08:09:42 +00:00
parent 14d0806319
commit 542dc57237

View file

@ -15,25 +15,28 @@ using namespace TelldusCore;
* Send message to the USB dongle
*/
int Device::send(const std::string &strMessage){
try{
FT_STATUS ftStatus = FT_OK;
FT_HANDLE fthHandle = 0;
std::string msgBack;
ULONG bytesWritten, bytesRead;
char *tempMessage;
int intDongleIndex;
char in;
int intDongleIndex = getDongleIndex();
FT_STATUS ftStatus = FT_OK;
FT_HANDLE fthHandle = 0;
intDongleIndex = getDongleIndex();
if (intDongleIndex < 0) {
return TELLSTICK_ERROR_NOT_FOUND;
}
ftStatus = FT_Open(intDongleIndex, &fthHandle);
int intBaudRate = 9600; //always 9600
ftStatus = FT_SetBaudRate(fthHandle, intBaudRate);
ftStatus = FT_SetBaudRate(fthHandle, 9600); //always 9600
FT_SetTimeouts(fthHandle,5000,0);
ULONG bytesWritten, bytesRead;
char *tempMessage = (char *)malloc(sizeof(char) * (strMessage.size()+1));
strcpy(tempMessage, strMessage.c_str());
tempMessage = (char *)malloc(sizeof(char) * (strMessage.size()));
for(unsigned int i = 0; i < strMessage.size(); ++i) {
tempMessage[i] = strMessage[i];
}
ftStatus = FT_Write(fthHandle, tempMessage, (DWORD)strMessage.length(), &bytesWritten);
free(tempMessage);
@ -42,6 +45,7 @@ int Device::send(const std::string &strMessage){
ftStatus = FT_Read(fthHandle,&in,1,&bytesRead);
if (ftStatus == FT_OK) {
if (bytesRead == 1) {
msgBack.append(1, in);
if (in == '\n') {
break;
}
@ -52,16 +56,10 @@ int Device::send(const std::string &strMessage){
c = false;
}
}
ftStatus = FT_Close(fthHandle);
if (!c) {
return TELLSTICK_ERROR_COMMUNICATION;
}
}
catch(...){
throw;
}
return TELLSTICK_SUCCESS;
}