I am trying to work with the CellularRadio driver and a module with the SIM900 and there are a few oddities.
In the constructor there is an AT command sent to the serial port and then check a for serial bytes. This I assume is to check if the modem is ON or not.
if (this.serial.BytesToRead != 0) {
this.power.Write(false);
Thread.Sleep(1000);
this.power.Write(true);
Thread.Sleep(2200);
this.serial.DiscardInBuffer();
this.serial.DiscardOutBuffer();
}
The check in my mind is wrong. It should be if(this.serial.BytesToRead == 0) { as this would be the case if the modem was off. In my case, the modem is ON and I get 6 bytes in the buffer so the call then loops through and switches off the modem. Also, when you next call the PowerOn function, it also pulses the reset line. I’ve modified the code so that the powerOn flag is set if characters are received in the constructor. I ignore the powerOn flag check in the PowerOn function because of this and just carry on.
In the DoWork thread, the check on CREG and CGREG does not work. The modem passes back 2 values with a comma separator. The existing code then fails and simply goes past the switch list.
I changed the code from this:
switch (response) {
case "0": this.OnGsmNetworkRegistrationChanged(this, NetworkRegistrationState.NotSearching); break;
case "1": this.OnGsmNetworkRegistrationChanged(this, NetworkRegistrationState.Registered); break;
case "2": this.OnGsmNetworkRegistrationChanged(this, NetworkRegistrationState.Searching); break;
case "3": this.OnGsmNetworkRegistrationChanged(this, NetworkRegistrationState.RegistrationDenied); break;
case "4": this.OnGsmNetworkRegistrationChanged(this, NetworkRegistrationState.Unknown); break;
case 5: this.OnGsmNetworkRegistrationChanged(this, NetworkRegistrationState.Roaming); break;
}
break;
to this, which supports both a dual and single value return.
int reg = 4;
if (response.IndexOf(",") != -1)
reg = int.Parse(response.Split(',')[1]);
else
reg = int.Parse(response);
switch (reg) {
case 0: this.OnGsmNetworkRegistrationChanged(this, NetworkRegistrationState.NotSearching); break;
case 1: this.OnGsmNetworkRegistrationChanged(this, NetworkRegistrationState.Registered); break;
case 2: this.OnGsmNetworkRegistrationChanged(this, NetworkRegistrationState.Searching); break;
case 3: this.OnGsmNetworkRegistrationChanged(this, NetworkRegistrationState.RegistrationDenied); break;
case 4: this.OnGsmNetworkRegistrationChanged(this, NetworkRegistrationState.Unknown); break;
case 5: this.OnGsmNetworkRegistrationChanged(this, NetworkRegistrationState.Roaming); break;
}
break;
I did the same with the CGREG check too.