RS232 串口接收不到数据
//发送数据正常,在HyperTerminal中可以接收显示
//从HyperTermianl发送,READBYTE()就是没有结果,请各位大侠不吝赐教.
// RS232.cpp : Defines the entry point for the console application.
//(VS2010)Project-->Property-->Configuration Properties-->Character Set:"Use Multi-Byte Character Set"
#include "stdafx.h"
/***********************************************************************
Module Name:
***********************************************************************/
#include <windows.h>
#include<stdio.h>
#include<string.h>
HANDLE hPort; // Device Handler
int OPENCOM(LPTSTR lpszPortName);
int CLOSECOM(HANDLE hCommPort);
int READBYTE(char* pszBuf,DWORD dwIncommingReadSize);
int SENDBYTE(char* pszBuf,DWORD dwNumBytesWritten);
/***********************************************************************
Function:OPENCOM(LPTSTR lpszPortName)
Parameter:
Date:
Ver:
***********************************************************************/
int OPENCOM(LPTSTR lpszPortName)
{
DWORD dwError;
hPort = CreateFile(lpszPortName, // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE, // Access (read/write) mode
0, // Share mode
NULL, // Pointer to the securityattribute
OPEN_EXISTING, // How to open the serial port
0, // Port attributes(Synchronization mode)
NULL); // Handle to port with attributeto copy
if(hPort==INVALID_HANDLE_VALUE )
{
dwError = GetLastError (); // Could not open the port.
MessageBox (NULL, TEXT("Unable to open the port"), TEXT("Error"), MB_OK);
return 0;
}
if(!SetupComm(hPort, 1024, 1024)) //Setup RX/TX memory
{
dwError = GetLastError (); // Could not Setup the port.
MessageBox (NULL, TEXT("Unable to Setup the port"), TEXT("Error"), MB_OK);
return 0;
}
//COMMCONFIG dcbSerParams;
//PortDCB = dcbSerParams.dcb;
DCB PortDCB;
PortDCB.DCBlength = sizeof(DCB);
if(GetCommState(hPort, &PortDCB)) /* Configuring Serial Port Settings */
{
PortDCB.BaudRate = CBR_9600;
PortDCB.Parity = NOPARITY;
PortDCB.ByteSize = 8;
PortDCB.StopBits = ONESTOPBIT;
/* Misc settings */
PortDCB.fBinary = TRUE;
PortDCB.fDtrControl = DTR_CONTROL_DISABLE;
PortDCB.fRtsControl = RTS_CONTROL_DISABLE;
PortDCB.fOutxDsrFlow = FALSE;
PortDCB.fOutxCtsFlow = FALSE;
PortDCB.fDsrSensitivity = FALSE;
PortDCB.fAbortOnError = TRUE;
//Configure the port according to the specifications of the DCB structure.
if(!SetCommState (hPort, &PortDCB))
{
dwError = GetLastError (); //Could not create the read thread.
MessageBox (NULL, TEXT("Unable to SetCommState the serial port"), TEXT("Error"), MB_OK);
return 0;
}
else
{
COMMTIMEOUTS CommTimeouts;
if(GetCommTimeouts(hPort, &CommTimeouts)) // Configuring Read & Write Time Outs
{
CommTimeouts.ReadIntervalTimeout = 20; //Read Timout
CommTimeouts.ReadTotalTimeoutConstant = 100;
CommTimeouts.ReadTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 100;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
if(!SetCommTimeouts(hPort, &CommTimeouts))
{
dwError = GetLastError ();
MessageBox(NULL, TEXT("Unable to set the time-out parameters"), TEXT("Error"), MB_OK);
return 0;
}
}
else
{
dwError = GetLastError ();
MessageBox (NULL, TEXT("Unable to GetCommTimeouts the serial port"), TEXT("Error"), MB_OK);
return 0;
}
}
}
else
{
dwError = GetLastError (); //Could not get comm status.
MessageBox (NULL, TEXT("Unable to get the port status"), TEXT("Error"), MB_OK);
return 0;
}
}
/***********************************************************************
Function:int SENDBYTE (BYTE Byte)
Parameter:
Date:
Ver:
Note: Synchronization Communication (Write Bytes)
***********************************************************************/
int SENDBYTE(char* pszBuf,DWORD dwNumBytesWritten)
{
DWORD dwError;
if(!WriteFile (hPort, // Port handle
pszBuf, // Pointer to the data to write
dwNumBytesWritten, // Number of bytes to write
&dwNumBytesWritten, // Pointer to the number of bytes written
NULL)) // Must be NULL for Windows CE
{
// WriteFile failed. Report error.
dwError = GetLastError ();
return 0;
}
return 1;
}
/***********************************************************************
Function:int mFlush()
Parameter:
Date:
Ver:
Note: Synchronization Communication
***********************************************************************/
int mFlush()
{
DWORD dwFlag=PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR;
if(PurgeComm(hPort,dwFlag))
return 0;
else
return 1;
}
/***********************************************************************
Function:int READBYTE(char* pszBuf,DWORD dwBytesToRead);
Parameter:
Date:
Ver:
Note: Synchronization Communication (Read Bytes)
***********************************************************************/
int READBYTE(char* pszBuf,DWORD dwIncommingReadSize)
{
DWORD dwError;
COMSTAT commStat; //
//OVERLAPPED m_osRead; //Asyn mode
//memset(pszBuf,0,sizeof(OVERLAPPED));//
memset(pszBuf,0,sizeof(pszBuf));
if(hPort == INVALID_HANDLE_VALUE)
{
// Could not open the port.
dwError = GetLastError ();
MessageBox (NULL, TEXT("Unable to open the port"), TEXT("Error"), MB_OK);
return 0;
}
else{
//COMSTAT commStat;
ClearCommError(hPort,&dwError,&commStat); //if dwError is not null.the port is in the error state
dwIncommingReadSize = min(dwIncommingReadSize,(DWORD)commStat.cbInQue) ;
printf("......commStat.cbInQue %d ......\n",dwIncommingReadSize);
}
if(!dwError)
{
//ReadFile(hPort, pszBuf, dwIncommingReadSize, &dwIncommingReadSize, &m_osRead);
ReadFile(hPort, pszBuf, dwIncommingReadSize, &dwIncommingReadSize, NULL);
printf(".......< Complete Received > ........\n");
}
else
{
dwError = GetLastError ();
printf("Read Error!\n");
return 0;
}
return 1;
}
/***********************************************************************
Function :int CLOSECOM (HANDLE hPort)
Parameter:
Date:
Ver:
***********************************************************************/
int CLOSECOM(HANDLE hPort)
{
DWORD dwError;
if(hPort!=INVALID_HANDLE_VALUE)
{
//Close the communication port.
if(!CloseHandle(hPort))
{
dwError=GetLastError();
return 0;
}
else
{
hPort=INVALID_HANDLE_VALUE;
return 1;
}
}
CloseHandle(hPort);
return 0;
}
//int _tmain(int argc, _TCHAR* argv[])
void main()
{
char *Temp = "Hello,World!\n\r";
char buff[1024];
DWORD dwIncommingReadSize=1024;
LPTSTR lpszPortName = "COM3";
OPENCOM(lpszPortName);
//SENDBYTE(Temp,strlen(Temp)); //Send string ...
READBYTE(buff,dwIncommingReadSize);
printf("%s",buff);
Sleep(100);
mFlush();
CLOSECOM(hPort);
system("PAUSE");
//return 0;
}