Parsing Arduino串行读取和解析十六进制
我正在尝试为Parsing Arduino串行读取和解析十六进制,parsing,arduino,serial-port,dmx512,Parsing,Arduino,Serial Port,Dmx512,我正在尝试为DMX软件Freestyle创建一个Arduino接口,但由于知道所接收数据的编码,因此很难解析所接收的数据 下面的图像是一个串行监视器,我连接到Freestyler以查看传入的数据,格式非常简单,每个通道3个字节。第一个字节是startOfMessage,第二个字节是通道号,第三个字节是通道值 串行监视器显示为十六进制和十进制 为了进行测试,我尝试在正确解析startOfmessage(一个常量)时打开一个led byte myByte; void setup(void){ Se
DMX
软件Freestyle
创建一个Arduino接口,但由于知道所接收数据的编码,因此很难解析所接收的数据
下面的图像是一个串行监视器,我连接到Freestyler
以查看传入的数据,格式非常简单,每个通道3个字节。第一个字节是startOfMessage
,第二个字节是通道号,第三个字节是通道值
串行监视器显示为十六进制和十进制
为了进行测试,我尝试在正确解析startOfmessage(一个常量)时打开一个led
byte myByte;
void setup(void){
Serial.begin(9600); // begin serial communication
pinMode(13,OUTPUT);
}
void loop(void) {
if (Serial.available()>0) { // there are bytes in the serial buffer to read
while(Serial.available()>0) { // every time a byte is read it is expunged
// from the serial buffer so keep reading the buffer until all the bytes
// have been read.
myByte = Serial.read(); // read in the next byte
}
if(myByte == 72){
digitalWrite(13,HIGH);
}
if(myByte == 48){
digitalWrite(13,HIGH);
}
delay(100); // a short delay
}
}
有人能给我指出正确的方向吗
您必须检测
startOfMesage
,并用它读取频道和值。所以,从串口读取字节,直到检测到“0x48”
byte myByte, channel, value;
bool lstart = false; //Flag for start of message
bool lchannel = false; //Flag for channel detected
void setup(){
Serial.begin(9600); // begin serial communication
pinMode(13,OUTPUT);
}
void loop() {
if (Serial.available()>0) { // there are bytes in the serial buffer to read
myByte = Serial.read(); // read in the next byte
if(myByte == 0x48 && !lstart && !lchannel){ //startOfMessage
lstart = true;
digitalWrite(13,HIGH);
} else {
if(lstart && !lchannel){ //waiting channel
lstart = false;
lchannel = true;
channel = myByte;
} else {
if(!lstart && lchannel){ //waiting value
lchannel = false;
value = myByte;
} else {
//incorrectByte waiting for startOfMesagge or another problem
}
}
}
}
}
不是很优雅,但可以工作