[英]Passing serialport data data in a buffer/variable in C++
我從3傳感器發送的串行端口中的數據格式為
sensor0,sensor1,sensor2
我使用這個程序:
#include <iostream>
#include <string>
#include <sstream>
#include <Windows.h>
#include "SerialClass.h"
char buffer[25];
using namespace std;
int i;
int main()
{
Serial oSerial("COM8:");
oSerial.WriteData("1",1);
Sleep(100);
oSerial.ReadData(buffer,25);
string str = buffer;
string word;
stringstream stream(str);
while( getline(stream, word, ',') )
cout <<word << "\n";
Sleep (10000);
}
結果是:
sensor0
sensor1
sensor2
我希望數據的結果在緩沖區/變量中,以便結果看起來像
y1=sensor0
y2=sensor1
y3=sensor2
我該如何更改代碼,以便結果是這樣的。
在代碼片段中:
int count = 1;
while( getline(stream, word, ',') )
{
cout<<"y"<<count<<"="<<word<< "\n";
count++;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.