64,662
社区成员
发帖
与我相关
我的任务
分享
//串口发送数据函数
void CComDiagDlg::SendData(CString strInput)
{
CString strTemp,strClip,strTemp1;
int iLen,i,iCount;
char charInput[100];
BYTE arrSendData[100],byte1[1],byte2[1];
CByteArray Array1;
//strInput+= '\0';
strcpy(charInput,strInput);
iLen=strInput.GetLength();
iCount=0;
for(i=0;i<iLen;i++)
{
switch(charInput[i])
{
case 48: //字符0
case 49: //字符1
case 50: //字符2
case 51: //字符3
case 52: //字符4
case 53: //字符5
case 54: //字符6
case 55: //字符7
case 56: //字符8
case 57: //字符9
case 65: //字符A
case 66: //字符B
case 67: //字符C
case 68: //字符D
case 69: //字符E
case 70: //字符F
{
strTemp+=charInput[i];
if((i+1)%2==0)
{ //将字符串转换为数值
arrSendData[iCount]=(BYTE)strtoul(strTemp,0,16); //strtol
iCount ++;
strTemp="";
}
break;
}
default: break; //其它字符
}
}
Array1.RemoveAll();
Array1.SetSize(iCount);
for(i=0;i<iCount;)
{
Array1.SetAt(i,arrSendData[i]);
i ++;
}
if(Array1.GetSize()>0)
{
myComm.SetOutput(COleVariant(Array1)); //发送数据
}
}
void CComDiagDlg::SendData(CString strInput)
{
CString strTemp,strClip,strTemp1;
int iLen,i,iCount;
char charInput[100];
BYTE arrSendData[100];
CByteArray Array1;
strInput+=",";
strcpy(charInput,strInput);
iLen=strInput.GetLength();
iCount=0;
for(i=0;i<iLen;i++)
{
switch(charInput[i])
{
case 48: //字符0
case 49: //字符1
case 50: //字符2
case 51: //字符3
case 52: //字符4
case 53: //字符5
case 54: //字符6
case 55: //字符7
case 56: //字符8
case 57: //字符9
case 65: //字符A
case 66: //字符B
case 67: //字符C
case 68: //字符D
case 69: //字符E
case 70: //字符F
{
strTemp+=charInput[i];
break;
}
default: //其它字符
{
if(strTemp!="")
{
//将字符串转换为数值
arrSendData[iCount]=(BYTE)strtol(strTemp,0,16);
iCount++;
strTemp="";
}
break;
}
}
}
Array1.RemoveAll();
Array1.SetSize(iCount);
for(i=0;i<iCount;i++)
{
Array1.SetAt(i,arrSendData[i]);
}
if(Array1.GetSize()>0)
{
myComm.SetOutput(COleVariant(Array1)); //发送数据
}
}