VS2008 C++开发的串口助手 接收记数器 不正常 懂的朋友 帮看看那问题
已经奋斗了几天了还是 找不出问题对于初学者来说 真难!!!!!!!!!!!!!不管接收到多少数据 RX都是显示1
http://cache.amobbs.com/bbs_upload782111/files_50/ourdev_713862CAYJW2.jpg
(原文件名:4GIF4@OR
源程序:
点击此处下载 ourdev_713924CHNLNN.rar(文件大小:26.73M) (原文件名:serialcom.rar)
代码如下:
// serialcomDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "serialcom.h"
#include "serialcomDlg.h"
#include "CFolderSelectDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
WNDPROC hPreProc;
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CserialcomDlg 对话框
CserialcomDlg::CserialcomDlg(CWnd* pParent /*=NULL*/)
: CDialog(CserialcomDlg::IDD, pParent)
, comsel(0)
, m_Setting(_T(""))
, flag(true)
, m_RXData(_T(""))
, m_TXData(_T("Q"))
, rxcounter(0)
, txcounter(0)
, m_time(_T("1000"))
, iconflag(false)
, strPath(_T(""))
, m_savePath(_T("C:\\COMDATA\\"))
, m_TXData2(_T("U"))
, m_TXData3(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON_MAIN1);
m_hIcon1=AfxGetApp()->LoadIcon(IDI_ICON_MAIN);
}
void CserialcomDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_COMPORT, m_ctrlPort);
DDX_Control(pDX, IDC_SPEED, m_ctrlSpeed);
DDX_Control(pDX, IDC_ISCHECK, m_ctrlCheck);
DDX_Control(pDX, IDC_DATA, m_ctrlData);
DDX_Control(pDX, IDC_STOP, m_ctrlStop);
DDX_Control(pDX, IDC_MSCOMM1, m_ctrlComm);
DDX_Control(pDX, IDC_BT_CLOSEPORT, m_ctrlBTClose);
DDX_Control(pDX, IDC_ICON_ONOFF, m_ctrlIcon);
DDX_Text(pDX, IDC_EDIT2, m_RXData);
DDX_Text(pDX, IDC_EDIT3, m_TXData);
DDX_Control(pDX, IDC_EDIT2, m_ctrlRXData);
DDX_Control(pDX, IDC_EDIT3, m_ctrlTXData);
DDX_Control(pDX, IDC_CHECK_VIEW_16, m_check);
DDX_Control(pDX, IDC_CHECK_SEND_16, m_sendbyhex);
DDX_Control(pDX, IDC_RX, m_rx);
DDX_Control(pDX, IDC_TX, m_tx);
DDX_Control(pDX, IDC_CHECK_AUTO_SEND, m_ctrlautosend);
DDX_Text(pDX, IDC_EDIT_SEND_TIME, m_time);
DDX_Control(pDX, IDC_CHECK_AUTO_CLEAR, m_ctrlautoclear);
DDX_Text(pDX, IDC_SAVEPATH, m_savePath);
DDX_Text(pDX, IDC_EDIT5, m_TXData2);
DDX_Control(pDX, IDC_EDIT5, m_ctrlTXData2);
DDX_Control(pDX, IDC_EDIT4, m_ctrlTXData3);
DDX_Text(pDX, IDC_EDIT4, m_TXData3);
}
BEGIN_MESSAGE_MAP(CserialcomDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BT_CLOSEPORT, &CserialcomDlg::OnBnClickedBtCloseport)
ON_BN_CLICKED(IDC_BT_CLOSE, &CserialcomDlg::OnBnClickedBtClose)
ON_BN_CLICKED(IDC_BT_SEND_HAND, &CserialcomDlg::OnBnClickedBtSendHand)
ON_BN_CLICKED(IDC_BT_CLEAR, &CserialcomDlg::OnBnClickedBtClear)
ON_BN_CLICKED(IDC_BT_STOPVIEW, &CserialcomDlg::OnBnClickedBtStopview)
ON_BN_CLICKED(IDC_BT_CLEAR_SEND_, &CserialcomDlg::OnBnClickedBtClearSend)
ON_CBN_SELCHANGE(IDC_COMPORT, &CserialcomDlg::OnCbnSelchangeComport)
ON_CBN_SELCHANGE(IDC_SPEED, &CserialcomDlg::OnCbnSelchangeSpeed)
ON_CBN_SELCHANGE(IDC_ISCHECK, &CserialcomDlg::OnCbnSelchangeIscheck)
ON_CBN_SELCHANGE(IDC_DATA, &CserialcomDlg::OnCbnSelchangeData)
ON_CBN_SELCHANGE(IDC_STOP, &CserialcomDlg::OnCbnSelchangeStop)
ON_BN_CLICKED(IDC_CHECK_SEND_16, &CserialcomDlg::OnBnClickedCheckSend16)
ON_BN_CLICKED(IDC_BT_CLEARCOUNTER, &CserialcomDlg::OnBnClickedBtClearcounter)
ON_BN_CLICKED(IDC_CHECK_AUTO_SEND, &CserialcomDlg::OnBnClickedCheckAutoSend)
ON_WM_TIMER()
ON_EN_ERRSPACE(IDC_EDIT2, &CserialcomDlg::OnEnErrspaceEdit2)
ON_BN_CLICKED(IDC_BUTTON1, &CserialcomDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CserialcomDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON5, &CserialcomDlg::OnBnClickedButton5)
ON_BN_CLICKED(IDC_BUTTON4, &CserialcomDlg::OnBnClickedButton4)
ON_BN_CLICKED(IDC_BT_SEND_HAND2, &CserialcomDlg::OnBnClickedBtSendHand2)
ON_BN_CLICKED(IDC_BT_SEND_HAND3, &CserialcomDlg::OnBnClickedBtSendHand3)
END_MESSAGE_MAP()
// CserialcomDlg 消息处理程序
BOOL CserialcomDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
//执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
CserialcomDlg::AddList();
CserialcomDlg::SetWindowTextA(_T("串口调试工具V1.0"));
CWnd::CenterWindow();
GetDlgItem(IDC_CHECK_AUTO_SEND)->EnableWindow(FALSE);
CreateDirectory(_T("C:\\COMDATA\\"),NULL);
m_ctrlBTClose.SetWindowText(_T("打开串口"));
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_OFF));
CserialcomDlg::OpenPort();
SetTimer(2,500,NULL);
GetDlgItem(IDC_CHECK_AUTO_SEND)->EnableWindow(TRUE);
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_ON));
m_ctrlBTClose.SetWindowText(_T("关闭串口"));
flag=false;
UpdateData(FALSE);
return TRUE;// 除非将焦点设置到控件,否则返回 TRUE
}
void CserialcomDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
//来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//这将由框架自动完成。
void CserialcomDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CserialcomDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CserialcomDlg::AddList(void)
{
m_ctrlPort.AddString(_T("com1"));
m_ctrlPort.AddString(_T("com2"));
m_ctrlPort.AddString(_T("com3"));
m_ctrlPort.AddString(_T("com4"));
m_ctrlPort.AddString(_T("com5"));
m_ctrlPort.AddString(_T("com6"));
m_ctrlPort.AddString(_T("com7"));
m_ctrlPort.AddString(_T("com8"));
m_ctrlPort.SetCurSel(0);
comsel=1;
m_ctrlSpeed.AddString(_T("300"));
m_ctrlSpeed.AddString(_T("600"));
m_ctrlSpeed.AddString(_T("1200"));
m_ctrlSpeed.AddString(_T("2400"));
m_ctrlSpeed.AddString(_T("4800"));
m_ctrlSpeed.AddString(_T("9600"));
m_ctrlSpeed.AddString(_T("19200"));
m_ctrlSpeed.AddString(_T("38400"));
m_ctrlSpeed.AddString(_T("43000"));
m_ctrlSpeed.AddString(_T("56000"));
m_ctrlSpeed.AddString(_T("57600"));
m_ctrlSpeed.AddString(_T("115200"));
m_ctrlSpeed.SetCurSel(5);
m_ctrlCheck.AddString(_T("无(NONE)"));
m_ctrlCheck.AddString(_T("奇(ODD)"));
m_ctrlCheck.AddString(_T("偶(EVEN)"));
m_ctrlCheck.SetCurSel(0);
m_ctrlData.AddString(_T("8"));
m_ctrlData.AddString(_T("7"));
m_ctrlData.AddString(_T("6"));
m_ctrlData.SetCurSel(0);
m_ctrlStop.AddString(_T("1"));
m_ctrlStop.AddString(_T("2"));
m_ctrlStop.SetCurSel(0);
//GetCurrentSetting();
}
void CserialcomDlg::OpenPort(void)
{
comsel=(m_ctrlPort.GetCurSel()+1);
GetCurrentSetting();
if(m_ctrlComm.get_PortOpen())
m_ctrlComm.put_PortOpen(FALSE);
m_ctrlComm.put_CommPort(comsel);
if( !m_ctrlComm.get_PortOpen())
{
m_ctrlComm.put_PortOpen(TRUE);
}
else
AfxMessageBox(_T("cannot open serial port"));
m_ctrlComm.put_Settings(m_Setting);
m_ctrlComm.put_InputMode(1);
m_ctrlComm.put_RThreshold(1);
m_ctrlComm.put_InputLen(0);
m_ctrlComm.get_Input();
UpdateData(FALSE);
}
void CserialcomDlg::GetCurrentSetting(void)
{
CString temp;
int tmp;
m_Setting=_T("");
m_ctrlSpeed.GetLBText(m_ctrlSpeed.GetCurSel(),temp);
temp+=',';
m_Setting+=temp;
tmp=m_ctrlCheck.GetCurSel();
if(tmp==0)
{
m_Setting+=_T("n,");
}
else if(tmp==1)
{
m_Setting+=_T("o,");
}
else
{
m_Setting+=_T("e,");
}
m_ctrlData.GetLBText(m_ctrlData.GetCurSel(),temp);
temp+=',';
m_Setting+=temp;
m_ctrlStop.GetLBText(m_ctrlStop.GetCurSel(),temp);
m_Setting+=temp;
}
void CserialcomDlg::ClosePort(void)
{
UpdateData(FALSE);
if(m_ctrlComm.get_PortOpen())
m_ctrlComm.put_PortOpen(FALSE);
}
void CserialcomDlg::OnBnClickedBtCloseport()
{
if(flag)
{
/*m_ctrlBTClose.SetWindowText(_T("打开串口"));
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_OFF));
m_ctrlComm.put_PortOpen(FALSE);*/
OpenPort();
GetDlgItem(IDC_CHECK_AUTO_SEND)->EnableWindow(TRUE);
SetTimer(2,500,NULL);
flag=!flag;
m_ctrlBTClose.SetWindowText(_T("关闭串口"));
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_ON));
}
else
{
/*m_ctrlBTClose.SetWindowText(_T("关闭串口"));
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_ON));
m_ctrlComm.put_PortOpen(TRUE);*/
ClosePort();
GetDlgItem(IDC_CHECK_AUTO_SEND)->EnableWindow(FALSE);
KillTimer(2);
KillTimer(1);
flag=!flag;
m_ctrlBTClose.SetWindowText(_T("打开串口"));
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_OFF));
}
}
void CserialcomDlg::OnBnClickedBtClose()
{
// TODO: 在此添加控件通知处理程序代码
CserialcomDlg::OnOK();
}
BEGIN_EVENTSINK_MAP(CserialcomDlg, CDialog)
ON_EVENT(CserialcomDlg, IDC_MSCOMM1, 1, CserialcomDlg::OnComm, VTS_NONE)
END_EVENTSINK_MAP()
void CserialcomDlg::OnComm()
{
// TODO: 在此处添加消息处理程序代码
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
// BYTE rxdata;
CString strtemp,str;
GetDlgItem(IDC_BT_STOPVIEW)->GetWindowText(str);
if(m_ctrlComm.get_CommEvent()==2)
{
variant_inp=m_ctrlComm.get_Input();
safearray_inp=variant_inp;
len=safearray_inp.GetOneDimSize();
BYTE *rxdata;
rxdata=new BYTE;
//BYTE *rxdata=(BYTE *)malloc(sizeof(BYTE));
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);
for(k=0;k<len;k++)
{
BYTE bt=*(char*)(rxdata+k);
if(m_check.GetCheck()==BST_CHECKED)
strtemp.Format(_T("%X "),bt);
else
strtemp.Format(_T("%c"),bt);
if(str==_T("停止显示"))
{
m_RXData+=strtemp;
if(m_ctrlautoclear.GetCheck()==BST_CHECKED)
{
if(m_RXData.GetLength()>4096)
{
m_RXData=_T("");
}
}
}
}
//free(rxdata);
delete rxdata;
txcounter+=len;
str.Format(_T("Rx:%d"),rxcounter);
m_rx.SetWindowText(str);
}
UpdateData(FALSE);
}
void CserialcomDlg::OnBnClickedBtSendHand()
{
// TODO: 在此添加控件通知处理程序代码
CserialcomDlg::SendData();
}
void CserialcomDlg::OnBnClickedBtClear()
{
// TODO: 在此添加控件通知处理程序代码
m_ctrlRXData.SetWindowText(_T(""));
m_RXData=_T("");
}
void CserialcomDlg::OnBnClickedBtStopview()
{
// TODO: 在此添加控件通知处理程序代码
CString str;
GetDlgItem(IDC_BT_STOPVIEW)->GetWindowText(str);
if(str==_T("停止显示"))
{
GetDlgItem(IDC_BT_STOPVIEW)->SetWindowText(_T("继续显示"));
}
else
{
GetDlgItem(IDC_BT_STOPVIEW)->SetWindowText(_T("停止显示"));
}
}
void CserialcomDlg::OnBnClickedBtClearSend()
{
// TODO: 在此添加控件通知处理程序代码
m_ctrlTXData.SetWindowText(_T("Q"));
m_ctrlTXData2.SetWindowText(_T("U"));
m_ctrlTXData3.SetWindowText(_T(""));
}
void CserialcomDlg::OnCbnSelchangeComport()
{
// TODO: 在此添加控件通知处理程序代码
ClosePort();
GetDlgItem(IDC_CHECK_AUTO_SEND)->EnableWindow(FALSE);
KillTimer(2);
m_ctrlBTClose.SetWindowText(_T("打开串口"));
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_OFF));
GetCurrentSetting();
OpenPort();
GetDlgItem(IDC_CHECK_AUTO_SEND)->EnableWindow(TRUE);
SetTimer(2,500,NULL);
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_ON));
m_ctrlBTClose.SetWindowText(_T("关闭串口"));
}
void CserialcomDlg::OnCbnSelchangeSpeed()
{
// TODO: 在此添加控件通知处理程序代码
ClosePort();
GetDlgItem(IDC_CHECK_AUTO_SEND)->EnableWindow(FALSE);
KillTimer(2);
m_ctrlBTClose.SetWindowText(_T("打开串口"));
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_OFF));
GetCurrentSetting();
OpenPort();
GetDlgItem(IDC_CHECK_AUTO_SEND)->EnableWindow(TRUE);
SetTimer(2,500,NULL);
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_ON));
m_ctrlBTClose.SetWindowText(_T("关闭串口"));
}
void CserialcomDlg::OnCbnSelchangeIscheck()
{
// TODO: 在此添加控件通知处理程序代码
ClosePort();
GetDlgItem(IDC_CHECK_AUTO_SEND)->EnableWindow(FALSE);
KillTimer(2);
m_ctrlBTClose.SetWindowText(_T("打开串口"));
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_OFF));
GetCurrentSetting();
OpenPort();
GetDlgItem(IDC_CHECK_AUTO_SEND)->EnableWindow(TRUE);
SetTimer(2,500,NULL);
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_ON));
m_ctrlBTClose.SetWindowText(_T("关闭串口"));
}
void CserialcomDlg::OnCbnSelchangeData()
{
// TODO: 在此添加控件通知处理程序代码
ClosePort();
GetDlgItem(IDC_CHECK_AUTO_SEND)->EnableWindow(FALSE);
KillTimer(2);
m_ctrlBTClose.SetWindowText(_T("打开串口"));
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_OFF));
GetCurrentSetting();
OpenPort();
GetDlgItem(IDC_CHECK_AUTO_SEND)->EnableWindow(TRUE);
SetTimer(2,500,NULL);
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_ON));
m_ctrlBTClose.SetWindowText(_T("关闭串口"));
}
void CserialcomDlg::OnCbnSelchangeStop()
{
// TODO: 在此添加控件通知处理程序代码
ClosePort();
GetDlgItem(IDC_CHECK_AUTO_SEND)->EnableWindow(FALSE);
KillTimer(2);
m_ctrlBTClose.SetWindowText(_T("打开串口"));
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_OFF));
GetCurrentSetting();
OpenPort();
GetDlgItem(IDC_CHECK_AUTO_SEND)->EnableWindow(TRUE);
SetTimer(2,500,NULL);
m_ctrlIcon.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_ON));
m_ctrlBTClose.SetWindowText(_T("关闭串口"));
}
LRESULT CALLBACK EditProc(HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
if(uMsg==WM_CHAR)
{
if(((wParam>='0')&&(wParam<='F'))||((wParam>='a')&&(wParam<='f'))||(wParam==' ')||(wParam==VK_BACK))
{
if(wParam>='a'&&wParam<='f')
{
wParam-=32;
}
return hPreProc(hwnd,uMsg,wParam,lParam);
}
}
else
{
return hPreProc(hwnd,uMsg,wParam,lParam);
}
}
void CserialcomDlg::OnBnClickedCheckSend16()
{
// TODO: 在此添加控件通知处理程序代码
if(m_sendbyhex.GetCheck()==BST_CHECKED)
{
m_TXData.Empty();
m_ctrlTXData.SetWindowText(_T(""));
hPreProc=(WNDPROC)SetWindowLong(GetDlgItem(IDC_EDIT3)->m_hWnd,GWL_WNDPROC,(LONG)EditProc);
}
else
{
SetWindowLong(GetDlgItem(IDC_EDIT3)->m_hWnd,GWL_WNDPROC,(LONG)hPreProc);
}
}
void CserialcomDlg::OnBnClickedBtClearcounter()
{
// TODO: 在此添加控件通知处理程序代码
rxcounter=0;
txcounter=0;
m_rx.SetWindowText(_T("Rx:0"));
m_tx.SetWindowText(_T("Tx:0"));
}
void CserialcomDlg::OnBnClickedCheckAutoSend()
{
// TODO: 在此添加控件通知处理程序代码
if(m_ctrlautosend.GetCheck()==BST_CHECKED)
{
SetTimer(1,GetDlgItemInt(IDC_EDIT_SEND_TIME),NULL);
}
else
{
KillTimer(1);
}
}
void CserialcomDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if(nIDEvent==1)
{
CserialcomDlg::SendData();
}
if(nIDEvent==2)
{
if(iconflag)
{
iconflag=false;
SetIcon(m_hIcon,FALSE);
}
else
{
iconflag=true;
SetIcon(m_hIcon1,FALSE);
}
}
CDialog::OnTimer(nIDEvent);
}
void CserialcomDlg::SendData(void)
{
int i,len;
unsigned char tmp;
CString str,str1,str2;
CByteArray Binbyte;
str=_T("");
str1=_T("");
str2=_T("");
m_ctrlTXData.GetWindowText(m_TXData);
m_TXData.Replace(_T(" "),_T(""));
len=m_TXData.GetLength();
// rxcounter+=len;
Binbyte.RemoveAll();
UpdateData(TRUE);
if(m_sendbyhex.GetCheck()==BST_CHECKED)
{
rxcounter+=(len/2);
for(i=0;i<len-1;i+=2)
{
tmp=0;
tmp=(BYTE)(m_TXData.GetAt(i)>='A'?m_TXData.GetAt(i)-55:m_TXData.GetAt(i)-'0');
tmp=(BYTE)(tmp*16+(BYTE)(m_TXData.GetAt(i+1)>='A'?m_TXData.GetAt(i+1)-55:m_TXData.GetAt(i+1)-'0'));
Binbyte.Add(tmp);
}
/*if(len%2!=0)
{
Binbyte.Add((BYTE)(m_TXData.GetAt(len-1)-55));
rxcounter+=1;
}*/
COleVariant var(Binbyte);
m_ctrlComm.put_Output(var);
}
else
{
rxcounter+=len;
m_ctrlComm.put_Output(COleVariant(m_TXData));
}
str2.Format(_T("Tx:%d"),rxcounter);
m_tx.SetWindowText(str2);
}
void CserialcomDlg::OnEnErrspaceEdit2()
{
// TODO: 在此添加控件通知处理程序代码
m_RXData=_T("");
}
void CserialcomDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CFileDialog dlg(TRUE);
dlg.m_ofn.lpstrTitle=_T("选择要发送的文件");
dlg.m_ofn.lpstrFilter=_T("Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0");
if(IDOK==dlg.DoModal())
{
strPath=dlg.GetPathName();
GetDlgItem(IDC_FILE_PATH)->SetWindowText(strPath);
}
}
void CserialcomDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
CFile file;
if(file.Open(strPath,CFile::modeRead,NULL)==0)
{
MessageBox(_T("文件不存在!"),_T("错误"),MB_OK);
}
else
{
CString str;
char *pBuf;
pBuf=new char;
ULONGLONG dwFileLen;
dwFileLen=file.GetLength();
for(int i=0;i<dwFileLen/1024;i++)
{
file.Read(pBuf,1024);
// while(!is_RxOver);
m_ctrlComm.put_Output(COleVariant(pBuf));
}
if(dwFileLen%1024!=0)
{
memset(pBuf,0,1024);
file.Read(pBuf,dwFileLen%1024);
m_ctrlComm.put_Output(COleVariant(pBuf));
}
file.Close();
rxcounter+=dwFileLen;
str.Format(_T("Tx:%d"),rxcounter);
m_tx.SetWindowText(str);
//free(pBuf);
delete pBuf;
}
GetDlgItem(IDC_BUTTON2)->EnableWindow(TRUE);
}
void CserialcomDlg::OnBnClickedButton5()
{
if(g_fSelectFolderDlg(&m_savePath,m_savePath,false))
{
UpdateData(false);
}
}
void CserialcomDlg::OnBnClickedButton4()
{
SYSTEMTIME tm;
DWORD dwlen=m_RXData.GetLength();
::GetLocalTime(&tm);
CString filename;
filename.Format(_T("%d-%d-%d-%d-%d-%d.txt"),tm.wYear,tm.wMonth,tm.wDay,tm.wHour,tm.wMinute,tm.wSecond);
m_savePath+=filename;
CFile file;
if(dwlen!=0)
{
char *pBuf;
pBuf=new char;
if(file.Open(m_savePath,CFile::modeCreate|CFile::modeWrite,NULL)==0)
{
MessageBox(_T("文件夹不存在"),_T("错误"),MB_OK);
}
else
{
file.Write(m_RXData,dwlen);
file.Close();
}
//free(pBuf);
delete pBuf;
}
}
void CserialcomDlg::OnBnClickedBtSendHand2()
{
// TODO: 在此添加控件通知处理程序代码
int i,len;
unsigned char tmp;
CString str,str1,str2;
CByteArray Binbyte;
str=_T("");
str1=_T("");
str2=_T("");
m_ctrlTXData2.GetWindowText(m_TXData2);
m_TXData2.Replace(_T(" "),_T(""));
len=m_TXData2.GetLength();
// rxcounter+=len;
Binbyte.RemoveAll();
UpdateData(TRUE);
if(m_sendbyhex.GetCheck()==BST_CHECKED)
{
rxcounter+=(len/2);
for(i=0;i<len-1;i+=2)
{
tmp=0;
tmp=(BYTE)(m_TXData2.GetAt(i)>='A'?m_TXData2.GetAt(i)-55:m_TXData2.GetAt(i)-'0');
tmp=(BYTE)(tmp*16+(BYTE)(m_TXData2.GetAt(i+1)>='A'?m_TXData2.GetAt(i+1)-55:m_TXData2.GetAt(i+1)-'0'));
Binbyte.Add(tmp);
}
/*if(len%2!=0)
{
Binbyte.Add((BYTE)(m_TXData.GetAt(len-1)-55));
rxcounter+=1;
}*/
COleVariant var(Binbyte);
m_ctrlComm.put_Output(var);
}
else
{
rxcounter+=len;
m_ctrlComm.put_Output(COleVariant(m_TXData2));
}
str2.Format(_T("Tx:%d"),rxcounter);
m_tx.SetWindowText(str2);
}
void CserialcomDlg::OnBnClickedBtSendHand3()
{
// TODO: 在此添加控件通知处理程序代码
int i,len;
unsigned char tmp;
CString str,str1,str2;
CByteArray Binbyte;
str=_T("");
str1=_T("");
str2=_T("");
m_ctrlTXData3.GetWindowText(m_TXData3);
m_TXData3.Replace(_T(" "),_T(""));
len=m_TXData3.GetLength();
// rxcounter+=len;
Binbyte.RemoveAll();
UpdateData(TRUE);
if(m_sendbyhex.GetCheck()==BST_CHECKED)
{
rxcounter+=(len/2);
for(i=0;i<len-1;i+=2)
{
tmp=0;
tmp=(BYTE)(m_TXData3.GetAt(i)>='A'?m_TXData3.GetAt(i)-55:m_TXData3.GetAt(i)-'0');
tmp=(BYTE)(tmp*16+(BYTE)(m_TXData3.GetAt(i+1)>='A'?m_TXData3.GetAt(i+1)-55:m_TXData3.GetAt(i+1)-'0'));
Binbyte.Add(tmp);
}
/*if(len%2!=0)
{
Binbyte.Add((BYTE)(m_TXData.GetAt(len-1)-55));
rxcounter+=1;
}*/
COleVariant var(Binbyte);
m_ctrlComm.put_Output(var);
}
else
{
rxcounter+=len;
m_ctrlComm.put_Output(COleVariant(m_TXData3));
}
str2.Format(_T("Tx:%d"),rxcounter);
m_tx.SetWindowText(str2);
} 这个网上有参考源码的,看是不是没有更新显示 大家帮帮我吧!!!我都弄了几天了都弄不了 ding 已修改好,楼主注意变量的使用.
txcounter,rxcounter
修改文件serialcom20120209ourdev_717079VUNJ90.rar(文件大小:208K) (原文件名:serialcom20120209.rar) 楼主应该是参考了蛮多龚建伟的源代码吧 编译是通过了软件打不开我用的VS2013 稍后 我整理好一个上传 mark一下
页:
[1]