对于前面的代码,串口监视器将获得十六进制的消息,如“08000AE23BDB”,但在处理这些数据时只显示为“NaN”。我想也许只有字符串可以进行处理?但是“字符串缓冲区(缓冲区[count])”似乎无法工作,我该如何转换呢?
以下是arduino的代码:
#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(2, 3);
unsigned char buffer[64]; // buffer array for data recieve over serial port
int count=0; // counter for buffer array
void setup()
{
SoftSerial.begin(9600); // the SoftSerial baud rate
Serial.begin(9600); // the Serial port of Arduino baud rate.
}
void loop()
{
if (SoftSerial.available()) // if date is comming from softwareserial port ==> data is comming from SoftSerial shield
{
while(SoftSerial.available()) // reading data into char array
{
buffer[count++]=SoftSerial.read(); // writing data into array
if(count == 64)break;
}
Serial.write(buffer,count); // if no data transmission ends, write buffer to hardware serial port
clearBufferArray(); // call clearBufferArray function to clear the storaged data from the array
count = 0; // set counter of while loop to zero
}
if (Serial.available()) // if data is available on hardwareserial port ==> data is comming from PC or notebook
{
SoftSerial.write(Serial.read()); // write it to the SoftSerial shield
}
}
void clearBufferArray() // function to clear buffer array
{
for (int i=0; i<count;i++){
{
buffer[i]=NULL;
}
} // clear all index of array with command NULL
}
这是处理的按摩部分:
void serialEvent(Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
float[] data = float(split(inString, ","));
if (data.length >=1)
{
direction1 = data[0];
}
println("data");
println(data[0]);
}
}
答案 0 :(得分:0)
也许这个?
String inString = "08000AE23BDB ";
if (inString != null) {
inString = trim(inString);
String[] data = split(inString, ",");
if (data.length >=1)
{
//direction1 = data[0];
}
println(unhex(data[0]));
}