Exemple #1
0
        public M3SCommandReceivedEventArgs(IM3S_Dataframe rReceivedFrame)
        {
            frame = rReceivedFrame;
            byte[] frameBytes = frame.GetDataframe();
            int    upperBound = frameBytes[2];          // M3s-Upperbound

            cmd        = frameBytes[3];
            paramBytes = new byte[upperBound];

            for (int i = 0; i < upperBound; i++)
            {
                paramBytes[i] = frameBytes[i + 4];
            }
        }
Exemple #2
0
        private bool processCommand(IM3S_Dataframe rDataFrame, out int oErrorCode)
        {
            oErrorCode = -555;
            byte[] payload = protocol.ExtractPayload(rDataFrame.GetDataframe());

            byte cmd = payload[0];

            switch (cmd)
            {
            case (byte)M3SCommand.GetInformation:
                oErrorCode = sendSlaveInfo(rDataFrame);
                break;

            case (byte)M3SCommand.Ping:
                oErrorCode = 0;                                 // no error, ack is sent outside...
                cnt++;

                break;

            default:

                if (delegateCommand != null)
                {
                    return(delegateCommand(this, new M3SCommandReceivedEventArgs(rDataFrame)));
                }
                return(false);
            }

            if (oErrorCode == 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #3
0
 private int writeToHardware(IM3S_Dataframe frameToWrite)
 {
     ackAlreadySent  = true;
     somethingToSend = true;
     return(hwInterface.WriteData(frameToWrite.GetDataframe()));
 }
Exemple #4
0
        // Schreibt Frame und stellt fest, ob ein Acknowledge benötigt wird oder nicht. Wenn ja, wird auf Acknowledge gewartet, sonst nur versendet
        private int writeFrame(IM3S_Dataframe vFrame)
        {
            bool sendSuccesful = false;
            int ptrBefore;
            int error=0;

            // TODO: Derive acknowledge from data..

            if(available)
            {
                if(interferenceHandling)
                {
                    recBuffer.HandleGarbage();
                }

                if(!vFrame.NeedsAcknowledgement) // Wenn kein Acknowledge gefordert wird
                {
                    return(this.sendToHardware(vFrame.GetDataframe()));
                }

                // ELSE

                for(int i= 0; (i < tryXTimes) && !(sendSuccesful); i++)
                {
                    ptrBefore = recBuffer.DataPtr;
                    //stdOut.Debug("PtrBefore now (before sending..): " + ptrBefore.ToString()); // Ausgabe zum Bibliotheksdebuggen

                    if(sendToHardware(vFrame.GetDataframe()) == 0) // wenn erfolgreich gesendet wurde
                    {

                        // stdOut.Debug("Data Sent: " + TBLConvert.BytesToHexString(pData)); // Ausgabe zum Bibliotheksdebuggen

                        readTimeoutReached = false;
                        watchDog.Enabled = true;					// Starte für Timeout

                        // hier komm ich her...

                        while(recBuffer.DataPtr < (ptrBefore+protocol.MinimumFrameLength) && !readTimeoutReached)  // wait until dataptr points to Place BEHIND last Databyte
                          {
                          		// wait
                          }

                        watchDog.Enabled = false; // watchdog aus...

                        if(!readTimeoutReached)
                        {
                            byte[] received = recBuffer.readBytes(ptrBefore, ptrBefore + (protocol.AcknowledgeFrameLength -1)); // read Frame that is supposed to be Acknowledge frame..

                            // Check if Acknowledge
                            try
                            {
                                if(protocol.IsAcknowledge(vFrame.GetDataframe(), received, out error))
                                {
                                    sendSuccesful = true;

                                    recBuffer.FreeBytes(ptrBefore, ptrBefore + received.Length-1);
                                    recBuffer.Flush();
                                }
                                else
                                {
                                    if(error == -36)
                                    {
                                        return(error); // Explizites NAK erhalten...
                                    }

                                    sendSuccesful = false;
                                    int err;

                                    if(!Reset(out err))
                                    {
                                        return(err);
                                    }

                                    // send again, probably log error?!
                                    // ERROR: not Acknowledge, Log?
                                }
                            }
                            catch(Exception e) // Frame error in irgend einer Weise...
                            {
                                // getLastError hat die FEhlernummer der Exception inna..
                                if(debugMode)
                                {
                                    stdOut.Error("Intern TBL.Communication.devCom.writeFrame(byte[]), Acknowledge auswerten: " + Environment.NewLine + EDOLLHandler.GetLastError(), e.ToString());
                                }

                            }
                        }
                        else
                        {
                            // Error: readtimeout reached
                            if(debugMode)
                            {
                                stdOut.Debug("Readtimeout reached @ " + watchDog.Interval.ToString() + " ms!! Buffer contains: " + recBuffer.ToString());

                            }
                        }
                    }
                }

                if(!sendSuccesful)
                {
                    if(error == 0)
                    {
                        error =  -203; // Timeout
                    }

                    return(error);
                }
                else
                {
                    return(0);
                }
            }
            else
            {
                // TODO: Fehlernummer "unavailable"
                return(-555); // ruhig weiterlaufen lassen, sonst werden dauernd fehler ausgegeben... Da wird getäuscht!!
            }
        }
Exemple #5
0
        private bool sendCommandReadAnswer(IM3S_Dataframe pCmdFrame, out byte[] oAnswer, out int oErrorCode)
        {
            oErrorCode=-555;
            oAnswer = null;
            bool acknowledged = !pCmdFrame.NeedsAcknowledgement; // wenn kein acknowledge benötigt wird, von vornherein acknowledged..

            int ptrBefore = recBuffer.DataPtr;

            if(available)
            {
                if(interferenceHandling)
                {
                    recBuffer.HandleGarbage();
                }

                for(int i= 0; (i < tryXTimes); i++)
                {
                    oErrorCode = sendToHardware(pCmdFrame.GetDataframe());

                    if( oErrorCode == 0) // wenn erfolgreich gesendet wurde
                    {
                        // stdOut.Debug("Data Sent: " + TBLConvert.BytesToHexString(pData)); // Ausgabe zum Bibliotheksdebuggen

                        readTimeoutReached = false;
                        watchDog.Enabled = true;					// Starte für Timeout

                        // hier komm ich her...

                        while(!readTimeoutReached)  // wait until dataptr points to Place BEHIND last Databyte
                          {
                            if(recBuffer.DataPtr >= (ptrBefore+protocol.MinimumFrameLength))
                            {
                                byte[] received = recBuffer.readBytes(ptrBefore, recBuffer.DataPtr-1); // read Frame that is supposed to be Acknowledge frame..

                                if(received.Length == protocol.AcknowledgeFrameLength)
                                {
                                    if(protocol.IsAcknowledge(pCmdFrame.GetDataframe(),received, out oErrorCode))
                                    {
                                        acknowledged = true;
                                        watchDog.Enabled = false;
                                        watchDog.Enabled = true;
                                        acknowledged = true;
                                        ptrBefore += protocol.AcknowledgeFrameLength; // offset erhöhen
                                    }
                                    else
                                    {
                                        if(oErrorCode == -36)
                                        {
                                            // Explizites NAK
                                            return(false);
                                        }
                                    }
                                }
                                else
                                {
                                    if(protocol.IsFrame(received))
                                    {
                                        if(acknowledged || protocol.IsImplicitAcknowledge(pCmdFrame.GetDataframe(), received, out oErrorCode))
                                        {
                                            oAnswer = received;
                                            oErrorCode = 0;
                                            recBuffer.FreeBytes(ptrBefore, recBuffer.DataPtr-1);
                                            recBuffer.Flush();
                                            return(true);		// FOUND!!
                                        }
                                        else
                                        {
                                            if(oErrorCode == -37)
                                            {
                                                // Explizites NAKimplizit
                                                return(false);
                                            }
                                        }
                                    }
                                }
                            }
                          }

                        watchDog.Enabled = false; // watchdog aus...

                        if(readTimeoutReached)
                        {
                            if(debugMode)
                            {
                                stdOut.Debug("Readtimeout reached @ " + watchDog.Interval.ToString() + " ms!! Buffer contains: " + recBuffer.ToString());
                            }

                            recBuffer.FreeBytes(ptrBefore, recBuffer.DataPtr-1);
                            recBuffer.Flush();

                            Reset(out oErrorCode);

                            if(oErrorCode != 0)			// RESET ist schiefgegangen
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            throw new Exception("internal sendCommandReadAnswer: This case should not be reached... Please contact Author of Library");
                        }
                    }
                }

                if(oErrorCode == 0)
                {
                    ptrBefore += protocol.AcknowledgeFrameLength;

                    if(debugMode)
                    {
                        stdOut.Debug("Command successfully sent, got acknowledged by slave @ internal sendCommandReadAnswer(byte[]) .. waiting for response...");
                    }

                    readTimeoutReached = false;
                    watchDog.Enabled = true;

                    while(!readTimeoutReached)
                    {
                        int bufferEnd = recBuffer.DataPtr;
                        int possFrameStart = ptrBefore;

                        if((bufferEnd - ptrBefore) >= protocol.MinimumFrameLength)
                        {
                            //starte suche#
                            int possUpperBound;
                            int possFrameEnd;

                            while(possFrameStart + protocol.MinimumFrameLength <= recBuffer.DataPtr)
                            {
                                possUpperBound = recBuffer.ReadByte(possFrameStart + protocol.UpperBoundPosition);
                                possFrameEnd = possFrameStart+protocol.Overhead + possUpperBound;

                                byte[] possFrame = recBuffer.readBytes(possFrameStart, possFrameEnd);

                                if(protocol.IsFrame(possFrame))
                                {
                                    watchDog.Enabled = false;

                                    if(debugMode)
                                    {
                                        stdOut.Debug("Response frame ("+(possFrameEnd-possFrameStart).ToString() +" bytes) found in receive buffer (holding " + recBuffer.DataPtr.ToString() + " bytes); Byte " + possFrameStart.ToString() + " to " +  (possFrameEnd).ToString() + " .. Removing them from ReceiveBuffer (Free and Flush)");
                                    }

                                    recBuffer.FreeBytes(possFrameStart,possFrameEnd);
                                    recBuffer.Flush(); // Buffer leeren

                                    oErrorCode =0;
                                    oAnswer = possFrame;
                                    return(true);
                                }
                                // else
                                possFrameStart++; // eines weiter hinten starten
                            }
                        }
                    }

                    // Watchdog hat gezogen

                    readTimeoutReached = false; // rücksetzen
                    oErrorCode=-217; // Watchdog gezogen...
                    oAnswer = null;
                    return(false);
                }

            }
            else
            {
                oErrorCode=-34;//not available
                oAnswer = null;
            }
            return(false);
        }
        public M3SCommandReceivedEventArgs(IM3S_Dataframe rReceivedFrame)
        {
            frame = rReceivedFrame;
            byte[] frameBytes = frame.GetDataframe();
            int upperBound = frameBytes[2]; // M3s-Upperbound

            cmd = frameBytes[3];
            paramBytes = new byte[upperBound];

            for(int i=0; i<upperBound; i++)
            {
                paramBytes[i] = frameBytes[i+4];
            }
        }
 private int writeToHardware(IM3S_Dataframe frameToWrite)
 {
     ackAlreadySent = true;
     somethingToSend = true;
     return(hwInterface.WriteData(frameToWrite.GetDataframe()));
 }
        private bool processCommand(IM3S_Dataframe rDataFrame, out int oErrorCode)
        {
            oErrorCode = -555;
            byte[] payload = protocol.ExtractPayload(rDataFrame.GetDataframe());

            byte cmd = payload[0];

            switch(cmd)
            {
                   case (byte)M3SCommand.GetInformation:
                        oErrorCode = sendSlaveInfo(rDataFrame);
                        break;

                    case (byte)M3SCommand.Ping:
                        oErrorCode = 0; // no error, ack is sent outside...
                        cnt++;

                        break;

                        default:

                            if(delegateCommand !=null)
                            {
                                return delegateCommand(this, new M3SCommandReceivedEventArgs(rDataFrame));
                            }
                            return(false);
            }

            if(oErrorCode==0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }