最近刚开始学串口编程,从网上下了一个API编程的例子进行编译,但出现了问题,编译都能通过,但用串口调试助手调试时,只能收数据,只要点发送,程序就死了,不知道问题出在哪?希望达人能帮我解决,源程序如下:
SerialPortControl.h
#ifndef _SERIAL_PORT_CONTROL_H
#define _SERIAL_PORT_CONTROL_H#define COM_RECVDATA WM_USER+1000//自定义消息extern HANDLE hCom; //全局变量,串口句柄
extern HANDLE hCommThread; //全局变量,串口线程
extern HANDLE hReadEvent;
//串口监视线程控制函数
extern DWORD WINAPI SerialPort1ThreadProcess(HWND hWnd);
//打开并设置PC串口1(COM1)
extern BOOL OpenSerialPort1();#endifSerialPortControl.cpp
#include "StdAfx.h"
#include "SerialPortControl.h"HANDLE hCom; //全局变量,串口句柄
HANDLE hCommThread; //全局变量,串口线程
HANDLE hReadEvent;
BOOL OpenSerialPort1()
{
//打开并设置COM1
hCom=CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0,NULL , OPEN_EXISTING, 0, NULL);
if (hCom==(HANDLE)-1)
{
AfxMessageBox("打开COM1失败");
return false;
}
else
{
DCB wdcb;
wdcb.DCBlength=sizeof(DCB);
GetCommState (hCom, &wdcb);
wdcb.BaudRate=9600;//波特率:9600,其他:不变
SetCommState (hCom, &wdcb);

COMMTIMEOUTS CommTimeouts;
GetCommTimeouts(hCom,&CommTimeouts); CommTimeouts.ReadIntervalTimeout=MAXDWORD;
CommTimeouts.ReadTotalTimeoutConstant=0;
CommTimeouts.ReadTotalTimeoutMultiplier=0;
CommTimeouts.WriteTotalTimeoutConstant=1000;
CommTimeouts.WriteTotalTimeoutMultiplier=10; SetCommTimeouts(hCom,&CommTimeouts);

PurgeComm(hCom, PURGE_TXCLEAR|PURGE_RXCLEAR);
}
return true;
}
//以一个线程不同监控串口行接收的数据
DWORD WINAPI SerialPort1ThreadProcess( HWND hWnd)//主窗口句柄
{
char str[101];
DWORD wCount; //读取的字节数
DWORD dwCommModemStatus; while(1)
{
ReadFile(hCom,str, 100, &wCount, NULL);
if(wCount > 0) //收到数据
{
str[wCount] = '\0';
::PostMessage(hWnd, COM_RECVDATA, (unsigned int) str, wCount); 
//发送消息给对话框主窗口,以进行接收内容的显示
}
//}
}
return TRUE;
}// commtestlastDlg.cpp : implementation file
//#include "stdafx.h"
#include "commtestlast.h"
#include "commtestlastDlg.h"
#include "SerialPortControl.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass 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()/////////////////////////////////////////////////////////////////////////////
// CCommtestlastDlg dialogCCommtestlastDlg::CCommtestlastDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCommtestlastDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCommtestlastDlg)
m_send = _T("");
m_recv = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CCommtestlastDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCommtestlastDlg)
DDX_Text(pDX, IDC_SEND_EDIT, m_send);
DDX_Text(pDX, IDC_RECV_EDIT, m_recv);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CCommtestlastDlg, CDialog)
//{{AFX_MSG_MAP(CCommtestlastDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEND_BUTTON, OnSendButton)
ON_BN_CLICKED(IDC_CLEAR_BUTTON, OnClearButton)
ON_MESSAGE(COM_RECVDATA, OnRecvData)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CCommtestlastDlg message handlersBOOL CCommtestlastDlg::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
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
OpenSerialPort1();
    //启动串口监视线程
DWORD threadID;
hCommThread = CreateThread((LPSECURITY_ATTRIBUTES)NULL, 0,
(LPTHREAD_START_ROUTINE)SerialPort1ThreadProcess, AfxGetMainWnd()->m_hWnd, 0, &threadID);
if (hCommThread == NULL)
{
::AfxMessageBox("创建串口1处理线程失败");
::PostQuitMessage(0);
}


return TRUE;  // return TRUE  unless you set the focus to a control
}void CCommtestlastDlg::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 CCommtestlastDlg::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 CCommtestlastDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CCommtestlastDlg::OnSendButton() 
{
// TODO: Add your control notification handler code here
    if(hCommThread!=INVALID_HANDLE_VALUE)
{

UpdateData(true);
    
DWORD wCount = 0;

WriteFile(hCom, m_send, m_send.GetLength(), &wCount, NULL);//发送数据

}



}void CCommtestlastDlg::OnClearButton() 
{
// TODO: Add your control notification handler code here
m_send = "";
UpdateData(false);


}
//接收数据后(通过监听线程发来的用户自定义消息)显示
void CCommtestlastDlg::OnRecvData(WPARAM wParam, LPARAM lParam)
{
CString recvStr((char *)wParam);
m_recv += recvStr;
UpdateData(false);
}怀疑是线程的问题,但是解决不了,求助!!