I've set up a PWM in my code that's going to be controlling a servo.
PWM throttlePort2 = new PWM(PWMChannels.PWM_PIN_D11, 2000, 0.0, false);//throttlePort2.Frequency = 2000;//throttlePort2.DutyCycle = 0;throttlePort2.Start();
I've set up an event handler for incoming data from a COM port. Data is sent about 4 to 8 times per second depending on conditions. When that data is received I'm using it to alter the duty cycle on the PWM.
double _throttlePosition;public double ThrottlePosition{ get { return _throttlePosition; } set { if (value != _throttlePosition) { _throttlePosition = value; if (throttlePort != null) throttlePort.DutyCycle = _throttlePosition; } }}
When the code is running the PWM doesn't ever appear to be altered. If I set a breakpoint here and allow the code to continue immediately after the breakpoint then it executes fine. But it only seems to execute under this condition. In verifying what was going on I tried using the PWM on the onboard led and also tried with a speaker connected to the PWM pin. Both yield the same results. Is there anything I'm doing wrong?
I found another thread from 2 years ago about an odd behaviour (bug?) that occurs if you don't empty the read buffer (http://forums.netdui...recieved-event/). But I don't know if it is applicable given the way that I am reading from the port.
void OnControlDataReceived(object sender, SerialDataReceivedEventArgs e){ while (_controlPort.BytesToRead > 0) { int bytesToReadCount = _controlPort.BytesToRead; int bytesRead = _controlPort.Read( _receiveBuffer, _receiveBufferLocation, _controlPort.BytesToRead ); _receiveBufferLocation += bytesRead; CheckBufferForFlush(); //<-- some stuff happens here //to empty _receiveBuffer }}