mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-07-11 22:03:01 -06:00
Bazaar/Serial : added write timeout
git-svn-id: svn://ultimatepp.org/upp/trunk@9867 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
a6a58fd8a0
commit
a17fb14662
1 changed files with 47 additions and 7 deletions
|
|
@ -233,11 +233,27 @@ bool Serial::Read(char &c, uint32_t timeout)
|
|||
}
|
||||
|
||||
// write a single byte
|
||||
bool Serial::Write(char c)
|
||||
bool Serial::Write(char c, uint32_t timeout)
|
||||
{
|
||||
unsigned long count = 0;
|
||||
WriteFile(fd, &c, 1, &count, NULL);
|
||||
return count == 1;
|
||||
if(!timeout)
|
||||
{
|
||||
WriteFile(fd, &c, 1, &count, NULL);
|
||||
return count == 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t tim = msecs() + timeout;
|
||||
for(;;)
|
||||
{
|
||||
WriteFile(fd, &c, 1, &count, NULL);
|
||||
if(count == 1)
|
||||
return true;
|
||||
if((uint32_t)msecs() > tim)
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -281,7 +297,7 @@ String Serial::Read(size_t reqSize, uint32_t timeout)
|
|||
ReadFile(fd, buf, 1000, &count, NULL);
|
||||
if(!count)
|
||||
{
|
||||
if(msecs() > tim)
|
||||
if((uint32_t)msecs() > tim)
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
|
@ -294,11 +310,35 @@ String Serial::Read(size_t reqSize, uint32_t timeout)
|
|||
}
|
||||
|
||||
// writes data
|
||||
bool Serial::Write(String const &data)
|
||||
bool Serial::Write(String const &data, uint32_t timeout)
|
||||
{
|
||||
unsigned long count = 0;
|
||||
WriteFile(fd, ~data, data.GetCount(), &count, NULL);
|
||||
return count == data.GetCount();
|
||||
|
||||
int dLen = data.GetCount();
|
||||
if(!timeout)
|
||||
{
|
||||
WriteFile(fd, ~data, dLen, &count, NULL);
|
||||
return count == dLen;
|
||||
}
|
||||
|
||||
uint32_t tim = msecs() + timeout;
|
||||
const char *dPos = ~data;
|
||||
for(;;)
|
||||
{
|
||||
WriteFile(fd, dPos, dLen, &count, NULL);
|
||||
if(count == dLen)
|
||||
return true;
|
||||
if(count > 0)
|
||||
{
|
||||
dPos += count;
|
||||
dLen -= count;
|
||||
continue;
|
||||
}
|
||||
if((uint32_t)msecs() >= tim)
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
// check if opened
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue