none
run-time check failure #2 stack around the variable 'reciveByte' was corrupted RRS feed

  • 问题

  • // TEST.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include "stdlib.h"


    /// <summary>
    /// 打包发送帧
    /// </summary>
    /// <param name="packetType">帧类型</param>
    /// <param name="stationNum">站号</param>
    /// <param name="length">命令帧长度:命令数据的长度+2</param>
    /// <param name="commandCode">命令码</param>
    /// <param name="commandData">命令数据</param>
    /// <returns>打包后可发送的数据帧</returns>
    void PacketingSendFrame(char packetType, char stationNum, char length, char commandCode, char* commandData, char* packetArray)
    {
     char checkSum = 0x00;
        char _sendPacketType = (char)packetType;
        char _sendStationNum = stationNum;
        char _sendLength = length;
        char _sendCommandCode = commandCode;
        char *_sendCommandData = commandData;
     char _sendCheckSum;

        packetArray[0] = _sendPacketType;
        packetArray[1] = _sendStationNum;
        packetArray[2] = _sendLength;
        packetArray[3] = _sendCommandCode;
        checkSum = (char)(packetArray[0] + packetArray[1] + packetArray[2] + packetArray[3]);
     int i = length - 3;
        while (i >= 0)
     {
      packetArray[4 + i] = _sendCommandData[i];
      checkSum += _sendCommandData[i];
      i--;
     }
     checkSum = (char)((checkSum ^ 0xff) + 1);
     _sendCheckSum = checkSum;
     packetArray[3 + _sendLength - 1] = checkSum;
    }

    ///接收过程中状态枚举
    enum FramereadStatus
    {
     HEADER,   //帧头
     STATIONUM,    //站号
     LENGTH,  //长度
     CMD,    //命令
     DATA,   //数据
     CHECK   //校验
    };
    ///帧头类型枚举
    enum FramereadHeader
    {
     /// <summary>
     /// 命令帧
     /// </summary>
     Fcmd = 0xA5,   //命令帧
     /// <summary>
     /// 响应帧
     /// </summary>
     Fact = 0xE5,    //响应帧
     /// <summary>
     /// 完成帧
     /// </summary>
     Fcmp = 0xE9     //完成帧
    };

    /// <summary>
    /// 从串口获取响应帧/完成帧,并进行解译
    /// </summary>
    /// <param name="packetType">获取帧类型</param>
    /// <param name="stationNum">获取帧站号</param>
    /// <param name="length">获取帧长度</param>
    /// <param name="commandCode">获取帧指令码</param>
    /// <param name="commandData">获取帧指令数据</param>
    /// <returns>获取帧完成并且校验通过为false,反之则为true</returns>
    bool UnpacketingGetFrame(char *packetType, char *stationNum,char *length, char *commandCode, char *commandData)
    {
        char reciveByte;       //接收的字节数据
     char getCheckSum = 0x00;            //数据帧校验计算
     bool _reciveCompleteFlag = false;    //接收到校验帧后为true
        char _reciveCommandDataCount = 0;     //接收指令数据计数器
     char _getCheckSum = 0x00;    //接收帧校验
     FramereadStatus _frameGetStatus = HEADER;   //帧读取状态

     do
     {
      //reciveByte = (Byte)serialPort.ReadByte(); //读取一个字节
         /// 测试代码
      scanf("%x",&reciveByte);
      ///测试代码结束
      switch (_frameGetStatus)
      {
       case HEADER:    //当前读取状态为读取帧头
       {
        if (reciveByte == (char)Fact || reciveByte == (char)Fcmp)
        {
         *packetType = reciveByte;    //更新包类型
         getCheckSum = reciveByte; //计算校验数据
         _frameGetStatus = STATIONUM;    //跟新帧读取状态
        }
       } break;
       case STATIONUM: //当前状态为读取帧站号
       {
        *stationNum = reciveByte;   //跟新站号
        getCheckSum += reciveByte;    //计算校验数据
        _frameGetStatus = LENGTH;  //跟新帧读取状态
       } break;
       case LENGTH:    //当前状态为读取指令数据长度
       {
        *length = reciveByte;    //跟新指令数据长度
        getCheckSum += reciveByte;    //计算校验数据
        _frameGetStatus = CMD; //跟新帧读取状态
       } break;
       case CMD:   //当前状态为读取指令码
       {
        *commandCode = reciveByte;   //跟新指令码
        getCheckSum += reciveByte;    //计算校验数据
        _frameGetStatus = DATA; //跟新帧读取状态
       } break;
       case DATA://当前状态为读取指令数据
       {
        commandData[_reciveCommandDataCount] = reciveByte;   //跟新指令数据
        getCheckSum += reciveByte;    //计算校验数据
        _reciveCommandDataCount++;
        if (_reciveCommandDataCount == *length - 2)
         _frameGetStatus = CHECK;   //跟新帧读取状态
       } break;
       case CHECK: //当前状态为读取校验数据
       {
        _getCheckSum = reciveByte; //更新校验数据
        getCheckSum = (char)((getCheckSum ^ 0xff) + 1); //更新校验数据
        _reciveCompleteFlag = true;
       } break;
      }
     } while (!_reciveCompleteFlag);
     if (_getCheckSum == getCheckSum)    //帧接收完成,比较帧校验成功
     {
      //返回正确
      return false;
     }
     else     //帧校验失败
     {
      packetType = 0x00;
      stationNum = 0x00;
      length = 0x00;
      commandCode = 0x00;
      commandData = NULL;
      //返回错误
      return true;
     }
    }    //这里提示以上错误

    int _tmain(int argc, _TCHAR* argv[])
    {
        int i = 0,j=0;
     //test1
     char arraydata[9];
     char data[]={0x11,0x12,0x32,0x31};
     //test2
     char packetType;
     char stationNum;
     char length = 0x00;
     char commandCode;
     char commandData[4];
     
     //test1
     PacketingSendFrame(0xE5,0x00,0x06,0x71,data,arraydata);
     printf("Hello World\n");
     for(i = 0;i<9;i++)
     {
      printf("%02X  ",arraydata[i]);
     }
     printf("\n");

     //test2
     UnpacketingGetFrame(&packetType, &stationNum, &length, &commandCode, commandData);    //函数调用返回时报错
     //{
     // //printf("test");
     // //for(j = 0;j<4;j++)
     // //{
     // // //printf("%02X  ",commandData[j]);
     // // printf("%d",j);
     // //}
     //}
     printf("hello world");
     return 0;
    }


    • 已编辑 smzcfsj 2012年9月15日 14:32
    2012年9月15日 14:19

答案

  • Run-Time   Check   Failure   #2    
    一般是栈被破坏,您的代码可能有缓冲区溢出一类的问题,比如数组下标越界。

    另外您代码中的if (reciveByte == (char)Fact || reciveByte == (char)Fcmp)语句存在一点问题
    Fact和Fcmp的枚举值都大于127,而char的范围是在-128 ~ 127之间。


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2012年9月15日 14:37
    版主