📄 mainfrm.cpp
字号:
temp=m_temp[i];
temp_hi=_lrotr(temp,8);
temp_lo=temp&0x00ff;
// temp_hi=0x50;
// temp_lo=0xa0; // 调试用
if(i==1) {temp_hi=4;} //这是数据包长度
for(k=0;k<1E6;k++)//分高低字节发送
{if(_inp(LSR)&0x020){_outp(COM,temp_hi);break;}} //_lrotr()为右移函数
Sleep(1000); //延时1秒,参数的单位是毫秒
for(k=0;k<1E6;k++)
{if(_inp(LSR)&0x020){_outp(COM,temp_lo);break;}}//直接输出temp,实际就是
// 输出其低八位,用temp_lo反而出错.
if(i!=3)
{Sleep(1000);} //延时1秒,参数的单位是毫秒
// check=check+temp_hi+temp_lo;
}
// goto end;//调试用
// check=!check+1;
//_outp(COM,check); 先不发送校验和
//sscanf(datadlg.m_len,"%04x",&data_len); //获得所读数据长度,16进制
// m_wndStatusBar.SetPaneText(4,"0",true);
//if(data_len==0){MessageBox("NO DATA READ!!!");clickok=true;goto end;}
//byte为接收到的字节
//unsigned short j=0; 这是为调试而设的一个变量
// AfxMessageBox("enter read program"); //这一句调试用
for(i=0;i<2*data_len+2;i++)
{
for(k=0;k<1E6;k++)
{if(_inp(LSR)&0x01){inbyte[i]=_inp(COM);break;}} //每一个接收数据都应在循环中读入
}
// AfxMessageBox("right1"); 调试用
for(i=0;i<2*data_len+2;i++)
{
byte_temp=inbyte[i];
if(i!=0)
{ if((i%2)==0)vstr=vstr+" "; //每4个字符空两格显示
if((i%20)==0)vstr=vstr+"\r\n"; //每10个换行
}
if(byte_temp<0x10) vstr=vstr+"0";
sprintf(display_ch,"%x",byte_temp); //将所得数值转化为字符显示
vstr=vstr+display_ch;
}
// for(;;)
// {
// if(_inp(LSR)&0x01)
// {
// char statuschar[10];
// in_byte=_inp(COM);
// if(in_byte<0x10)vstr=vstr+"0";
// sprintf(display_ch,"%x",in_byte); //将所得数值转化为字符显示
// vstr=vstr+display_ch;
// i++;
// if(i==0) AfxMessageBox("1 time"); //调试用
// //if(i==2) AfxMessageBox("2 time"); //调试用
// //if(i==3) AfxMessageBox("3 time"); //调试用
// //if(i>0) break; 这一句调试用
// if((i%2)==0)vstr=vstr+" "; //每4个字符空两格显示
// if((i%20)==0)vstr=vstr+"\r\n"; //每10个换行
// //AfxMessageBox("right0"); //调试用
// if(i==2*data_len+2) break;
// checksum=checksum+in_byte;
// sprintf(statuschar,"%d",i+1);//状态栏显示所读字节数
//AfxMessageBox("right1"); //调试用
//m_wndStatusBar.SetPaneText(4,statuschar,true);这句出错,所以改成注释
// AfxMessageBox("right2"); //调试用
// }
// }
AfxMessageBox("Now jump the cycle"); //这一句测试是否跳出了循环
checksum=!checksum+1;
if(checksum==byte_temp)
AfxMessageBox("校验和正确!");
// else AfxMessageBox("错误!!! 校验和不正确!!!");
//show_sth: 这个标号show_sth也是供调试用.
int re=AfxMessageBox("是否现在显示所读数据?",MB_OKCANCEL|MB_ICONINFORMATION,0);
if(re==IDOK)//确定,则显示所读数据
{
view->SetWindowText(vstr);
ment->SetModifiedFlag(true);
}
else//取消,则保存数据至DSPDATA文件
{
// CFileDialog* open_file=new CFileDialog(false);
// open_file->m_ofn.lpstrFilter ="文件(*.txt)\0*.txt\0所有文件(*.*)\0*.*\0\0";
CFile tempreadfile;
tempreadfile.Open("c:\\my documents\\dsp_data.txt",
CFile::modeWrite|CFile::modeCreate,NULL);
tempreadfile.Write(vstr,vstr.GetLength());
tempreadfile.Close();
}
clickok=false;
// AfxBeginThread(MyReadThread,RBuffer);因为读数线程易出错,先不用
}else clickok=true;
end:i=0;
}
UINT MyReadThread(LPVOID pRParam )
{
char *str=(char *)pRParam;
CString vstr="";
char display_ch[10]="";
unsigned char in_byte,checksum=0;
unsigned short i=0;
//unsigned short j=0; 这是为调试而设的一个变量
//AfxMessageBox("enter read program"); //这一句调试用
for(;;)
{
if(_inp(LSR)&0x01)
{
char statuschar[10];
in_byte=_inp(COM);
if(in_byte<0x10)vstr=vstr+"0";
sprintf(display_ch,"%x",in_byte); //将所得数值转化为字符显示
vstr=vstr+display_ch;
i++;
// if(i==1) AfxMessageBox("1 time"); //调试用
//if(i==2) AfxMessageBox("2 time"); //调试用
//if(i==3) AfxMessageBox("3 time"); //调试用
//if(i>0) break; 这一句调试用
if((i%2)==0)vstr=vstr+" "; //每4个字符空两格显示
if((i%20)==0)vstr=vstr+"\r\n"; //每10个换行
//AfxMessageBox("right0"); //调试用
if(i==2*data_len+2) break;
checksum=checksum+in_byte;
sprintf(statuschar,"%d",i+1);//状态栏显示所读字节数
//AfxMessageBox("right1"); //调试用
//m_wndStatusBar.SetPaneText(4,statuschar,true);这句出错,所以改成注释
// AfxMessageBox("right2"); //调试用
}
}
AfxMessageBox("Now jump the cycle"); //这一句测试是否跳出了循环
checksum=!checksum+1;
if(checksum==in_byte)
AfxMessageBox("校验和正确!");
else AfxMessageBox("错误!!! 校验和不正确!!!");
int re=AfxMessageBox("是否现在显示所读数据?",MB_OKCANCEL|MB_ICONINFORMATION,0);
if(re==IDOK)//确定,则显示所读数据
{
view->SetWindowText(vstr);
ment->SetModifiedFlag(true);
}
else//取消,则保存数据至DSPDATA文件
{
// CFileDialog* open_file=new CFileDialog(false);
// open_file->m_ofn.lpstrFilter ="文件(*.txt)\0*.txt\0所有文件(*.*)\0*.*\0\0";
CFile tempreadfile;
tempreadfile.Open("c:\\my documents\\dsp_data.txt",
CFile::modeWrite|CFile::modeCreate,NULL);
tempreadfile.Write(vstr,vstr.GetLength());
tempreadfile.Close();
}
clickok=true;
return 0;
}
unsigned long exe_len=0;
unsigned char buffer[1000000];
CString openfilename;
UINT MyWriteThread_BIN(LPVOID pParam );
UINT MyWriteThread_HEX(LPVOID pParam );
INT StringToInt(CString *psNum);
void CMainFrame::OnMenuExe()
{ CString strHR;
strHR.Format("\r\n\r\n"); //定义硬回车
int nChoice=AfxMessageBox("是否传送二进制文件?"+strHR+"Yes:传送二进制文件; No:Intel HEX文件",MB_YESNO|MB_ICONINFORMATION,0);
// TODO: Add your command handler code here
if(nChoice==IDYES) //传输二进制文件
{clickok=false;
AfxMessageBox("传输二进制文件,请先传送低位文件");
CFileDialog* open_file=new CFileDialog(true);
open_file->m_ofn.lpstrFilter ="可执行文件(*.exe)\0*.exe\0所有文件(*.*)\0*.*\0\0";
if (open_file->DoModal()==1)
{
openfilename=open_file->GetFileName();
CStdioFile m_file;
m_file.Open(openfilename,CFile::modeRead|CFile::typeBinary,NULL);
exe_len=m_file.GetLength();
m_file.Read(buffer,exe_len);
m_file.Close ();
char total[10];
sprintf(total,"%d",exe_len);
m_wndStatusBar.SetPaneText(2,total,true);
AfxBeginThread(MyWriteThread_BIN,buffer);
}else clickok=true;
delete open_file;
}
//TODO:add your extra code here
else //传输HEX文件
{int m_temp[4]={0};
int temp_hi,temp,i,k;
AfxMessageBox("传输Intel HEX文件,请先传送低位文件");
int result=programdlg.DoModal();
if(result=IDOK)
{m_temp[0]=0x50A0; //数据头50A0H
m_temp[1]=0x0510; //数据包长度05H,命令码10H
sscanf(programdlg.m_program,"%04x",&(m_temp[2])); //获得起始地址
}
clickok=false;
CFileDialog* open_file=new CFileDialog(true);
open_file->m_ofn.lpstrFilter ="可执行文件(*.exe)\0*.exe\0所有文件(*.*)\0*.*\0\0";
if (open_file->DoModal()==1)
{
openfilename=open_file->GetFileName();
CStdioFile m_file;
m_file.Open(openfilename,CFile::modeRead|CFile::typeText,NULL);
//define a new file
CStdioFile fileTarget;
//create a new file name.
CString strNewFileName=openfilename;
int nDot=strNewFileName.Find('.');
if(nDot>-1)
strNewFileName=strNewFileName.Left(nDot);
strNewFileName+=".TXT";
//make sure source and target are different!
if(openfilename==strNewFileName)
{ AfxMessageBox("Stop!Source and target are the same!",MB_OK|MB_ICONSTOP);
return;
}
//open target file
fileTarget.Open(strNewFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeText,NULL);
CString strline;
bool exit_flag=0; //退出标志初值为零
while(m_file.ReadString(strline))
{ CString ch_temp;
if(exit_flag==1) {break;}
int length=strline.GetLength(); //测试一行的长度
if(length<75) {exit_flag=1;} //到达倒数第二行,退出标志赋1
ch_temp=strline.Mid(9,length-11);//要去掉开头的九个
//和最后的两个(校验数),所以减去11.
fileTarget.WriteString(ch_temp);
}
exe_len=fileTarget.GetLength();
m_temp[3]=exe_len/2;//除以2,得到传输总字节数
AfxMessageBox("准备传送文件.");
for(i=0;i<4;i++)
{
temp=m_temp[i];
temp_hi=_lrotr(temp,8); //得到高八位
for(k=0;k<1E5;k++)
{if(_inp(LSR)&0x020) {_outp(COM,temp_hi);break;}} //传输高八位
if(i<2) Sleep(1000);
for(k=0;k<1E5;k++)
{if(_inp(LSR)&0x020) {_outp(COM,temp);break;}}//传输低八位
if(i<2) Sleep(1000);
}
if(openfilename=="Fileh8.hex")
{
for(k=0;k<1E5;k++)
{if(_inp(LSR)&0x020) {_outp(COM,0x55);break;}}
}
if(openfilename=="Filel8.hex")
{
for(k=0;k<1E5;k++)
{if(_inp(LSR)&0x020) {_outp(COM,0x40);break;}}
}
m_file.SeekToBegin();
exit_flag=0;
CString *point;
while(m_file.ReadString(strline))
{ if(exit_flag==1) {break;}
int length=strline.GetLength();
if(length<75) {exit_flag=1;}
length=(length-11)/2;
i=0;
while(length)
{ CString ch_temp=strline.Mid(9+i,1);
point=&ch_temp;
int bit0=StringToInt(point);//高位
i++;
ch_temp=strline.Mid(9+i,1);
point=&ch_temp;
int bit1=StringToInt(point);//低位
int trans_data=bit0*16+bit1;
i++;
length--;
for(k=0;k<1E5;k++)
{if(_inp(LSR)&0x020) {_outp(COM,trans_data);break;}}
}
}
//fileTarget.Read(buffer,exe_len); 有问题
m_file.Close (); //关闭源文件
fileTarget.Close();//关闭目标文件
char total[10];
sprintf(total,"%d",exe_len);
m_wndStatusBar.SetPaneText(2,total,true);
AfxMessageBox("文件传送完毕"); //调试用
//AfxBeginThread(MyWriteThread_HEX,buffer);
}else clickok=true;
delete open_file;
}
}
INT StringToInt(CString *psNum)
{ if((*psNum)=='A') {return 10;}
if((*psNum)=='B') {return 11;}
if((*psNum)=='C') {return 12;}
if((*psNum)=='D') {return 13;}
if((*psNum)=='E') {return 14;}
if((*psNum)=='F') {return 15;}
return (INT)atol(psNum->GetBuffer(10));
}
UINT MyWriteThread_BIN(LPVOID pParam )
{
unsigned char *ch=(unsigned char *)pParam;
unsigned short i=0;
char statuschar[15];
for(;;)
{
if(_inp(LSR)&0x020)
{
_outp(COM,*(ch+i));
i++;
sprintf(statuschar,"%d",i);
//AfxMessageBox("third breakpoint"); 调试用
//m_wndStatusBar.SetPaneText(4,statuschar,true);
//AfxMessageBox("fourth breakpoint"); 调试用
if(i==exe_len)
break;
}
}
AfxMessageBox("文件"+openfilename+"传输完毕!!!");
clickok=true;
return 0;
}
UINT MyWriteThread_HEX(LPVOID pParam )
{
unsigned char *ch=(unsigned char *)pParam;
unsigned short i=0;
char statuschar[15];
for(;;)
{
if(_inp(LSR)&0x020)
{
_outp(COM,*(ch+i));
i++;
sprintf(statuschar,"%d",i);
//AfxMessageBox("third breakpoint"); 调试用
//m_wndStatusBar.SetPaneText(4,statuschar,true);
//AfxMessageBox("fourth breakpoint"); 调试用
if(i==exe_len)
break;
}
}
AfxMessageBox("文件"+openfilename+"传输完毕!!!");
clickok=true;
return 0;
}
int baud=3,com=1,databit=3,parity=2,stopbit=0;
void CMainFrame::OnMenuSet()
{
// TODO: Add your command handler code here
CSetdlg setdlg;
int result=setdlg.DoModal();
if(result==IDOK)
{
if(com==0){
COM=COM+16*16;
LSR=LSR+16*16;
LCR=LCR+16*16;
MSB=MSB+16*16;
LSB=LSB+16*16;}
_outp(LCR,0x80);//除数寄存器允许访问
if(baud==0){_outp(MSB,0);_outp(LSB,0x60);}//波特 =1200
if(baud==1){_outp(MSB,0);_outp(LSB,0x30);}//2400
if(baud==2){_outp(MSB,0);_outp(LSB,0x18);}//4800
if(baud==3){_outp(MSB,0);_outp(LSB,0x0c);}//9600
if(parity==2)_outp(LCR,(databit+stopbit*4));//发送字符位数=8,不要校验
if(parity==0)_outp(LCR,8+(databit+stopbit*4));//奇校验
if(parity==1)_outp(LCR,8+16+(databit+stopbit*4));//偶校验
}
}
void CMainFrame::OnUpdateMenuData(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if(clickok==true)
{
pCmdUI->Enable(true);
}else
{
pCmdUI->Enable(true);
}
}
void CMainFrame::OnUpdateMenuExe(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if(clickok==true)
{
pCmdUI->Enable(true);
}else
{
pCmdUI->Enable(true);
}
}
void CMainFrame::OnUpdateMenuHand(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if(clickok==true)
{
pCmdUI->Enable(true);
}else
{
pCmdUI->Enable(true);
}
}
void CMainFrame::OnUpdateMenuProgram(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if(clickok==true)
{
pCmdUI->Enable(true);
}else
{
pCmdUI->Enable(true);
}
}
void CMainFrame::OnUpdateMenuRead(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if(clickok==true)
{
pCmdUI->Enable(true);
}else
{
pCmdUI->Enable(true);
}
}
void CMainFrame::OnUpdateMenuSet(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if(clickok==true)
{
pCmdUI->Enable(true);
}else
{
pCmdUI->Enable(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -