使用Bluno Nano(DFR0296)的Arduino和C#连接

时间:2019-06-03 20:15:43

标签: c# bluetooth arduino arduino-ide

  1. 下面的代码是Arduino IDE代码,
  2. 在Arduino-uno和Arduino-pro微板上工作良好。
  3. 这会将值(32大小的缓冲区)从Arduino发送到“ C#串行通信程序”。
  4. 但是,当我用Bluno Nano(DFR0296)替换Arduino并运行它时,该值无法传输到C#程序。 (针号已正确检查)
  5. 我在Arduino代码中输入错误还是应该在C#代码中更正?

sketch_dec05a.ino

int En0 = 7;  //  Low enabled
int En1 = 6;  //  Low enabled

int S0  = 5;
int S1  = 4;
int S2  = 3; 
int S3  = 2;


int SIG_pin = A7;

typedef union {
  uint16_t values[32];
  uint8_t buffer[70];
} xData;

xData data;

void setup() {
  Serial.begin(115200);
  pinMode(En0, OUTPUT);
  pinMode(En1, OUTPUT);

  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);

}

void loop() {


while(!Serial.available()) ;

Serial.read();

for(int i = 0 ; i < 32 ; i++){
    data.values[i] = readMux(i);
  }

  Serial.write(data.buffer, 64);


}


int readMux(int channel){
  int controlPin[] = {S0,S1,S2,S3,En0,En1};

  int muxChannel[32][6]={
    {0,0,0,0,0,1}, //channel 0
    {0,0,0,1,0,1}, //channel 1
    {0,0,1,0,0,1}, //channel 2
    {0,0,1,1,0,1}, //channel 3
    {0,1,0,0,0,1}, //channel 4
    {0,1,0,1,0,1}, //channel 5
    {0,1,1,0,0,1}, //channel 6
    {0,1,1,1,0,1}, //channel 7
    {1,0,0,0,0,1}, //channel 8
    {1,0,0,1,0,1}, //channel 9
    {1,0,1,0,0,1}, //channel 10
    {1,0,1,1,0,1}, //channel 11
    {1,1,0,0,0,1}, //channel 12
    {1,1,0,1,0,1}, //channel 13
    {1,1,1,0,0,1}, //channel 14
    {1,1,1,1,0,1}, //channel 15
    {0,0,0,0,1,0}, //channel 16
    {0,0,0,1,1,0}, //channel 17
    {0,0,1,0,1,0}, //channel 18
    {0,0,1,1,1,0}, //channel 19
    {0,1,0,0,1,0}, //channel 20
    {0,1,0,1,1,0}, //channel 21
    {0,1,1,0,1,0}, //channel 22
    {0,1,1,1,1,0}, //channel 23
    {1,0,0,0,1,0}, //channel 24
    {1,0,0,1,1,0}, //channel 25
    {1,0,1,0,1,0}, //channel 26
    {1,0,1,1,1,0}, //channel 27
    {1,1,0,0,1,0}, //channel 28
    {1,1,0,1,1,0}, //channel 29
    {1,1,1,0,1,0}, //channel 30
    {1,1,1,1,1,0}  //channel 31
  };

  //loop through the 6 sig
  for(int i = 0; i < 6; i ++){
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  return val;
}

1 个答案:

答案 0 :(得分:0)

这个问题很老了,但也许其他人会需要答案。在PC上用C#编写的程序的串口初始化代码中,添加一行:

serial.DtrEnable = true;