您的位置:首页 > 其它

VC菜菜鸟-创建一个即时串口通信程序

2012-07-10 00:39 323 查看
//编者注:串口接收基于多线程任务。

#include <windows.h>

#include <windowsx.h>

#include "main.h"

#include "dialogs.h"

#include "resource.h"

#include "stdio.h"

HANDLE hCom;

DWORD dwError;

DCB LpdcbCom;

OVERLAPPED m_ov;

//Compile error using LPDCB

char ScomBuf[256];

DWORD WINAPI PTRead(LPVOID pParam)

{

DWORD EV_COMM;

char str[256];

char ByteRead[256];

DWORD BytesSent;

COMSTAT ComSta;

DWORD Counter;

DWORD ErrorNum;

while(1)

{

( WaitCommEvent(hCom,&EV_COMM,&m_ov));

if( EV_COMM == EV_RXCHAR )

{

// MessageBox(NULL,"RX Event Happened...","",MB_OK);

EV_COMM = 0;

// sprintf(ByteRead,"\0");

for(int tmp=0; tmp<256; tmp++)

ByteRead[tmp]= '\0';

ClearCommError(hCom,&ErrorNum,&ComSta);

ReadFile(hCom, // Handle to COMM port

&ByteRead, // RX Buffer Pointer

ComSta.cbInQue, // Read one byte

&BytesSent, // Stores number of bytes read

&m_ov); // pointer to the m_ov structure

sprintf(ByteRead,"%s\0",ByteRead);

SetDlgItemText((HWND )(pParam),IDC_TEXT,ByteRead);//Note: HWND is a Pointer...

//MessageBox(NULL,ByteRead,"",MB_OK);

/*

sprintf(str,"%c",ByteRead);

if( ByteRead == '\0' )

{

MessageBox(NULL,str,NULL,MB_OK);

break;

}

*/

}

// MessageBox(NULL,"Sub-thread","",MB_OK);

// Sleep(1);

}

/*

while(1)

{

ReadFile(hCom, // Handle to COMM port

&ByteRead, // RX Buffer Pointer

1, // Read one byte

&BytesSent, // Stores number of bytes read

&m_ov); // pointer to the m_ov structure

sprintf(str,"%c",ByteRead);

MessageBox(hwnd,str,NULL,MB_OK);

if( ByteRead == '\0' ) break;

}

*/

}

void OpenScom()

{

DWORD Error=0;

CloseHandle(hCom); //Avoid Open Scomm too much time in the same thread.

hCom = CreateFile( "COM7",\

GENERIC_READ | GENERIC_WRITE,\

0,\

NULL,\

OPEN_EXISTING,

FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,\

NULL

);

if( hCom == INVALID_HANDLE_VALUE)

{

if( 5 == GetLastError() )

{

sprintf(ScomBuf,"%s","串口已被占用");

MessageBox(NULL,ScomBuf,NULL,MB_OK);

}

ClearCommError(hCom,&Error,NULL);

// sprintf(ScomBuf,"%u",dwError);

}

GetCommState(hCom,&LpdcbCom);

BuildCommDCB("baud=9600 parity=N data=8 stop=1",&LpdcbCom);

SetCommState(hCom,&LpdcbCom);

}

void InitScom()

{

}

BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

{

switch(uMsg)

{

//BEGIN MESSAGE CRACK

HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);

HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);

HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);

//END MESSAGE CRACK

}

return FALSE;

}

////////////////////////////////////////////////////////////////////////////////

// Main_OnInitDialog

BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)

{

// Set app icons

HICON hIcon = LoadIcon((HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE) ,MAKEINTRESOURCE(IDI_ICONAPP));

SendMessage(hwnd, WM_SETICON, TRUE, (LPARAM)hIcon);

SendMessage(hwnd, WM_SETICON, FALSE, (LPARAM)hIcon);

// SetDlgItemText(hwnd,IDC_TEXT,"jr");

CreateThread(NULL,0,PTRead,hwnd,0,NULL);

//

// Add initializing code here

//

return TRUE;

}

////////////////////////////////////////////////////////////////////////////////

// Main_OnCommand

void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)

{

DWORD BytesSent = 0;

char ByteRead;

char str[255];

switch(id)

{

case IDC_SEND:

// MessageBox(hwnd,"TEST","TEST",MB_OK);

// TransmitCommChar(hCom,'J');

WriteFile(hCom, // Handle to COMM Port

"Hello,world.", // Pointer to message buffer in calling finction

12, // Length of message to send

&BytesSent, // Where to store the number of bytes sent

&m_ov ); // Overlapped structure

break;

case IDC_OK:

// MessageBox(hwnd,TEXT("You clicked OK!"),TEXT("SCOM"),MB_OK);

OpenScom();

SetCommMask(hCom,EV_RXCHAR);

// EndDialog(hwnd, id);

break;

case IDC_CANCEL:

// MessageBox(hwnd,TEXT("You clicked Cancel!"),TEXT("SCOM"),MB_OK);

CloseHandle(hCom);

// EndDialog(hwnd, id);

break;

default:break;

}

}

////////////////////////////////////////////////////////////////////////////////

// Main_OnClose

void Main_OnClose(HWND hwnd)

{

EndDialog(hwnd, 0);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: