//发送数据正常,在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"
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;
}
//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 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);
}