private serialPck rxPck_ = new serialPck(); // temp. Package to handle incomming bytes #endregion Fields #region Methods public char[] BuildMessage(byte Message) { int i = 0; char[] package = new char[5]; serialPck message = new serialPck(); message.ID += IDMESSAGE; message.Size = 1; message.Bytes[0] = Message; package[i++] = (char)STARTFLAG; package[i++] = (char)message.ID; package[i++] = (char)message.Bytes[0]; package[i++] = (char)(message.ID + message.Bytes[0]); //CheckSum package[i++] = (char)STOPFLAG; return package; }
public char[] BuildMessage(byte Message) { int i = 0; char[] package = new char[5]; serialPck message = new serialPck(); message.ID += IDMESSAGE; message.Size = 1; message.Bytes[0] = Message; package[i++] = (char)STARTFLAG; package[i++] = (char)message.ID; package[i++] = (char)message.Bytes[0]; package[i++] = (char)(message.ID + message.Bytes[0]); //CheckSum package[i++] = (char)STOPFLAG; return(package); }
public int ByteImport(byte newByte) { if (!((rxPck_.Flags & PCKCMPL) != 0) // it's a new package && !((rxPck_.ID & IDMESSAGE) != 0) // it's data package && ((rxPck_.ID & IDSIZE) != 0) // it's a package with size info && (rxPck_.SizeCnt < rxPck_.Size)) // it's not the the stop flag { if ((rxPck_.Flags & PCKSIZE) != 0) // the size is received { rxPck_.Bytes[rxPck_.SizeCnt++] = newByte; } else { rxPck_.Size = (byte)(newByte - 4); rxPck_.Flags += PCKSIZE; return PCKSIZE; } return 0; } switch (newByte) { case STARTFLAG: // always do (STARTFLAG) rxPck_.ID = 0; // clear IDenntity Byte rxPck_.Size = 1; rxPck_.SizeCnt = 0; // reset DataBuffer rxPck_.ChkSum = 0; if ((rxPck_.Flags & PCKCMPL) != 0) // prev. Pack. complete ? { rxPck_.Flags = 0; // reset Command Flags } else { // no -> StopError rxPck_.Flags = 0; // reset Command Flags return PERRSTOP; } break; case ESCFLAG: if (((rxPck_.Flags & PCKCMPL) != 0) || ((rxPck_.Flags & PCKESC) != 0) ) { rxPck_.Flags |= PERRCMD;// error return PERRCMD; } else rxPck_.Flags |= PCKESC; // set EscapeFlag break; case STOPFLAG: if (((rxPck_.Flags & PCKCMPL) != 0) || (rxPck_.SizeCnt < 1)) { rxPck_.Flags |= PERRSTART; // missing StartFlag return PERRSTART; } else { rxPck_.Flags |= PCKCMPL; // set StopFlag if ((rxPck_.ID & IDSIZE) != 0) //if stop and package with size info { if (rxPck_.SizeCnt == rxPck_.Size) { inPck_ = rxPck_; return PCKCMPL; } else { rxPck_.Flags |= PERRCHKSUM; // CheckSumError return PERRCHKSUM; } } rxPck_.ChkSum = rxPck_.ID; // and calc. CHKSUM for (int j = 0; j < (rxPck_.SizeCnt - 1); j++) if ((rxPck_.Bytes[j] == STARTFLAG) || (rxPck_.Bytes[j] == STOPFLAG) || (rxPck_.Bytes[j] == ESCFLAG) ) rxPck_.ChkSum += (byte)(rxPck_.Bytes[j] & ~ESCMASK); else rxPck_.ChkSum += rxPck_.Bytes[j]; if (rxPck_.ChkSum != rxPck_.Bytes[--rxPck_.SizeCnt]) { // remove CHKSUM rxPck_.Flags |= PERRCHKSUM; // CheckSumError return PERRCHKSUM; } else { rxPck_.Size = rxPck_.SizeCnt; inPck_ = rxPck_; return PCKCMPL; } }//else (!missing startflag) // break; default: if ((rxPck_.Flags & PCKCMPL) != 0) { // StartError rxPck_.Flags |= PERRSTART; // (missing flag) return PERRSTART; } if (!(rxPck_.Flags != 0)) // new transmission { // -> this is PackageID rxPck_.Flags = PCKID; // set flag rxPck_.ID = newByte; return PCKID; } else // running transmisssion { //return PERRSTART; try { rxPck_.Bytes[(++rxPck_.SizeCnt) - 1] = newByte; } /* TODO : ??? ++C -1 ??? */ catch { return PERRCHKSUM; } if ((rxPck_.Flags & PCKESC) != 0) // check EscapeFlag { rxPck_.Bytes[rxPck_.SizeCnt - 1] |= ESCMASK; rxPck_.Flags = (byte)(rxPck_.Flags & ~PCKESC);// clear EscapeFlag } } break;// default }//switch (newByte) return 0; }
public bool GetPck(ref serialPck inPackage) { inPackage = inPck_; return true; }
public int ByteImport(byte newByte) { if (!((rxPck_.Flags & PCKCMPL) != 0) && // it's a new package !((rxPck_.ID & IDMESSAGE) != 0) && // it's data package ((rxPck_.ID & IDSIZE) != 0) && // it's a package with size info (rxPck_.SizeCnt < rxPck_.Size)) // it's not the the stop flag { if ((rxPck_.Flags & PCKSIZE) != 0) // the size is received { rxPck_.Bytes[rxPck_.SizeCnt++] = newByte; } else { rxPck_.Size = (byte)(newByte - 4); rxPck_.Flags += PCKSIZE; return(PCKSIZE); } return(0); } switch (newByte) { case STARTFLAG: // always do (STARTFLAG) rxPck_.ID = 0; // clear IDenntity Byte rxPck_.Size = 1; rxPck_.SizeCnt = 0; // reset DataBuffer rxPck_.ChkSum = 0; if ((rxPck_.Flags & PCKCMPL) != 0) // prev. Pack. complete ? { rxPck_.Flags = 0; // reset Command Flags } else { // no -> StopError rxPck_.Flags = 0; // reset Command Flags return(PERRSTOP); } break; case ESCFLAG: if (((rxPck_.Flags & PCKCMPL) != 0) || ((rxPck_.Flags & PCKESC) != 0)) { rxPck_.Flags |= PERRCMD; // error return(PERRCMD); } else { rxPck_.Flags |= PCKESC; // set EscapeFlag } break; case STOPFLAG: if (((rxPck_.Flags & PCKCMPL) != 0) || (rxPck_.SizeCnt < 1)) { rxPck_.Flags |= PERRSTART; // missing StartFlag return(PERRSTART); } else { rxPck_.Flags |= PCKCMPL; // set StopFlag if ((rxPck_.ID & IDSIZE) != 0) //if stop and package with size info { if (rxPck_.SizeCnt == rxPck_.Size) { inPck_ = rxPck_; return(PCKCMPL); } else { rxPck_.Flags |= PERRCHKSUM; // CheckSumError return(PERRCHKSUM); } } rxPck_.ChkSum = rxPck_.ID; // and calc. CHKSUM for (int j = 0; j < (rxPck_.SizeCnt - 1); j++) { if ((rxPck_.Bytes[j] == STARTFLAG) || (rxPck_.Bytes[j] == STOPFLAG) || (rxPck_.Bytes[j] == ESCFLAG) ) { rxPck_.ChkSum += (byte)(rxPck_.Bytes[j] & ~ESCMASK); } else { rxPck_.ChkSum += rxPck_.Bytes[j]; } } if (rxPck_.ChkSum != rxPck_.Bytes[--rxPck_.SizeCnt]) { // remove CHKSUM rxPck_.Flags |= PERRCHKSUM; // CheckSumError return(PERRCHKSUM); } else { rxPck_.Size = rxPck_.SizeCnt; inPck_ = rxPck_; return(PCKCMPL); } } //else (!missing startflag) // break; default: if ((rxPck_.Flags & PCKCMPL) != 0) { // StartError rxPck_.Flags |= PERRSTART; // (missing flag) return(PERRSTART); } if (!(rxPck_.Flags != 0)) // new transmission { // -> this is PackageID rxPck_.Flags = PCKID; // set flag rxPck_.ID = newByte; return(PCKID); } else // running transmisssion { //return PERRSTART; try { rxPck_.Bytes[(++rxPck_.SizeCnt) - 1] = newByte; } /* TODO : ??? ++C -1 ??? */ catch { return(PERRCHKSUM); } if ((rxPck_.Flags & PCKESC) != 0) // check EscapeFlag { rxPck_.Bytes[rxPck_.SizeCnt - 1] |= ESCMASK; rxPck_.Flags = (byte)(rxPck_.Flags & ~PCKESC); // clear EscapeFlag } } break; // default }//switch (newByte) return(0); }
public bool GetPck(ref serialPck inPackage) { inPackage = inPck_; return(true); }