📄 tcp2serialdlg.cpp
字号:
// Tcp2SerialDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Tcp2Serial.h"
#include "Tcp2SerialDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTcp2SerialDlg dialog
CTcp2SerialDlg::CTcp2SerialDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTcp2SerialDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTcp2SerialDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTcp2SerialDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTcp2SerialDlg)
DDX_Control(pDX, IDC_MSG, m_msg);
DDX_Control(pDX, IDC_LIST_CONFIG, m_list);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTcp2SerialDlg, CDialog)
//{{AFX_MSG_MAP(CTcp2SerialDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_MESSAGE( WM_COMMNOTIFY, OnCommNotify )
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTcp2SerialDlg message handlers
BOOL CTcp2SerialDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
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);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
m_nCount = 0;
m_sendflag = 0;
m_sendNetWorkLen = 0;
memset( &m_SerialData, 0, sizeof( m_SerialData ) );
memset( &m_NetWorkData, 0, sizeof( m_NetWorkData ) );
memset( &m_dt1info, 0, sizeof( m_dt1info ) );
memset( &m_dt1data, 0, sizeof( m_dt1data ) );
memset( &m_SendNetData, 0, sizeof( m_SendNetData ) );
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CImageList m_ImageList;
m_ImageList.Create( 1, 16, ILC_COLOR|ILC_MASK, 1, 1 );
m_list.SetTextColor( 0xff0000 );
m_list.ModifyStyle( 0, LVS_REPORT, TRUE );
m_list.SetImageList( &m_ImageList, LVSIL_SMALL );
ASSERT( (m_list.GetStyle()&LVS_SHAREIMAGELISTS ) == 0 );
m_list.SetExtendedStyle( m_list.GetExtendedStyle()|LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_FLATSB );
m_list.DeleteAllItems();
while( m_list.DeleteColumn( 0 ) );
m_list.InsertColumn( 0, "发送字节数", LVCFMT_LEFT, 80 );
m_list.InsertColumn( 1, "接受字节数", LVCFMT_LEFT, 80 );
m_list.InsertColumn( 2, "数据端口IP", LVCFMT_LEFT, 120 );
m_list.InsertColumn( 3, "错误信息", LVCFMT_LEFT, 80 );
m_list.InsertColumn( 4, "数据端口", LVCFMT_LEFT, 60 );
m_list.InsertColumn( 5, "串口", LVCFMT_LEFT, 50 );
m_list.InsertColumn( 6, "波特率", LVCFMT_LEFT, 50 );
m_list.InsertItem( 0, "" );
// TODO: Add extra initialization here
CIniFile config( ".//config.ini" );
info.tcpport = config.ReadInteger( "设置", "TCP端口号", 80000 );
info.comport = config.ReadInteger( "设置", "串口号", 1 );
info.baudrate = (DWORD)config.ReadInteger( "设置", "波特率", 9600 );
info.bytesize = (BYTE)config.ReadInteger( "设置", "数据位", 8 );
info.stopbit = (BYTE)config.ReadInteger( "设置", "停止位", ONESTOPBIT );
info.parity = (BYTE)config.ReadInteger( "设置", "校验位", NOPARITY );
info.isClient = (BOOL)config.ReadInteger( "设置", "是否为客户端", TRUE );
info.isLogin = (BOOL)config.ReadInteger( "设置", "写入日志", FALSE );
info.serverIp = config.ReadString( "设置", "连接服务器IP", "192.168.100.71" );
info.logFilename = config.ReadString( "设置", "日志文件名", "tcp2serial.log" );
SetTimer( 999, 100, NULL );
OnStart( !info.isClient );
return TRUE; // return TRUE unless you set the focus to a control
}
void CTcp2SerialDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTcp2SerialDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
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;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTcp2SerialDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTcp2SerialDlg::OnStart( BOOL isServer /* = TRUE */ )
{
DCB dcb;
char text[32];
m_tcp.SetPart( this );
m_tcpClient.SetPart( this );
if ( isServer )
{
m_tcp.Create( info.tcpport, SOCK_STREAM, FD_READ|FD_WRITE|FD_CLOSE|FD_ACCEPT);
m_tcp.Listen();
}
else
{
m_tcpClient.Create();
m_tcpClient.Connect( info.serverIp, info.tcpport );
}
sprintf( text, "%d", info.tcpport );
m_list.SetItemText( 0, 4, text );
dcb.BaudRate = info.baudrate; dcb.ByteSize = info.bytesize;
dcb.StopBits = info.stopbit; dcb.Parity = info.parity;
if ( m_com.Open( AfxGetApp()->m_pMainWnd->m_hWnd, info.comport, dcb ) )
{
sprintf( text, "串口%d", info.comport );
m_list.SetItemText( 0, 5, text );
sprintf( text, "%d", info.baudrate );
m_list.SetItemText( 0, 6, text );
}
else
{
m_list.SetItemText( 0, 3, "串口打开失败!" );
}
}
void CTcp2SerialDlg::OnAccecp()
{
CString str;
sockaddr_in addr;
int size = sizeof(sockaddr);
int bResult = m_tcp.Accept( m_tcpClient, (sockaddr*)&addr, &size );
str.Format( "%s", inet_ntoa( addr.sin_addr ) );
m_list.SetItemText( 0, 2, str );
}
void CTcp2SerialDlg::OnConnect()
{
m_list.SetItemText( 0, 2, info.serverIp );
}
void CTcp2SerialDlg::OnClose()
{
m_tcpClient.Close();
}
void CTcp2SerialDlg::OnCommNotify( WPARAM wParam, LPARAM lParam )
{
if ( !m_com.m_bIsOpened || ( wParam&EV_RXCHAR ) != EV_RXCHAR )
{
SetEvent( m_com.m_hPostMsgEvent );
return;
}
BYTE buffer[512];
int len = m_com.ReadData( buffer, 512 );
if ( len > 0 )
{
if ( OnProcessSerialComData( buffer, len ) )
{
if ( m_sendflag == 0 )
{
OnWriteToNetWork();
}
else
{
OnWriteToSerial();
}
m_nCount = 0;
}
}
}
void CTcp2SerialDlg::OnReceive()
{
BYTE buffer[512];
memset( buffer, 0, sizeof( buffer ) );
int len = m_tcpClient.Receive( buffer, 512 );
if ( len > 0 )
{
static count = 0;
count += len;
CString str;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -