我正在尝试为DMX
软件Freestyler
创建一个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
}
}
有人能让我朝着正确的方向前进吗?
答案 0 :(得分:0)
You must detect startOfMesage
and with it, read channel and value. So, read byte from serial until you detect '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
}
}
}
}
}
Not very elegant but could work.