2 u011208984 u011208984 于 2013.08.01 11:11 提问

麻烦大家帮我详细解释下下面这段与串口交互的代码,谢谢!

void __fastcall TfrmPatient::edtDosageDblClick(TObject *Sender)
{
//read comm data,then set in edtDosage edit
//open com port
HANDLE hCom = CreateFile(g_ConfigManager.sComPort.c_str(),GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(hCom ==(HANDLE)-1)
{
ShowMessage("打开串口失败,请检查串口!");
return ;
}
//get comport state
DCB db;
db.DCBlength = sizeof(db);
bool bSuccess = GetCommState(hCom,&db);
if (!bSuccess)
{
ShowMessage("获取端口状态失败,端口被占用?");
}

db.BaudRate = CBR_9600;
db.ByteSize = 8;
db.Parity = NOPARITY;
db.StopBits = ONESTOPBIT;
 //set com state
bSuccess = SetCommState(hCom,&db);
if (!bSuccess)
{
    ShowMessage("设置端口失败!");
}
AnsiString sText ="@";
int nLen;
//write data to com port
bool bWriteStat = ::WriteFile(hCom,sText.c_str(),sText.Length(),(unsigned long*)&nLen,NULL);
if (!bWriteStat)
{
    ShowMessage("通讯失败,请检查串口设置是否正确!");
    return;
}

char* pReturn = new char[64];
nLen = 64;
memset(pReturn,0,sizeof(pReturn));
Sleep(1000);
//get the response data from com port
ReadFile(hCom,pReturn,nLen,(unsigned long*)&nLen,NULL);
AnsiString sReturn(pReturn);
// the device is OK
if(sReturn == "O")
{
    sText = "$CR1H#";
    bWriteStat = WriteFile(hCom,sText.c_str(),sText.Length(),(unsigned long*)&nLen,NULL);
    if(bWriteStat)
    {
        Sleep(1000);
        nLen = 64;
        memset(pReturn,0,sizeof(pReturn));
        //get the response data from com port
        bWriteStat = ReadFile(hCom,pReturn,nLen,(unsigned long*)&nLen,NULL);
        if(bWriteStat)
        {
            sReturn = pReturn;
            if(sReturn.Length() > 0)
            {
                //resolve the string
                int nIndex = sReturn.AnsiPos(" ");
                AnsiString str1 = sReturn.SubString(nIndex,sReturn.Length() - nIndex+1);
                str1 = str1.TrimLeft();
                //unit
                AnsiString strUnit = str1.SubString(str1.Length(),1);
                char cUnit = strUnit.operator [](1) ;
                //dosage
                str1 = str1.SubString(1,str1.Length() -1);
                switch(cUnit)
                {
                    case '0':
                        strUnit ="Over Range";//超出量程
                        break;
                    case '1':
                        strUnit = "uCi";
                        break;
                    case '2':
                        strUnit = "mCi";
                        break;
                    case '3':
                        strUnit = "Ci";
                        break;
                    case '4':
                        strUnit = "kBq";
                        break;
                    case '5':
                        strUnit = "MBq";
                        break;
                    case '6':
                        strUnit = "GBq";
                        break;
                    case '7':
                        strUnit = "mV";
                        break;
                    case '8':
                        strUnit = "V";
                        break;
                }
                edtDosage->Text = str1 +strUnit;

            }
        }
    }
}
CloseHandle(hCom);
delete []pReturn;

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!