背景:作業原因剛接觸C++ Builer,之前看過一點點c的基礎,代碼是上一個開發者做的,他沒有使用串口控制元件,而是自己做了一個通信模塊,可支持telnet和serialport,他做的串口發送好像只支持字串發送,目前我這邊的新需求是發送十六進制陣列(應該是這個意思),不知道怎么修改,如下:
我使用串口除錯助手發送的資料是:02 02 11 11 03 03

我想在代碼里邊也實作下發“02 02 11 11 03 03”這個十六進制的陣列?不知道該怎么修改,特此請教各位,先在此感謝~
在此奉上我所有的分,雖然積分不多

這個是發送的部分:
bool TEST_RunSend(CTestObj* pThis,AnsiString str)
{
pThis->m_asErrorMsg = "send_";
bool flag;
CRegex regex;
AnsiString asNewString;
std::vector<std::string> vecResult;
std::string strRegex;
std::string sendstr;
std::string strError;
AnsiString asTemp;
AnsiString asScan;
bool bReplaceIP = false;
//獲取是否替換命令中IP
asTemp = pThis->m_pConfig->GetValue(REPLACE_CMD_IP);
if (asTemp.IsEmpty())
{
bReplaceIP = false;
}
else
{
bReplaceIP = atoi(asTemp.c_str());
}
str = GetValue("send" , str);
//決議指令,去掉一些格式符
sendstr = fnParseString(pThis->m_pConfig,str).c_str();
if(sendstr == "NULL")
{
sendstr = "";
}
//替換命令中的IP--IPTS_QA_005
if (bReplaceIP)
{
AnsiString asRegex = "(\\d+\\.)(\\d+\\.)(\\d+\\.)(\\d+)";
asNewString.printf("\\1\\2\\3%d",pThis->m_stBrdInfo.ulTestBrdIndex);
regex.fnRegReplace(sendstr.c_str(),asRegex.c_str(),asNewString.c_str(),sendstr,strError);
}
if(sendstr == "InfoScan")
{
asScan = asScanInfo;
sendstr = "attd setmac " + std::string(asScan.c_str());
}
//發送指令
sendstr = fnChangeHex(sendstr.c_str()).c_str();
flag = pThis->m_communication.fnSendString(sendstr,500,FormMain->m_bSendDataByChar,strError);
這里是fnSendString函式功能
bool CCommunication::fnSendString(const std::string& strSend,const unsigned long ulTime,bool bSendByChar,std::string &strError)
{
WaitForSingleObject((HANDLE)m_hMutex,INFINITE);
std::string strSendAdd = strSend + "\r\n";
return m_pCommuBase->fnSendData(strSendAdd.c_str(),strSendAdd.size(),ulTime,strError);
strError = "通訊基類沒有創建";
ReleaseMutex((HANDLE)m_hMutex);
return false;
}
這里是fnSendData函式
bool CCommuSerial::fnSendData( const char* pbData,const unsigned long ulDataLen, const unsigned long ulTime,std::string &strError )
{
//呼叫回呼函式
if (0 != m_pfnCommCallBack)
{
m_pfnCommCallBack((unsigned long)pbData,m_ulCallBackParam2);
}
if (-1 != m_sComPort)
{
int iSend = sio_write(m_sComPort,(char*)pbData,ulDataLen);
if(0 >= iSend)
{
strError = "發送資料(";
strError += pbData ;
strError += ")失敗";
return false;
}
return true;
}
return false;
}
uj5u.com熱心網友回復:
extern PACKAGE AnsiString __fastcall IntToHex(int Value, int Digits);extern PACKAGE AnsiString __fastcall IntToHex(__int64 Value, int Digits);
Description
IntToHex converts a number into a string containing the number's hexadecimal (base 16) representation. Value is the number to convert. Digits indicates the minimum number of hexadecimal digits to return.
——————
AnsiString::printf
Sets the value of the AnsiString given a format string and its arguments.
int __cdecl printf(const char* format, ...);
Description
Use printf to set the value of the AnsiString given a standard C++ format specifier. Pass the values to any arguments in the format specifier as additional parameters following the format parameter. This method returns the length of the final formatted string.
——————
AnsiString __fastcall operator +(const AnsiString& rhs) const;
Description
The friend function returns an AnsiString that is the concatenation of strings lhs and rhs.
The method returns an AnsiString that is the concatenation of this AnsiString and rhs.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/100685.html
標籤:C#
