📄 spserialport.cpp
字号:
// SPSerialPort.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "PSerialPort.h"
//Global Variables:
HWND g_hwndCB;
HINSTANCE g_hInst;
//串口类
CPSerialPort *pSerialPort;
//串口相关变量
BOOL Open;
int PortNo,BaudRate,DataBits,StopBits,Parity;
//串口数据消息
#define WM_NEW_DATA_ARRIVE WM_USER+105
//串口数据接收回调函数
void OnDataArrive(char *data,int length,DWORD userdata)
{
HWND pWnd=(HWND )userdata;
::SendMessage(pWnd,WM_NEW_DATA_ARRIVE,(WPARAM)data,LPARAM(length));
}
static SHACTIVATEINFO s_sai;
// Forward declarations of functions included in this code module:
LRESULT CALLBACK MainDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK Setup (HWND, UINT, WPARAM, LPARAM);
HWND CreateRpCommandBar(HWND);
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
g_hInst = hInstance;
//申请串口类
pSerialPort=new CPSerialPort();
DialogBox(hInstance, (LPCTSTR)IDD_MAIN, NULL, (DLGPROC)MainDlgProc);
//释放
if(pSerialPort)
{
delete pSerialPort;
}
return 0;
}
// Mesage handler for the SerialPort
LRESULT CALLBACK MainDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
SHINITDLGINFO shidi;
TCHAR Port[16];
unsigned char Send[256];
unsigned short SendText[256];
DWORD dwCharToWrite,dwBytesWritten;
int i;
unsigned char Rece[1024];
unsigned short ReceText[1024];
int BytesReceived;
int TextLength;
unsigned char *receive;
HWND hEdit;
switch (message)
{
case WM_INITDIALOG:
// Create a Done button and size it.
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
g_hwndCB = CreateRpCommandBar(hDlg);
//初始化变量
PortNo=1;
BaudRate=9600;
DataBits=8;
StopBits=ONESTOPBIT;
Parity=NOPARITY;
Open=FALSE;
return TRUE;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDOK:
pSerialPort->ClosePort();
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
break;
case IDM_TOOLS_EXIT:
pSerialPort->ClosePort();
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
break;
case IDM_TOOLS_OPEN:
if(!Open)
{
wsprintf(Port,TEXT("COM%d:"),PortNo);
Open=pSerialPort->OpenPort(Port,BaudRate,DataBits,StopBits,Parity,OnDataArrive,(DWORD)(hDlg));
if(Open)
{
pSerialPort->Activate();
}
}
break;
case IDM_TOOLS_CLOSE:
pSerialPort->ClosePort();
Open=FALSE;
break;
case IDM_TOOLS_SEND:
dwCharToWrite=GetDlgItemText(hDlg,IDC_EDIT_SEND,(LPWSTR)SendText,256);
if(Open&&dwCharToWrite!=0)
{
for(i=0;i<(int)dwCharToWrite;i++)
{
Send[i]=(unsigned char)SendText[i];
}
dwBytesWritten=pSerialPort->WritePort((char*)Send,dwCharToWrite);
if(dwBytesWritten==0)
{
MessageBox(hDlg,_T("无法向端口写入数据!"),_T("错误"),MB_OK);
}
}
break;
case IDM_CLEAR_SEND:
SetDlgItemText(hDlg,IDC_EDIT_SEND,TEXT(""));
break;
case IDM_CLEAR_RECEIVE:
SetDlgItemText(hDlg,IDC_EDIT_RECEIVE,TEXT(""));
break;
case IDM_COM1:
PortNo=1;
break;
case IDM_COM2:
PortNo=2;
break;
case IDM_COM3:
PortNo=3;
break;
case IDM_COM4:
PortNo=4;
break;
case IDM_COM5:
PortNo=5;
break;
case IDM_COM6:
PortNo=6;
break;
case IDM_COM7:
PortNo=7;
break;
case IDM_COM8:
PortNo=8;
break;
case IDM_COM9:
PortNo=9;
break;
case IDM_BAUD_1200:
BaudRate=1200;
break;
case IDM_BAUD_2400:
BaudRate=2400;
break;
case IDM_BAUD_4800:
BaudRate=4800;
break;
case IDM_BAUD_9600:
BaudRate=9600;
break;
case IDM_BAUD_19200:
BaudRate=19200;
break;
case IDM_BAUD_38400:
BaudRate=38400;
break;
case IDM_BAUD_57600:
BaudRate=57600;
break;
case IDM_BAUD_115200:
BaudRate=115200;
break;
case IDM_DATABITS_7:
DataBits=7;
break;
case IDM_DATABITS_8:
DataBits=8;
break;
case IDM_STOP_BITS_ONE:
StopBits=ONESTOPBIT;
break;
case IDM_STOP_BITS_ONEHALF:
StopBits=ONE5STOPBITS;
break;
case IDM_STOP_BITS_TWO:
StopBits=TWOSTOPBITS;
break;
case IDM_PARITY_NONE:
Parity=NOPARITY;
break;
case IDM_PARITY_ODD:
Parity=ODDPARITY;
break;
case IDM_PARITY_EVEN:
Parity=EVENPARITY;
break;
case IDM_PARITY_MARK:
Parity=MARKPARITY;
break;
case IDM_PARITY_SPACE:
Parity=SPACEPARITY;
break;
default:
break;
}
}
break;
case WM_NEW_DATA_ARRIVE:
receive=(unsigned char *)wParam;
BytesReceived=(int)lParam;
//更新接收区显示
ZeroMemory(ReceText,sizeof(ReceText));
TextLength=GetDlgItemText(hDlg,IDC_EDIT_RECEIVE,(LPWSTR)ReceText,1024);
if(TextLength+BytesReceived>1024)
{
if(BytesReceived>1024)
{
BytesReceived=1024;
}
for(i=0;i<BytesReceived;i++)
{
ReceText[i]=receive[i];
}
SetDlgItemText(hDlg,IDC_EDIT_RECEIVE,(LPWSTR)ReceText);
TextLength=BytesReceived;
}
else
{
for(i=0;i<BytesReceived;i++)
{
ReceText[i+TextLength]=receive[i];
}
SetDlgItemText(hDlg,IDC_EDIT_RECEIVE,(LPWSTR)ReceText);
TextLength=TextLength+BytesReceived;
}
//卷动
hEdit=GetDlgItem(hDlg,IDC_EDIT_RECEIVE);
SendMessage(hEdit,EM_SETSEL,(WPARAM)((INT)TextLength),(LPARAM)((INT)TextLength));
SendMessage(hEdit,EM_SCROLLCARET,NULL,NULL);
break;
}
return FALSE;
}
HWND CreateRpCommandBar(HWND hwnd)
{
SHMENUBARINFO mbi;
memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hwnd;
mbi.nToolBarId = IDR_MENU;
mbi.hInstRes = g_hInst;
mbi.nBmpId = 0;
mbi.cBmpImages = 0;
if (!SHCreateMenuBar(&mbi))
return NULL;
return mbi.hwndMB;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -