回复 2楼 红色警戒
非常感谢你的回复。我的问题在光读取串口数据的时候,是没有上面的问题的,只有在点击保存开始按钮后才会出现这个问题。代码如下,谢谢:
程序代码:
void CMag_ComDlg::OnComm()
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
COleVariant myVar;
LONG len,k;
BYTE rxdata[3000]; //设置BYTE数组
CString strtemp,a,dataInit,point2;
int index_equ[8]={0,};
int index_n[3]={0,};
int index_g[3]={0,};
int ind_equ=0;
int ind_n=0;
int ind_g=0;
int ind_mx,ind_my,ind_mz,ind_gx,ind_gy,ind_gz;
char TT[150]={0,};
char magX_str[9],magY_str[9],magZ_str[9],accX_str[8],accY_str[8],accZ_str[8];
double magX,magY,magZ,accX,accY,accZ,mag_all;
static BOOL datastart=0;
if(m_com.GetCommEvent()==2) //事件值为2表示接收缓冲区内有字符
{
// variant_inp=m_com.GetInput(); //读缓冲区
// safearray_inp=variant_inp; //VARIANT型变量转换为ColeSafeArray型变量
myVar.Attach(m_com.GetInput());
//safearray_inp=variant_inp;
safearray_inp=myVar;
len=safearray_inp.GetOneDimSize(); //得到有效数据长度
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
for(k=0;k<len;k++) //将数组转换为Cstring型变量
{
BYTE bt=*(char*)(rxdata+k); //字符型
if(bt == 0x0d)
{
datastart=1;
}
if(datastart == 1)
{
strtemp.Format("%c",bt); //将字符送入临时变量strtemp存放
if(bt == 0x43)
{
datastart = 0;
int length=m_dataRead.GetLength();
dataInit = m_dataRead;
for(int j=0;j<length;j++)
TT[j]=dataInit.GetAt(j);
m_dataRead.Empty();
for(int f=0;f<length;f++)
{
if (TT[f]==0x3d)
{
index_equ[ind_equ]=f;
ind_equ++;
}
if(TT[f] == 0x6e)
{
index_n[ind_n]=f;
ind_n++;
}
if(TT[f] == 0x67)
{
index_g[ind_g]=f;
ind_g++;
}
}
ind_mx=index_n[0]-index_equ[0]-1;
ind_my=index_n[1]-index_equ[1]-1;
ind_mz=index_n[2]-index_equ[2]-1;
ind_gx=index_g[0]-index_equ[3]-1;
ind_gy=index_g[1]-index_equ[4]-1;
ind_gz=index_g[2]-index_equ[5]-1;
for(int imx=0;imx<ind_mx;imx++)
{
magX_str[imx]=TT[index_equ[0]+imx+1];
}
for(int imy=0;imy<ind_my;imy++)
{
magY_str[imy]=TT[index_equ[1]+imy+1];
}
for(int imz=0;imz<ind_mz;imz++)
{
magZ_str[imz]=TT[index_equ[2]+imz+1];
}
for(int igx=0;igx<ind_gx;igx++)
{
accX_str[igx]=TT[index_equ[3]+igx+1];
}
for(int igy=0;igy<ind_gy;igy++)
{
accY_str[igy]=TT[index_equ[4]+igy+1];
}
for(int igz=0;igz<ind_gz;igz++)
{
accZ_str[igz]=TT[index_equ[5]+igz+1];
}
/*for(int l=0;l<9;l++)
{
magX_str[l]=TT[2+l];
magY_str[l]=TT[2+9+l];
magZ_str[l]=TT[2+18+l];
}
for(int m=0;m<8;m++)
{
accX_str[m]=TT[2+27+m];
accY_str[m]=TT[2+27+8+m];
accZ_str[m]=TT[2+27+16+m];
}*/
//gate_data.Format("%s,%s,%s,%s,%s,%s\n\r",magX_str,magY_str,magZ_str,accX_str,accY_str,accZ_str);
magX=atof(magX_str);
magY=atof(magY_str);
magZ=atof(magZ_str);
accX=atof(accX_str);
accY=atof(accY_str);
accZ=atof(accZ_str);
mag_all=sqrt(magX*magX+magY*magY+magZ*magZ);
point2.Format("%.2f",mag_all);
gate_data.Format("%.1f,%.1f,%.1f,%.2f",magX,magY,magZ,mag_all);
m_magX=magX;
m_magY=magY;
m_magZ=magZ;
m_accX=accX;
m_accY=accY;
m_accZ=accZ;
m_mag_all=point2;
//dataInit.Delete(1,1);
//if(saveFlag == 1)
// dataSave+=gate_data;
}
m_dataRead+=strtemp;//加入接收编辑框对应字符串
}
}
}
UpdateData(FALSE); //更新编辑框内容
}
void CMag_ComDlg::GetSerialPort()//网上找的代码http://topic.
{ //结果是将串口值存入arrCom的字符串数组中
//CStringArray arrCom;
HKEY hKey;
int rtn;
//m_cmbComm.ResetContent();
rtn = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Hardware\\DeviceMap\\SerialComm",
NULL, KEY_READ, &hKey); // 打开串口注册表
if( rtn == ERROR_SUCCESS)
{
int i=0;
char portName[256],commName[256];
DWORD dwLong,dwSize;
while(1)
{
dwSize = sizeof(portName);
dwLong = dwSize;
rtn = RegEnumValue( hKey, i, portName, &dwLong,
NULL, NULL, (PUCHAR)commName, &dwSize );
m_arrCom.Add(commName);
if( rtn == ERROR_NO_MORE_ITEMS ) // 枚举串口
break;
i++;
}
RegCloseKey(hKey);
}
}
void CMag_ComDlg::OnButtonOpen()
{
// TODO: Add your control notification handler code here
CString str;
if(GetDlgItemText(IDC_BUTTON_OPEN,str),str=="打开Gate串口")
{
SetDlgItemText(IDC_BUTTON_OPEN,"关闭Gate串口");
}
else
{
SetDlgItemText(IDC_BUTTON_OPEN,"打开Gate串口");
}
if(str == "打开Gate串口" )
{
m_com.SetPortOpen(TRUE);
m_com.SetRThreshold(1);
m_com.SetInputLen(0); //设置当前接收区数据长度为0
m_com.GetInput(); //先预读缓冲区以清除残留数据
}
else
{
m_com.SetPortOpen(FALSE);
}
}
void CMag_ComDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(saveFlag == 1)
{
m_rFile.Write((LPCTSTR)dataSave,dataSave.GetLength());
m_rFile.Flush();
dataSave.Empty();
}
CDialog::OnTimer(nIDEvent);
}
void CMag_ComDlg::OnButtonSave()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CString bottonText;
int nLength;
if(GetDlgItemText(IDC_BUTTON_SAVE,bottonText),bottonText=="存储开始")
{
SetDlgItemText(IDC_BUTTON_SAVE,"存储结束");
}
else
{
SetDlgItemText(IDC_BUTTON_SAVE,"存储开始");
}
if(bottonText == "存储开始")
{
dataSave.Empty();
SetTimer(1,1000,NULL);
nLength = m_strCurPath.GetLength();
for( int nCount = 0; nCount < nLength; nCount++ )
{
if( m_strCurPath.GetAt( nCount ) == '\\' )
CreateDirectory( m_strCurPath.Left( nCount + 1 ), NULL );
}
CreateDirectory( m_strCurPath, NULL );
//CFile m_rFile;
LPCSTR lpszPath =m_strCurPath;// "c:\\comdata";
SetCurrentDirectory( lpszPath );
//文件名为Rec**.txt,以下代码自动检测文件名是否存在,若存在,则后面序号自动递增
//如Rec00.txt Rec01.txt,程序自动为正要保存的文件命名为Rec02.txt.
char buf[30];
CTime t = CTime::GetCurrentTime();
CString str1=t.Format("%Y年%m月%d日%H_%M_%S");
CString str=t.Format("%Y年%m月%d日%H时%M分%S秒\r\n");
sprintf(buf,"%s.txt",str1);
if(!m_rFile.Open(buf,CFile::modeCreate | CFile::modeWrite ))
{
AfxMessageBox( "创建记录文件失败!");
return;
}
if((access(buf,0))==-1)
{
AfxMessageBox("failed");
return;
}
m_rFile.Write((LPCTSTR)str,str.GetLength());
saveFlag=1;
}
else
{
KillTimer(1);
saveFlag=0;
CTime t = CTime::GetCurrentTime();
CString str2=t.Format("\r\n结束时间%Y年%m月%d日%H时%M分%S秒");
m_rFile.Write((LPCTSTR)str2,str2.GetLength());
m_rFile.Close();
}
}
void CMag_ComDlg::OnButtonPicStart()
{
// TODO: Add your control notification handler code here
CPicDlg tempDlg;
tempDlg.DoModal();
}
void CMag_ComDlg::OnSelendokComboComnum()
{
// TODO: Add your control notification handler code here
int i =m_comNum.GetCurSel();
CString a ;
m_comNum.GetLBText(i,a);
a.Delete(0,3);
int temp =atoi(a);
m_com.SetCommPort(temp);
m_comNumStr=a;
UpdateData(FALSE);
}
void CMag_ComDlg::OnComm2()
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
COleVariant myVar;
LONG len,k;
BYTE rxdata[1000]={0,}; //设置BYTE数组
CString strtemp,a,dataInit,lat_cstring,lon_cstring,time_gps_cstring,speed_gps_cstring;
CString lat_cstring_show,lon_cstring_show,time_gps_cstring_show,speed_gps_cstring_show,date_gps_show;
char TT[100]={0,};
static int count_test=0;
static BOOL datastart=0;
static bool dataend=0;
char lon[15]="";//经度
char lat[15]="";//维度
char time_gps[12]="";//时间
char speed[20]="";//速度
char speed_char[3];//速度标号
int hour,hour_1,hour_2;
if(m_com2.GetCommEvent()==2) //事件值为2表示接收缓冲区内有字符
{
// variant_inp=m_com2.GetInput(); //读缓冲区
// safearray_inp=variant_inp; //VARIANT型变量转换为ColeSafeArray型变量
myVar.Attach(m_com2.GetInput());
//safearray_inp=variant_inp;
safearray_inp=myVar;
len=safearray_inp.GetOneDimSize(); //得到有效数据长度
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
for(k=0;k<len;k++) //将数组转换为Cstring型变量
{
BYTE bt=*(char*)(rxdata+k);//字符型.这句话来推进bt变量的变化。
if(bt == 0x40)
{
datastart = 1;
}
if(datastart == 1)
{
strtemp.Format("%c",bt); //将字符送入临时变量strtemp存放
if(bt == 0x0d )
{
datastart = 0;
int length=dataRead_gps.GetLength();
dataInit = dataRead_gps;
for(int j=0;j<length;j++)
TT[j]=dataInit.GetAt(j);
dataRead_gps.Empty();
for(int k=0;k<12;k++)
{
time_gps[k]=TT[k+1+1];
}
for(int l=0;l<9;l++)
{
lat[l]=TT[13+l+1];
lon[l]=TT[21+l+1];
}
lon[9]=TT[30];
for(int m=0;m<15;m++)
{
speed[m]=TT[40+m+1];
}
if(speed[0] == 'E')
speed_char[0]='+';
else
speed_char[0]='-';
if(speed[5] == 'N')
speed_char[1]='+';
else
speed_char[1]='-';
if(speed[10] == 'U')
speed_char[2]='+';
else
speed_char[2]='-';
hour_1 = time_gps[6]-'0';
hour_2 = time_gps[7]-'0';
hour=(hour_1*10+hour_2+8)%24;
lat_cstring.Format("%c%c%c%c%c%c%c",lat[1],lat[2],lat[3],lat[4],lat[5],lat[6],lat[7],lat[8]);
lon_cstring.Format("%c%c%c%c%c%c%c%c",lon[1],lon[2],lon[3],lon[4],lon[5],lon[6],lon[7],lon[8],lon[9]);
time_gps_cstring.Format("%d%c%c%c%c%c%c",hour,time_gps[8],time_gps[9],time_gps[10],time_gps[11]);
speed_gps_cstring.Format("%c%c%c%c%c,%c%c%c%c%c,%c%c%c%c%c",speed_char[0],speed[1],speed[2],speed[3],speed[4],speed_char[1],speed[6],speed[7],speed[8],speed[9],speed_char[2],speed[11],speed[12],speed[13],speed[14]);
count_test++;
lat_cstring_show.Format("%c%c%c %c%c.%c%c%c",lat[0],lat[1],lat[2],lat[3],lat[4],lat[5],lat[6],lat[7]);
lon_cstring_show.Format("%c%c%c%c %c%c.%c%c%c",lon[0],lon[1],lon[2],lon[3],lon[4],lon[5],lon[6],lon[7],lon[8]);
time_gps_cstring_show.Format("%d:%c%c:%c%c",hour,time_gps[8],time_gps[9],time_gps[10],time_gps[11]);
speed_gps_cstring_show.Format("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",speed[0],speed[1],speed[2],speed[3],speed[4],speed[5],speed[6],speed[7],speed[8],speed[9],speed[10],speed[11],speed[12],speed[13],speed[14]);
date_gps_show.Format("20%c%c %c%c %c%c",time_gps[0],time_gps[1],time_gps[2],time_gps[3],time_gps[4],time_gps[5]);
m_gps_lat=lat_cstring_show;
m_gps_lon=lon_cstring_show;
m_gps_time=time_gps_cstring_show;
m_gps_speed=speed_gps_cstring_show;
m_gps_date=date_gps_show;
m_test=count_test;
gps_data.Format("%s,%s,%s,%s",lat_cstring,lon_cstring,time_gps_cstring,speed_gps_cstring);
//dataInit.Delete(1,1);
//if(saveFlag == 1)
//dataSave+=dataInit;
}
dataRead_gps+=strtemp;//界面中没有显示。加入接收编辑框对应字符串
}
}
}
UpdateData(FALSE); //更新编辑框内容
}
void CMag_ComDlg::OnComm3()
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
COleVariant myVar;
LONG len,k;
BYTE rxdata[2048]; //设置BYTE数组
CString strtemp,a,dataInit,mag_all_point6,mag_y_point5,mag_x_point5,mag_z_point5,mag_all_new_point6,mag_y_new_point5,mag_x_new_point5,mag_z_new_point5;
CString acc_x_point4,acc_y_point4,acc_z_point4;
char TT[53]={0,};
char magX_str[8],magY_str[8],magZ_str[8],accX_str[7],accY_str[7],accZ_str[7];
double magX,magY,magZ,magAll,magX_new,magY_new,magZ_new,magAll_new,accX,accY,accZ;
//double k1,k2,k3,ab_x,ab_y,ab_z,k_xy,k_z,ab_z2,k_zx;//袁智荣算法变量;
double a11,a12,a13,a22,a23,bx,by,bz;//林春生算法变量
static BOOL datastart=0;
if(m_com3.GetCommEvent()==2) //事件值为2表示接收缓冲区内有字符
{
// variant_inp=m_com3.GetInput(); //读缓冲区
// safearray_inp=variant_inp; //VARIANT型变量转换为ColeSafeArray型变量
myVar.Attach(m_com3.GetInput());
//safearray_inp=variant_inp;
safearray_inp=myVar;
len=safearray_inp.GetOneDimSize(); //得到有效数据长度
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
for(k=0;k<len;k++) //将数组转换为Cstring型变量
{
BYTE bt=*(char*)(rxdata+k); //字符型
if(bt == '$')
{
datastart=1;
}
if(datastart == 1)
{
strtemp.Format("%c",bt); //将字符送入临时变量strtemp存放
if(bt == 0x0d)
{
datastart = 0;
int length = comp_data.GetLength();
dataInit = comp_data;
for(int j=0;j<length;j++)
TT[j]=dataInit.GetAt(j);
comp_data.Empty();
for(int l=0;l<8;l++)
{
magX_str[l]=TT[2+l];
magY_str[l]=TT[2+9+l];
magZ_str[l]=TT[2+18+l];
}
for(int m=0;m<7;m++)
{
accX_str[m]=TT[2+27+m];
accY_str[m]=TT[2+27+8+m];
accZ_str[m]=TT[2+27+16+m];
}
magX=atof(magX_str);
magY=atof(magY_str);
magZ=atof(magZ_str);
accX=atof(accX_str);
accY=atof(accY_str);
accZ=atof(accZ_str);
magAll=magX*magX+magY*magY+magZ*magZ;
magAll=sqrt(magAll);
///////林春生的修正方法////////////////////////////
a11=0.8754;
a12=0.0249;
a13=0.0155;
a22=0.9966;
a23=-0.1125;
bx=0.0054;
by=0.0064;
bz=-0.0036;
magX_new=magX*a11+magY*a12+magZ*a13;
magY_new=magY*a22+magZ*a23;
magZ_new=magZ;///////////////////////////////////////////
magAll_new=magX_new*magX_new+magY_new*magY_new+magZ_new*magZ_new;
magAll_new=sqrt(magAll_new);
mag_x_point5.Format("%.5f",magX);
mag_y_point5.Format("%.5f",magY);
mag_z_point5.Format("%.5f",magZ);
mag_all_point6.Format("%.6f",magAll);
acc_x_point4.Format("%.4f",accX);
acc_y_point4.Format("%.4f",accY);
acc_z_point4.Format("%.4f",accZ);
mag_x_new_point5.Format("%.5f",magX_new);
mag_y_new_point5.Format("%.5f",magY_new);
mag_z_new_point5.Format("%.5f",magZ_new);
mag_all_new_point6.Format("%.6f",magAll_new);
m_comp_magX=mag_x_point5;
m_comp_magY=mag_y_point5;
m_comp_magZ=mag_z_point5;
m_comp_magAll=mag_all_point6;
//m_magX_new=mag_x_new_point5;
//m_magY_new=mag_y_new_point5;
//m_magZ_new=mag_z_new_point5;
//m_magAll_new=mag_all_new_point6;
m_comp_accX=acc_x_point4;
m_comp_accY=acc_y_point4;
m_comp_accZ=acc_z_point4;
dataInit.Format("%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.6f,%.6f,%s,%.4f,%.4f,%.4f,%s,%s\r\n",magX,magY,magZ,magX_new,magY_new,magZ_new,magAll,magAll_new,gate_data,accX,accY,accZ,gps_data,gyro_data_save);
if(saveFlag == 1)
{
dataSave+=dataInit;
}
}
comp_data+=strtemp;//界面中没有显示。加入接收编辑框对应字符串
}
}
}
UpdateData(FALSE); //更新编辑框内容
}
void CMag_ComDlg::OnComm4()
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
COleVariant myVar;
LONG len,k;
BYTE rxdata[2048]; //设置BYTE数组
CString strtemp,a,dataInit;
char TT[70]={0,};
char angX_str[15]={0,};
char angY_str[15]={0,};
char angZ_str[15]={0,};
char accX_str[10]={0,};
char accY_str[10]={0,};
char accZ_str[10]={0,};
double angX,angY,angZ,accX,accY,accZ;
int index_dot[6]={0,};
int index_sign[7]={0,};
int ind_dot=0;
int ind_sign=0;
int ind_angx,ind_angy,ind_angz,ind_accx,ind_accy,ind_accz;
static BOOL datastart=0;
if(m_com4.GetCommEvent()==2) //事件值为2表示接收缓冲区内有字符
{
// variant_inp=m_com4.GetInput(); //读缓冲区
// safearray_inp=variant_inp; //VARIANT型变量转换为ColeSafeArray型变量
myVar.Attach(m_com4.GetInput());
//safearray_inp=variant_inp;
safearray_inp=myVar;
len=safearray_inp.GetOneDimSize(); //得到有效数据长度
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
for(k=0;k<len;k++) //将数组转换为Cstring型变量
{
BYTE bt=*(char*)(rxdata+k); //字符型
if(bt == '$')
{
datastart=1;
}
if(datastart == 1)
{
strtemp.Format("%c",bt); //将字符送入临时变量strtemp存放
if(bt == 0x0a)
{
datastart = 0;
int length = gyro_data.GetLength();
dataInit = gyro_data;
for(int j=0;j<length;j++)
TT[j]=dataInit.GetAt(j);
gyro_data.Empty();
for(int f=0;f<length;f++)
{
if (TT[f]==0x2c)
{
index_dot[ind_dot]=f;
ind_dot++;
}
if(TT[f] == 0x2b || TT[f] == 0x2d)
{
index_sign[ind_sign]=f;
ind_sign++;
}
}
ind_angx=index_dot[0]-index_sign[0]-1;
ind_angy=index_dot[1]-index_sign[1]-1;
ind_angz=index_dot[2]-index_sign[2]-1;
ind_accx=index_dot[3]-index_sign[3]-1;
ind_accy=index_dot[4]-index_sign[4]-1;
ind_accz=length-index_sign[5]-1;
for(int iangx=0;iangx<=ind_angx;iangx++)
{
angX_str[iangx]=TT[index_sign[0]+iangx];
}
for(int iangy=0;iangy<=ind_angy;iangy++)
{
angY_str[iangy]=TT[index_sign[1]+iangy];
}
for(int iangz=0;iangz<=ind_angz;iangz++)
{
angZ_str[iangz]=TT[index_sign[2]+iangz];
}
for(int iaccx=0;iaccx<=ind_accx;iaccx++)
{
accX_str[iaccx]=TT[index_sign[3]+iaccx];
}
for(int iaccy=0;iaccy<=ind_accy;iaccy++)
{
accY_str[iaccy]=TT[index_sign[4]+iaccy];
}
for(int iaccz=0;iaccz<=ind_accz;iaccz++)
{
accZ_str[iaccz]=TT[index_sign[5]+iaccz];
}
angX=atof(angX_str);
angY=atof(angY_str);
angZ=atof(angZ_str);
accX=atof(accX_str);
accY=atof(accY_str);
accZ=atof(accZ_str);
m_gyro_angleX=angX;
m_gyro_angleY=angY;
m_gyro_angleZ=angZ;
m_gyro_accX=accX;
m_gyro_accY=accY;
m_gyro_accZ=accZ;
dataInit.Delete(0,2);
gyro_data_save=dataInit;
}
gyro_data+=strtemp;//界面中没有显示。加入接收编辑框对应字符串
}
}
}
UpdateData(FALSE); //更新编辑框内容
}