清理串行读取字符串的第一部分

时间:2018-09-03 19:13:02

标签: c# arduino serial-port arduino-uno

我目前正在从连接到Arduino的i2c传感器连续读取数据,并成功捕获数据。传感器获取4个不同的X,Y值,然后通过Serial.Print以以下格式发送它们:

X1,Y1,X2,Y2,X3,Y3,X4,Y4(其中X1-X4和Y1-Y4是介于0-1023之间的值,即1023、1023、1023、1023、1023、1023、1023 ,1023),然后执行Serial.println(“”)命令。整个过程不断重复进行,直到关闭为止。

我现在的问题是,字符串的第一部分在其前端具有完全无法使用的数据,然后将其标准化为我可以读取和使用的数据。串行监视器结果: enter image description here

继续这样直到停止。

我正试图从Visual Studios项目中读取此数据,而第一组数据确实阻碍了我将值放入数组时继续前进的能力。由于奇怪的字符,Visual Studio挂断了串行读取->数组。任何帮助,将不胜感激。

Arduino代码:

#include <Wire.h>
int IRsensorAddress = 0xB0;
//int IRsensorAddress = 0x58;

int slaveAddress;
int ledPin = 13;
boolean ledState = false;
byte data_buf[16];
int i;
int Ix[4];
int Iy[4];
int s;
byte test = 1023.5f;

void Write_2bytes(byte d1, byte d2)
  {
      Wire.beginTransmission(slaveAddress);
      Wire.write(d1); Wire.write(d2);
      Wire.endTransmission();
  }


void setup()
{
      slaveAddress = IRsensorAddress >> 1; // This results in 0x21 as the address to pass to TWI
      Serial.begin(19200);
      pinMode(ledPin, OUTPUT); // Set the LED pin as output
      Wire.begin();

      Wire.beginTransmission(slaveAddress);
      Wire.write(0x30); Wire.write(0x01);
      Wire.endTransmission(); delay(10);

      Wire.beginTransmission(slaveAddress);
      Wire.write(0x00); Wire.write(0x00);Wire.write(0x00); Wire.write(0x00);Wire.write(0x00); Wire.write(0x00);Wire.write(0x00); Wire.write(0x09);
      Wire.endTransmission(); delay(10);

      Wire.beginTransmission(slaveAddress);
      Wire.write(0x07); Wire.write(0x00);Wire.write(0x41);
      Wire.endTransmission(); delay(10);

      Wire.beginTransmission(slaveAddress);
      Wire.write(0x1A); Wire.write(0x40);Wire.write(0x00);
      Wire.endTransmission(); delay(10);

      Wire.beginTransmission(slaveAddress);
      Wire.write(0x33); Wire.write(0x03);
      Wire.endTransmission(); delay(10);

      Wire.beginTransmission(slaveAddress);
      Wire.write(0x30); Wire.write(0x08);
      Wire.endTransmission(); delay(10);
      delay(100);
      /* IR sensor initialize
      Write_2bytes(0x30,0x01); delay(10);
      Write_2bytes(0x30,0x08); delay(10);
      Write_2bytes(0x06,0x90); delay(10);
      Write_2bytes(0x08,0xC0); delay(10);
      Write_2bytes(0x1A,0x40); delay(10);
      Write_2bytes(0x33,0x33); delay(10);
      delay(100);
      */
}    
void loop()
    {
        ledState = !ledState;
        if (ledState) { digitalWrite(ledPin,HIGH); } else { digitalWrite(ledPin,LOW); }

        //IR sensor read
        Wire.beginTransmission(slaveAddress);
        Wire.write(0x36);
        Wire.endTransmission();
        Wire.requestFrom(slaveAddress, 16); // Request the 2 byte heading (MSB comes first)

        for (i=0;i<16;i++) { data_buf[i]=0; }
        i=0;

        while(Wire.available() && i < 16) {
            data_buf[i] = Wire.read();
            i++;
        }

        Ix[0] = data_buf[1];
        Iy[0] = data_buf[2];
        s = data_buf[3];
        //Ix[0] += (s & 0x30) <<4;
        Ix[0] = data_buf[1] | ((data_buf[3] >> 4) & 0x03) << 8;
        Iy[0] = data_buf[2] | ((data_buf[3] >> 6) & 0x03) << 8;
        //Ix[0] = Ix[0] / test;

        Ix[1] = data_buf[4];
        Iy[1] = data_buf[5];
        s = data_buf[6];
        Ix[1] += (s & 0x30) <<4;
        Iy[1] += (s & 0xC0) <<2;

        Ix[2] = data_buf[7];
        Iy[2] = data_buf[8];
        s = data_buf[9];
        Ix[2] += (s & 0x30) <<4;
        Iy[2] += (s & 0xC0) <<2;

        Ix[3] = data_buf[10];
        Iy[3] = data_buf[11];
        s = data_buf[12];
        Ix[3] += (s & 0x30) <<4;
        Iy[3] += (s & 0xC0) <<2;

        for(i=0; i<4; i++)
        {
            if (Ix[i] < 1000)
                Serial.print("");
            if (Ix[i] < 100)
                Serial.print("");
            if (Ix[i] < 10)
                Serial.print("");
            Serial.print( int(Ix[i]) );
            Serial.print(",");
            if (Iy[i] < 1000)
                Serial.print("");
            if (Iy[i] < 100)
                Serial.print("");
            if (Iy[i] < 10)
                Serial.print("");
            Serial.print( int(Iy[i]) );
            if (i<3)
                Serial.print(",");
        }

        //Serial.print(",");
        Serial.println("");
        delay(15);
    }

如果可以从该C#项目中解决此问题,我也可以提供Visual Studio代码。

0 个答案:

没有答案