本文整理汇总了C++中Serial::ReadData方法的典型用法代码示例。如果您正苦于以下问题:C++ Serial::ReadData方法的具体用法?C++ Serial::ReadData怎么用?C++ Serial::ReadData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Serial
的用法示例。
在下文中一共展示了Serial::ReadData方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: _tmain
// application reads from the specified serial port and reports the collected data
int _tmain(int argc, _TCHAR* argv[])
{
printf("Welcome to the serial test app!\n\n");
Serial* SP = new Serial("\\\\.\\COM13"); // adjust as needed
if (SP->IsConnected())
printf("We're connected");
char incomingData[2000] = ""; // don't forget to pre-allocate memory
//printf("%s\n",incomingData);
int dataLength = 1500;
int readResult = 0;
while(SP->IsConnected())
{
readResult = SP->ReadData(incomingData,dataLength);
// printf("Bytes read: (-1 means no data available) %i\n",readResult);
if (readResult == -1) continue;
std::string test(incomingData);
for (int i = 0; i < readResult; i++)
{
printf("%c", incomingData[i]);
}
// printf("%s",incomingData);
test = "";
Sleep(50);
}
return 0;
}
示例2: _tmain
//-----------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
Serial* SP = new Serial(COM_PORT); // Инициализация соединения
if (SP->IsConnected())
printf("We're connected\n\n");
else
printf("Connection refused\n\n");
char incomingData[BUF_SIZE] = ""; // Строка для посылки (данных)
int readResult = 0; // Каков размер нашей посылки
while (SP->IsConnected())
{
readResult = SP->ReadData(incomingData, BUF_SIZE-1);
// Если ничего нет readResult будет равно -1
if (DEBUG) printf("Bytes read: %i\n",readResult);
// Выводим для отладки кол-во байт переданных данных (или -1)
incomingData[readResult] = 0;
// Записываем конец строки после всех нужных данных
if (readResult > 0) printf("%s",incomingData);
// Eсли в посылке что-то было (и она была), то выводим ее
Sleep(UPD_TIME);
// Подождем пока, чтобы не вывести одно и тоже несколько раз
}
delete SP;
Sleep(2000);
return 0;
} // By SnipGhost 06.08.2016
示例3: _tmain
// application reads from the specified serial port and reports the collected data
int _tmain(int argc, _TCHAR* argv[])
{
printf("Welcome to the serial test app!\n\n");
Serial* SP = new Serial("COM4"); // adjust as needed
if (SP->IsConnected())
printf("We're connected\n");
char incomingData[256] = ""; // don't forget to pre-allocate memory
//printf("%s\n",incomingData);
int dataLength = 256;
int readResult = 0;
while(SP->IsConnected())
{
readResult = SP->ReadData(incomingData,dataLength);
// printf("Bytes read: (-1 means no data available) %i\n",readResult);
std::string test(incomingData);
printf("%s",incomingData);
test = "";
Sleep(500);
}
return 0;
}
示例4: _tmain
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Connecting to arduino..." << endl;
Serial* SP = new Serial("\\\\.\\COM7"); // adjust as needed
if (SP->IsConnected())
cout << "Connection established with Arduino!!" << endl; // Let us know the serial is connected
char incomingData[256] = ""; // don't forget to pre-allocate memory
int dataLength = 256;
int readResult = 0;
while(SP->IsConnected())
{
readResult = SP->ReadData(incomingData,dataLength);
//cout << "Bytes read: " << readResult << endl;
string test(incomingData);
if (readResult > 0) {
cout << incomingData << endl;
test = "";
}
Sleep(300);
}
cin >> argc;
return 0;
}
示例5: _RfidReader
DWORD WINAPI _RfidReader(LPVOID lpParameter)
{
Serial* s = new Serial();
char *lastTag = new char[128];
memset(lastTag, '\0', 128);
while(lastTag[0] != '\0');
int tagCount = 0;
while(rfidThreadRunning){
Sleep(50);
char *id = new char[128];
memset(id, '\0', 128);
while(id[0] != '\0');
if(tagCount >= 128){
tagCount = 0;
memset(lastTag, '\0', 128);
while(lastTag[0] != '\0');
}
s->ReadData(id, 1);
if(strlen(id) > 0){
for(int i = 0; i < strlen(id); i++){
lastTag[tagCount++] = id[i];
}
}
delete id;
for(int i = 0; i < strlen(lastTag); i++){
if(lastTag[i] == 0x03){
char* fullTag = sanatizeRfidReading(lastTag);
printf(fullTag);
getCredentials(fullTag);
tagCount = 0;
memset(lastTag, '\0', 128);
while(lastTag[0] != '\0');
delete fullTag;
break;
}
}
}
delete lastTag;
delete s;
return 0;
}
示例6: main
int main(int argc, char* argv[])
{
Serial * Arduino = new Serial("COM8");
cout << "Communicating with COM7 enter data to be sent\n";
char data[256] = "";
int nchar = 256;
char incomingData[256] = "";
while (Arduino->IsConnected())
{
cin >> data;
Arduino->WriteData(data, nchar);
Arduino->ReadData(incomingData, nchar);
cout << incomingData << endl;
}
return 0;
}
示例7: Serial
int *LedAmountTest(char *Comm)
{
Serial* SP = new Serial(Comm);
if (SP->IsConnected())
std::cout << "Connected with COM5" << std::endl;
bool exit = false;
//je kan hier op escape drukken om het programma af ste sluiten hier
std::cout << "Waiting for Arduino. Press ESC to quit" << std::endl;
char Rx_buffer[2] = "";
while (Rx_buffer[0] != '0') //blijf hier hangen tot de arduino klaar is
{
Sleep(100);
SP->ReadData(Rx_buffer, 2);
if (GetAsyncKeyState(VK_ESCAPE))
{
exit = true;
}
if (exit == true)
{
std::cout << "Something went wrong with communication. Check baudrate settings and COM port" << std::endl;
}
}
Rx_buffer[0] = '0';
std::cout << "Got response from arduino sending amount of leds" << std::endl;
//Stuur de hoeveelheid leds naar de arduino
UINT8 Tx_buffer[600 * 3];
ZeroMemory(Tx_buffer, 600 * 3);
Tx_buffer[0] = 600 >> 8 & 0x00FF;
Tx_buffer[1] = 600 & 0x00FF;
SP->WriteData((char*)Tx_buffer, 2);
std::cout << "Press up to add a LED and press down to remove one." << std::endl;
std::cout << "When you are satisfied press SPACEBAR to confirm" << std::endl;
std::cout << "Press ESC to quit" << std::endl;
//onderstaande stukje code zal blijven draaien tot je op ESC drukt
static int leds[4] = { 0 }, i = 0;
int offset = 0;
leds[0]++;
while (i <4)
{
if (GetAsyncKeyState(VK_SPACE)) //Als escape is ingedrukt zet exit true
{
offset += leds[i];
i++;
Sleep(500);
}
if (GetAsyncKeyState(VK_UP))
{
leds[i]++;
ZeroMemory(Tx_buffer, 600 * 3);
for (int j = 0; j < leds[i]; j++)
{
for (int k = 0; k < 3; k++)
{
Tx_buffer[(j + offset) * 3 + k] = 0xff;
}
}
SP->WriteData((char*)Tx_buffer, 1800);
Sleep(50);
}
if (GetAsyncKeyState(VK_DOWN) && leds[i] > 1)
{
leds[i]--;
ZeroMemory(Tx_buffer, 600 * 3);
for (int j = 0; j < leds[i]; j++)
{
for (int k = 0; k < 3; k++)
{
Tx_buffer[(j + offset) * 3 + k] = 0xff;
}
}
SP->WriteData((char*)Tx_buffer, 1800);
Sleep(50);
}
}
SP->~Serial();
return leds;
}
示例8: main
//.........这里部分代码省略.........
Scherm.Bereken_Grid(); //stel de hoeveelheid leds in die worden gebruikt en bereken Grid Grootte
Scherm.set_Gamma(gamma);
//Het programma moet eerst 0xff binnen krijgen als dat het geval is dan mag die beginnen met het oversturen
//van de hoeveelheid leds
//Als die hoeveelheden overeenkomen mag die beginnen met het zenden van led data
std::string String;
String = "\\\\.\\COM";
String += std::to_string(Config[8]);
char *temp = new char[String.size() + 1];
std::copy(String.begin(), String.end(), temp);
temp[String.size()] = '\0';
Serial* SP = new Serial(temp);
delete[] temp;
temp = nullptr;
if (SP->IsConnected())
std::cout << "Connected with COM5" << std::endl;
else
{
std::cout << "Communication Error. Exiting" << std::endl;
return 0;
}
char Rx_buffer[100] = ""; //Dit is de Rx_buffer deze moet een char zijn
//je kan hier op escape drukken om het programma af ste sluiten hier
std::cout << "Waiting for Arduino. Press ESC to quit" << std::endl;
SP->ReadData(Rx_buffer, 2);
while (Rx_buffer[0] != '0') //blijf hier hangen tot de arduino klaar is
{
Sleep(100);
SP->ReadData(Rx_buffer, 2);
if (GetAsyncKeyState(VK_ESCAPE))
{
exit = true;
}
if (exit == true)
{
std::cout << "Something went wrong with communication. Check baudrate settings and COM port" << std::endl;
return 0; //beeindig de software
}
}
Rx_buffer[0] = '0';
std::cout << "Got response from arduino sending amount of leds" << std::endl;
//Stuur de hoeveelheid leds naar de arduino
UINT8 Tx_buffer[600 * 3];
ZeroMemory(Tx_buffer, 600 * 3);
Tx_buffer[0] = Scherm.geefLeds() >> 8 & 0x00FF;
Tx_buffer[1] = Scherm.geefLeds() & 0x00FF;
SP->WriteData((char*)Tx_buffer, 2);
Sleep(1000); //Wacht 1 seconde op de arduino
std::cout << "Press END to quit capturing" << std::endl;
示例9: main
//.........这里部分代码省略.........
//printf("%s\n",incomingData);
int dataLength = 256;
int readResult = 0;
double t1 = 0;
double tChecksum_IN;
double tChecksum_OUT;
//double tRecieve_IN;
double tRecieve_OUT;
int loop = 0;
byte sum = 0;
byte recievedCheckSum = 0;
int left = 0;
int right = 0;
int recievedValue = 0;
float vADC = 0;
float voltagem = 0;
int ze = 255;
int newV1 = 0;
int newV2 = 0;
// int loop;
int change_loop = 1; // o valor dessea variavel fica entre 1 e 6, ela escolhe qual loop estaremos usando
char point = 'i'; // i-> ponto inicial do teste, f-> ponto final do teste
char param = '0'; // 0-> proporcional, 1-> integral, 2-> derivativo
int step_flag = 1; // 1-> passo alto, 2-> passo medio, 3-> passo baixo, 4-> achamos o valor "ótimo";
int analise = 0; // 0->análise do ts, 1->análise do overshoot, 2->análise do upertime
//=========================== INÍCIO DO LOOP ==============================//
while (true)
{
//====================Leitura Mensagem================================ //
readResult = SP->ReadData(incomingData, 11);
/*for (int i = 0; i<11; i++)
{
cout << dec << i << ": ";
cout << hex << (int)incomingData[i] << endl;
//cout << "haha" << endl;
//system("pause");
}*/
//O endereço do xbee de onde veio a mensagem é incomingData[4] e incomingData[5]
if (incomingData[0] == 0x7e && incomingData[1] == 0x00 && incomingData[2] == 0x07 && incomingData[3] == -127 && incomingData[4] == -86 && incomingData[5] == -86 && incomingData[7] == 0)
{
//cout << "_-_-_-_-_-_" << endl;
sum = 0;
recievedCheckSum = 0;
left = 0;
right = 0;
recievedValue = 0;
voltagem = 0;
vADC = 0;
//loop = 0;
for (int i = 0; i < 11; i++)
{
if (i>2 && i < 10)
{
sum = sum + incomingData[i];
}
}
sum = 0xFF - sum;
示例10: _tmain
//.........这里部分代码省略.........
is_any_direction =false;
is_tradition =true;
SP->WriteData("f",1);
}
last_in_situ =now_in_situ;
last_any_direction =now_any_direction;
last_tradition =now_tradition;
if(action)//Double-check that the inactivated throttle value is set to 0.
{
driveDutycycle = 0;
breaking = 225;
}
else
{
breaking = (int)((1-joyinfo.dwRpos/65535.0)*fullDutycycle); //Modify as necessary.
if (breaking=0)
driveDutycycle = (int)((1-joyinfo.dwZpos/65535.0)*fullDutycycle);
else
driveDutycycle = 0;
}
if(driveDutycycle>=0 && driveDutycycle<=255)
{
for (int k=0;k<3;k++)
{
outDrive[2-k]=char(driveDutycycle%10 + '0');
driveDutycycle=driveDutycycle/10;
}
}
if(breaking>=0 && breaking<=255)
{
for (int k=0;k<3;k++)
{
outBreak[2-k]=char(breaking%10 + '0');
breaking=breaking/10;
}
}
steer=(int)(joyinfo.dwXpos/65535.0*encoder_resolution);
if(steer>=0 && steer<=encoder_resolution)
{
for (int k=0;k<4;k++)
{
outSteer[3-k]=char(steer%10 + '0');
steer=steer/10;
}
}
//Writing to Arduino Seriall**************************************************
printf("outDrive:%s\n",outDrive);
bool isWriteDrive = SP->WriteData((char*)&outDrive, strlen(outDrive));
cout << "WriteSucceed?" << isWriteDrive << endl;
printf("outSteer:%s\n",outSteer);
bool isWriteSteer = SP->WriteData((char*)&outSteer, strlen(outSteer));
cout << "WriteSucceed?" << isWriteSteer << endl;
printf("outBreak:%s\n",outBreak);
bool isWriteBreak = SP->WriteData((char*)&outBreak, strlen(outBreak));
cout << "WriteSucceed?" << isWriteBreak << endl;
#if defined(_MSC_VER)
Sleep(120); //time in ms
system("cls");
#elif defined (__linux__)
} //If have sysevent then update joystick values
usleep(120000); //time in us
system("clear");
#endif
//Read back from Arduino
readResult = SP->ReadData(incomingData,dataLengthin);
///*if(readResult<2*sizeof(serial_format)){
// printf("No enough data received. Let's wait.\n");
// Sleep(1500);continue;
//}*/
int ptr=0;
while(!(incomingData[ptr]=='?' && incomingData[ptr+1]=='?') && ptr<readResult-sizeof(serial_format)) {
ptr++;
}
serial_format* ptr_to_first_valid = (serial_format*) &incomingData[ptr];
printf("Actual Steer: %d\n",ptr_to_first_valid->Steer);
printf("Actual Drive: %d\n",ptr_to_first_valid->Drive);
printf("Bus Voltage: %f\n", ptr_to_first_valid->Voltage);
printf("Current on Driving: %f\n", ptr_to_first_valid->CurrentD);
printf("Current on Steering: %f\n", ptr_to_first_valid->CurrentS);
printf("Power Consumed on This Unit: %f\n", (ptr_to_first_valid->Voltage)*((ptr_to_first_valid->CurrentD)+(ptr_to_first_valid->CurrentS)));
//However always read serial data.
#ifdef __linux__
int errNum = FFupdate(joy,ptr_to_first_valid->Steer);
#endif
}
SP->~Serial();
#ifdef __linux__
SDL_JoystickClose(joy);
#endif
system("pause");
return 0;
}