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

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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题