本文整理汇总了C++中Port::Read方法的典型用法代码示例。如果您正苦于以下问题:C++ Port::Read方法的具体用法?C++ Port::Read怎么用?C++ Port::Read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Port
的用法示例。
在下文中一共展示了Port::Read方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char **argv)
{
Args args(argc, argv, "PORT BAUD");
const DeviceConfig config = ParsePortArgs(args);
args.ExpectEnd();
InitialiseIOThread();
Port *port = OpenPort(config, nullptr, *(DataHandler *)nullptr);
if (port == NULL) {
delete port;
fprintf(stderr, "Failed to open COM port\n");
return EXIT_FAILURE;
}
ConsoleOperationEnvironment env;
if (!port->WaitConnected(env)) {
delete port;
DeinitialiseIOThread();
fprintf(stderr, "Failed to connect the port\n");
return EXIT_FAILURE;
}
char buffer[4096];
while (true) {
switch (port->WaitRead(env, 60000)) {
case Port::WaitResult::READY:
break;
case Port::WaitResult::TIMEOUT:
continue;
case Port::WaitResult::FAILED:
delete port;
return EXIT_FAILURE;
case Port::WaitResult::CANCELLED:
delete port;
return EXIT_SUCCESS;
}
int nbytes = port->Read(buffer, sizeof(buffer));
if (nbytes < 0)
break;
fwrite((const void *)buffer, 1, nbytes, stdout);
}
delete port;
DeinitialiseIOThread();
return EXIT_SUCCESS;
}
示例2: timeout
static bool
TryConnect(Port &port, char *user_data, size_t max_user_data,
OperationEnvironment &env)
{
port.Flush();
port.Write('\x02'); // send IO Mode command
unsigned user_size = 0;
TimeoutClock timeout(8000);
while (true) {
int remaining = timeout.GetRemainingSigned();
if (remaining < 0)
return false;
if (port.WaitRead(env, remaining) != Port::WaitResult::READY)
return false;
int nbytes = port.Read(user_data + user_size, max_user_data - user_size);
if (nbytes < 0)
return false;
if (user_size == 0) {
const char *minus = (const char *)memchr(user_data, '-', nbytes);
if (minus == NULL)
continue;
user_size = user_data + nbytes - minus;
memmove(user_data, minus, user_size);
} else
user_size += nbytes;
char *end = (char *)memchr(user_data, '\x13', user_size);
if (end != NULL) {
*end = 0;
port.Write('\x16');
return true;
}
if (user_size >= max_user_data)
/* response too large */
return false;
}
return false;
}
示例3: sizeof
int
CAI302::ReadLargeReply(Port &port, void *buffer, unsigned max_size,
OperationEnvironment &env,
std::chrono::steady_clock::duration timeout)
{
unsigned char header[5];
if (!port.FullRead(header, sizeof(header), env, timeout))
return -1;
if (header[0] == 0x09 && header[1] >= 0x10 &&
header[3] == 0x0d && header[4] == 0x0a) {
/* this is probably a "short" reply with an upload prompt, due to
a transmission error - now see if the remaining 4 bytes contain
the "up>" prompt */
char prompt[4];
if (port.Read(prompt, 4) == 4 && prompt[0] == 0x0a &&
prompt[1] == 'u' && prompt[2] == 'p' && prompt[3] == '>')
return -2;
return -1;
}
unsigned size = (header[0] << 8) | header[1];
if (size < sizeof(header))
return -1;
size -= sizeof(header);
if (size > max_size)
size = max_size;
if (!port.FullRead(buffer, size, env, timeout))
return -1;
// XXX verify the checksum
if (size < max_size) {
/* fill the rest with zeroes */
char *p = (char *)buffer;
std::fill(p + size, p + max_size, 0);
}
return size;
}