我的这段程序是要根据Edit控件里输入的内容,从而判断向串口输出命令字符,但是我自编的一个识别并获得命令字符的函数GetAtten好像没起作用,无论输入什么数字,它最后送给串口的都是AA 01 00;帮我看看是哪里出了问题?多谢多谢!
void __fastcall TPagesDlg::chan1KeyPress(TObject *Sender, char &Key)
{
if(Key==13)
{
iChan1=StrToInt(chan1->Text);
if(iChan1<0 || iChan1>80) ShowMessage("衰减量输入错误,请从新输入");
char c;
GetAtten(iChan1,c);
cChan1[2]=c;
Form1->YbCommDevice1->Write(cChan1,3);//cChan1[0]=0xAA;cChan1[1]=0x01;这两个都是固定不变的
}
}
char TPagesDlg::GetAtten(int a,char b)
{
switch (a)
{
case 0: b=0x01; break;
case 1: b=0x02; break;
case 2: b=0x05; break;
case 3: b=0x08; break;
case 4: b=0x0B; break;
case 5: b=0x0C; break;
case 6: b=0x10; break;
case 7: b=0x12; break;
case 8: b=0x15; break;
case 9: b=0x18; break;
case 10: b=0x1B; break;
case 11: b=0x1C; break;
case 12: b=0x20; break;
case 13: b=0x22; break;
case 14: b=0x25; break;
case 15: b=0x28; break;
case 16: b=0x2B; break;
case 17: b=0x2C; break;
case 18: b=0x30; break;
case 19: b=0x32; break;
case 20: b=0x35; break;
case 21: b=0x38; break;
case 22: b=0x3B; break;
case 23: b=0x3C; break;
case 24: b=0x40; break;
case 25: b=0x42; break;
case 26: b=0x45; break;
case 27: b=0x48; break;
case 28: b=0x4B; break;
case 29: b=0x4C; break;
case 30: b=0x50; break;
case 31: b=0x52; break;
case 32: b=0x55; break;
case 33: b=0x58; break;
case 34: b=0x5B; break;
case 35: b=0x5C; break;
case 36: b=0x60; break;
case 37: b=0x62; break;
case 38: b=0x65; break;
case 39: b=0x68; break;
case 40: b=0x6B; break;
case 41: b=0x6C; break;
case 42: b=0x70; break;
case 43: b=0x72; break;
case 44: b=0x75; break;
case 45: b=0x78; break;
case 46: b=0x7B; break;
case 47: b=0x7C; break;
case 48: b=0x80; break;
case 49: b=0x82; break;
case 50: b=0x85; break;
case 51: b=0x88; break;
case 52: b=0x8B; break;
case 53: b=0x8C; break;
case 54: b=0x90; break;
case 55: b=0x92; break;
case 56: b=0x95; break;
case 57: b=0x98; break;
case 58: b=0x9B; break;
case 59: b=0x9C; break;
case 60: b=0xA0; break;
case 61: b=0xA2; break;
case 62: b=0xA5; break;
case 63: b=0xA8; break;
case 64: b=0xAB; break;
case 65: b=0xAC; break;
case 66: b=0xB0; break;
case 67: b=0xB2; break;
case 68: b=0xB5; break;
case 69: b=0xB8; break;
case 70: b=0xBB; break;
case 71: b=0xBC; break;
case 72: b=0xC0; break;
case 73: b=0xC2; break;
case 74: b=0xC5; break;
case 75: b=0xC8; break;
case 76: b=0xCB; break;
case 77: b=0xCC; break;
case 78: b=0xD0; break;
case 79: b=0xD2; break;
case 80: b=0xD5; break;
}
return b;
}
--------------------next---------------------
char TPagesDlg::GetAtten(int a,char b)
这个函数的参数 b 按照你的意思我理解:
char c;
GetAtten(iChan1,c); //我理解你是想让函数 GetAtten 通过参数 b 来修改 c 的值,让 c 等于函数里面 case 赋的值
但是 C 语言的函数的普通参数是局部变量,在函数里面无论怎样修改 b 的值,也不会改变外面的 c 的值,
这样调用函数时是把 c 的值赋值给参数 b, b 是复制的 c 的内容,函数返回时 b 的内容就丢失了
参数可以向函数外传递值的有两种情况:
1.引用:
char TPagesDlg::GetAtten(int a,char &b) //这个 b 是引用
char c;
GetAtten(iChan1,c); //在函数里面的 b 就是函数外面的 c, 参数 b 是变量 c 的引用,如果改变了 b 的值,就是改变了 c 的值
2.指针
char TPagesDlg::GetAtten(int a,char *b) //b 是指针(地址型变量)
{
switch (a)
{
case 0: *b=0x01; break; //地址 b 里面的数等于 0x01, 如果 b 是 c 的地址,就是改变 c 的值了
case 1: *b=0x02; break;
...
}
char c;
GetAtten(iChan1,&c); //这个&符号是取地址,不是上面的引用了
--------------------next---------------------
阅读(1248) | 评论(0) | 转发(0) |