Loghorn / ant-plus

A node module for ANT+
MIT License
138 stars 70 forks source link

Can't receive data from multiple sensors at same time #10

Closed 8beeeaaat closed 6 years ago

8beeeaaat commented 6 years ago

I'm programming a Electron app. I did attached HeartRateSensor on channel 0 and SpeedCadenceSensor on channel 1. However, I've received only heart rate data in spite of SpeedCadenceSensor did attached. But, if I replace the channel, I can receive heart rate data instead of speed data. I need your help.

OS: macOS 10.13.2 Beta USB stick: GarminStick2

  initSensors() {
    const stick = new Ant.GarminStick2();
    const heartRateSensor = new HRS.HeartRateSensor(stick);
    const speedCadenceSensor = new SC.SpeedCadenceSensor(stick);
    speedCadenceSensor.setWheelCircumference(2.12);

    var dev_id = 0;

    heartRateSensor.on("hbdata", function(data) {
      // will call
      console.log(
        "heartRateSensor: ",
        data.DeviceID,
        data.ComputedHeartRate,
        data
      );
      if (data.DeviceID !== 0 && dev_id === 0) {
        dev_id = data.DeviceID;
        console.log("detaching...");
        heartRateSensor.detach();
        heartRateSensor.once("detached", function() {
          heartRateSensor.attach(0, dev_id);
        });
      }
    });

    heartRateSensor.on("attached", function() {
      // will call twice
      console.log("heartRateSensor attached");
    });
    heartRateSensor.on("detached", function() {
      console.log("heartRateSensor detached");
    });

    speedCadenceSensor.on("speedData", function(data) {
      // will not call
      console.log("speedCadenceSensor: ", data.DeviceID, data);
    });

    speedCadenceSensor.on("attached", function() {
      // will not call
      console.log("speedCadenceSensor attached");
    });
    speedCadenceSensor.on("detached", function() {
      console.log("speedCadenceSensor detached");
    });

    stick.on("startup", function() {
      console.log("startup");
      console.log("Max channels:", stick.maxChannels); // will output 8
      heartRateSensor.attach(0, 0); // I will not receive heart rate data, if it will change channel 0 to 1
      speedCadenceSensor.attach(1, 0); // I will receive speed data, if it will change channel 1 to 0
      console.log(stick.attachedSensors); // will output [HeartRateSensor, SpeedCadenceSensor]
    });
}
2017-12-09 02 51 49 2017-12-09 03 24 47
8beeeaaat commented 6 years ago

For your information, sample/sample.js was the same behavior too.

8beeeaaat commented 6 years ago

I found a solution in src/ant.ts!

-   static libConfig(how: number): Buffer {
+   static libConfig(channel: number, how: number): Buffer {
        let payload: number[] = [];
-       payload = payload.concat(this.intToLEHexArray(0));
+       payload = payload.concat(this.intToLEHexArray(channel));
                case Constants.MESSAGE_ENABLE_RX_EXT:
-                   this.write(Messages.libConfig(0xE0));
+                   this.write(Messages.libConfig(channel,0xE0));
                case Constants.MESSAGE_CHANNEL_PERIOD:
-                   this.write(Messages.libConfig(0xE0));
+                   this.write(Messages.libConfig(channel,0xE0));
Loghorn commented 6 years ago

Fixed by 58b6b6d47168806b7dbdaf64928ab2ecbb3a5759

8beeeaaat commented 6 years ago

@Loghorn Thank you so much !!

Loghorn commented 6 years ago

Thank you for the PR. I will soon publish an updated version on npm