我有一个问题是从串口拆分引擎控制系统字符串数据。通信协议描述如下。
实施例:
检索FADEC LCD屏幕上显示的数据。如果命令为RSD
,则输出为:
**C7h 2Ch 52h 53h 44h 2Ch 0Dh**
C7h -> Sync
2Ch -> “,” Separator
52h -> “R”
53h -> “S”
44h -> “D”
2Ch -> “,” Separator
0Dh -> CR, last byte.
ECU将返回两个字符串。第一个是收到的命令的副本,后面是请求的数据。在上面的例子中,它将返回:
**C7h, RSD, 0Dh**
C7h -> Sync byte
2Ch -> “,” Separator
Payload in ASCII “TrimLow EGT 20CRpm 50.000Pw=124”
2Ch -> “,” Separator
0Dh -> CR, last byte.
我以字符串格式读取屏幕上的所有数据。当我将RCV
发送到ECU时,它将根据上述表格返回RPM,温度,泵电压,电池电压和油门。
Xicoy描述:(命令,含义,有效载荷)
RCV读取当前值RPM,EGTinºC,泵功率,电池电压,节流%
byte veri[]= { 0xC7, 0x2C, 0x52, 0x43, 0x56, 0x2C, 0x0D }; // for RCV
和数据表(第5-6-7页):http://www.xicoy.com/SerialAdapt1_0.pdf
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int i=0;
byte veri[]= { 0xC7, 0x2C, 0x52, 0x43, 0x56, 0x2C, 0x0D }; // RCV
int myTimeout = 100;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
mySerial.setTimeout(myTimeout);
}
void loop() {
Serial.println(mySerial.readString());
for(i=0;i<7;i++)
{
mySerial.write(veri[i]);
}
}
如何在从ECU返回的数据包中分离RPM,EGT°C,泵电压,电池电压和油门%?在鉴别过程之后,我将通过第二个串口将其发送到LabView。 RPM值可以是6位数(约120000 RPM),排气温度是3位数(EGT 800°C),数字可能会丢失,因为除油门值外所有数据都为零。你能分享样品吗?感谢。
照片:
答案 0 :(得分:-1)
也回答是;
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int i=0;
byte veri[]= { 0xC7, 0x2C, 0x52, 0x43, 0x56, 0x2C, 0x0D }; // RCV
int myTimeout = 10;
String veri2[]={};
void setup() {
Serial.begin(9600);
while (!Serial) {
}
analogWrite(5,250);
mySerial.begin(9600);
mySerial.setTimeout(myTimeout);
}
void loop() { // run over and over
for(i=0;i<7;i++)
{
mySerial.write(veri[i]);
}
String split = mySerial.readString();
String word3 = getValue(split, ',',0);
Serial.println(word3);
for(i=0;i<7;i++)
{
mySerial.write(veri[i]);
}
String split2 = mySerial.readString();
String word4= getValue(split2, ',',1);
Serial.println(word4);
/*
split = mySerial.readString();
word3 = getValue(split, ',',2);
Serial.println(word3);
split = mySerial.readString();
word3 = getValue(split, ',',3);
Serial.println(word3);
split = mySerial.readString();
word3 = getValue(split, ',',4);
Serial.println(word3);
split = mySerial.readString();
word3 = getValue(split, ',',5);
Serial.println(word3);
split = mySerial.readString();
word3 = getValue(split, ',',6);
Serial.println(word3);
split = mySerial.readString();
word3 = getValue(split, ',',7);
Serial.println(word3);
}
* /
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}