- 论坛徽章:
- 0
|
我刚刚接触到编程。这是一个具有接受和发送数据功能的串口程序,有错误,我不会改,恳请各位高手帮我更正一下 谢谢! 急用!
#include "stdafx.h"
#include "stdio.h"
#include "resource.h"
#define MAX_LOADSTRING 100
#define BUFFER_SIZE 500
#define MSG(szMsg) MessageBox (NULL, szMsg , TEXT("Error", MB_OK)
HANDLE m_hCom;
BOOL OpenSerial(LPCTSTR lpszDevice);
DWORD WriteDataSerial(LPCVOID pdata, DWORD len);
DWORD ReadDataSerial (TCHAR ReceiveBuf[BUFFER_SIZE]);
void CloseSerial();
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{ LONG LastError=ERROR_SUCCESS;
LastError=OpenSerial("COM1"
if(LastError!=ERROR_SUCCESS)
{
MessageBox (NULL, TEXT("OpenSerial:COM1 failed",TEXT("Error", MB_OK);
return FALSE;
}
LastError=WriteDataSerial("Hello World"
if(LastError!=ERROR_SUCCESS)
{
MessageBox(NULL, TEXT("WriteDataSerial:send data failed",TEXT("Error", MB_OK);
return FALSE;
}
LastError=ReadDataSerial (TCHAR ReceiveBuf[BUFFER_SIZE]);
DWORD dwBytes;
const char *Byte = "Hello World";
ReadFile(m_hCom, &Byte,11, &dwBytes, 0);
if( LastError!=ERROR_SUCCESS)
{
MessageBox(NULL, TEXT("ReadDataSerial:read data failed",TEXT("Error", MB_OK);
return FALSE;
}
CloseSerial();
return 0;
}
BOOL OpenSerial(LPCTSTR lpszPortNum)
{
DCB dcb;
BOOL bSuccess;
m_hCom = CreateFile(lpszPortNum,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL);
if ( m_hCom == INVALID_HANDLE_VALUE )
{
MessageBox (NULL, TEXT("OpenSerial(): CreateFile() failed, invalid handle value!", TEXT("Error"), MB_OK);
return FALSE;
}
bSuccess = GetCommState(m_hCom, &dcb);
if ( ! bSuccess )
{
MessageBox (NULL, TEXT("OpenSerial(): GetCommState() failed!"), TEXT("Error"), MB_OK);
CloseSerial();
return FALSE;
}
dcb.BaudRate = CBR_9600 ;
dcb.ByteSize = 8 ;
dcb.Parity = NOPARITY ;
dcb.StopBits = ONESTOPBIT ;
bSuccess = SetCommState(m_hCom, &dcb);
if ( ! bSuccess )
{
MessageBox (NULL, TEXT("OpenSerial(): SetCommState() failed!"), TEXT("Error"), MB_OK);
CloseSerial();
return FALSE ;
}
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts(m_hCom, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 10;
CommTimeouts.ReadTotalTimeoutConstant = 10;
CommTimeouts.WriteTotalTimeoutMultiplier = 50;
CommTimeouts.WriteTotalTimeoutConstant = 100;
if (!SetCommTimeouts(m_hCom, &CommTimeouts))
{
return FALSE;
}
return TRUE ;
}
DWORD WriteDataSerial(LPCVOID pdata, DWORD len)
{
BOOL bSuccess ;
DWORD written = 0 ;
if ( len < 1 )
return(0);
bSuccess = WriteFile(m_hCom,
pdata,
len,
&written,
NULL);
if ( len != written )
{
MessageBox (NULL, TEXT("WriteDataSerial(): WriteFile() failed\n"), TEXT("Error"), MB_OK);
return(0);
}
return written;
}
DWORD ReadDataSerial(TCHAR ReceiveBuf[BUFFER_SIZE])
{
HANDLE hPort = m_hCom;
BYTE Byte;
int iCounter = 0;
DWORD dwBytes;
SetCommMask (hPort, EV_RXCHAR|EV_TXEMPTY);
if (m_hCom!= INVALID_HANDLE_VALUE)
{
DWORD dwCommStatus;
DWORD dwlength;
WaitCommEvent(hPort, &dwCommStatus, 0);
do
{
ReadFile(hPort, &Byte, dwlength, &dwBytes, 0);
if(dwBytes == dwlength)
{
ReceiveBuf[iCounter++] = Byte;
if(iCounter == BUFFER_SIZE)
{
MessageBox (NULL, TEXT("Error ReadDataSerial(): buffer full!\n"), TEXT("Error"), MB_OK);
return 0;
}
}
} while (dwBytes == dwlength);
iCounter = 0;
}
return 1;
}
void CloseSerial(void)
{
if ( m_hCom >; 0 )
{
CloseHandle(m_hCom);
}
m_hCom = 0;
} |
|