HuangBean 2021-10-10 20:44 采纳率: 100%
浏览 32
已结题

MFC程序能编译成功,但运行时点击按钮后提示参数错误(如图),代码已经贴出来了,是哪里出问题了吗?

class FX3U {
private:
    char readData[11];
    char writeData[11];
    char SetBit[9];
    char ResetBit[9];
    char* temp;
    int ByteLong;
    const int MBA = 0x800;
    const int XBA = 0x400;
    const int YBA = 0x500;
    int tempBA;
    void init();
    short getTempSum();
public:
    CString GetSendText(int RWType, char BAType, int Base_AddRess);
};

CString FX3U::GetSendText(int RWType,char BAType,int Base_AddRess) {
        /*    RWType:    =1    读取数据
                    =2    写入数据
                    =3    强制ON
                    =4    强制OFF
            BAType:    ='X'、'Y'、'M'、'D'
            Base_AddRess:    =0-97
        */
    init();
    int sum;
    CString ret;
    char *strBAc=new char[5];
    memset(strBAc,0,5);
    char* base_address = new char[4];
    int baseAddRessH = Base_AddRess / 10;
    int baseAddRessL = Base_AddRess % 10;
    
    if (baseAddRessL>7) {
        return _T("UNKNOW");
    }
    
    switch (BAType) {                //计算基址
    case 'X':tempBA = XBA + (baseAddRessH * 8) + baseAddRessL; break;
    case 'Y':tempBA = YBA + (baseAddRessH * 8) + baseAddRessL; break;
    case 'M':tempBA = MBA + (baseAddRessH * 8) + baseAddRessL; break;
    default:return _T("UNKNOW");
    }

    sprintf(strBAc, "%04X", tempBA);            //将基址拆分成4个十六进制数

    switch (RWType) {                //生成通讯报文
    case 1:
        ByteLong = 11; 
        temp = new char[11]; 
        temp[1] = 0x30; 
        temp[2] = strBAc[3];
        temp[3] = strBAc[2];
        temp[4] = strBAc[1];
        temp[5] = strBAc[0];
        temp[6] = 0x30;
        temp[7] = 0x31;
        temp[8] = 0x03;
        sum = getTempSum();            //计算校验和
        if (sum>0xFF) {
            sum -= 0x100;
            temp[9] = sum>>4;        //取高4位
            temp[10] = (sum << 4) >> 4;        //取低4位
        }
        else if (sum<=0xFF) {
            temp[9] = sum >> 4;
            temp[10] = (sum << 4) >> 4;
        }
        break;
    case 2:
        ByteLong = 11; 
        temp = new char[11]; 
        temp[1] = 0x31;
        temp[2] = strBAc[3];
        temp[3] = strBAc[2];
        temp[4] = strBAc[1];
        temp[5] = strBAc[0];
        temp[6] = 0x30;
        temp[7] = 0x31;
        temp[8] = 0x03;
        sum = getTempSum();
        if (sum > 0xFF) {
            sum -= 0x100;
            temp[9] = sum >> 4;
            temp[10] = (sum << 4) >> 4;
        }
        else if (sum <= 0xFF) {
            temp[9] = sum >> 4;
            temp[10] = (sum << 4) >> 4;
        }
        break;
    case 3:
        ByteLong = 9; 
        temp = new char[9]; 
        temp[1] = 0x37;
        temp[2] = strBAc[2];
        temp[3] = strBAc[3];
        temp[4] = strBAc[0];
        temp[5] = strBAc[1];
        temp[6] = 0x03;
        sum = getTempSum();
        if (sum > 0xFF) {
            sum -= 0x100;
            temp[7] = sum >> 4;
            temp[8] = (sum << 4) >> 4;
        }
        else if (sum <= 0xFF) {
            temp[7] = sum >> 4;
            temp[8] = (sum << 4) >> 4;
        }
        break;
    case 4:
        ByteLong = 9; 
        temp = new char[9]; 
        temp[1] = 0x38; 
        temp[2] = strBAc[2];
        temp[3] = strBAc[3];
        temp[4] = strBAc[0];
        temp[5] = strBAc[1];
        temp[6] = 0x03;
        sum = getTempSum();
        if (sum > 0xFF) {
            sum -= 0x100;
            temp[7] = sum >> 4;
            temp[8] = (sum << 4) >> 4;
        }
        else if (sum <= 0xFF) {
            temp[7] = sum >> 4;
            temp[8] = (sum << 4) >> 4;
        }
        break;
    default:return _T("UNKNOW");
    }

    temp[0] = 0x02;
    
    for (int i = 0; i < ByteLong;i++) {            //将char*类型的报文逐字赋值给CString类型的变量
        ret.SetAt(i,temp[i]);
    }
    
    return ret;
}

//以下是按钮事件函数:
void CCOMM1Dlg::OnBnClickedButtonTest()
{
    // TODO: 在此添加控件通知处理程序代码
    FX3U fx3u;
    CString sendText;
    sendText=fx3u.GetSendText(3,'Y',0);
    //m_CtrlComm.put_Output(COleVariant(sendText));
    m_StrSend = sendText;
    UpdateData(false);
}

img

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-10-10 20:50
    关注

    m_StrSend是什麽东西。你可以把154、155行先注释掉再测试一下是否报错

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月23日
  • 已采纳回答 10月15日
  • 创建了问题 10月10日

悬赏问题

  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM