📄 commcommunicator.cpp
字号:
/** @file
* CommCommunicator.cpp
*
* INTEL CONFIDENTIAL
* Copyright 2006 Intel Corporation All Rights Reserved.
* The source code contained or described herein and all documents related
* to the source code ("Material") are owned by Intel Corporation or its
* suppliers or licensors. Title to the Material remains with Intel Corporation
* or its suppliers and licensors. The Material contains trade secrets and
* proprietary and confidential information of Intel or its suppliers and licensors.
* The Material is protected by worldwide copyright and trade secret laws and
* treaty provisions. No part of the Material may be used, copied, reproduced,
* modified, published, uploaded, posted, transmitted, distributed, or disclosed
* in any way without Intel抯 prior express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be express
* and approved by Intel in writing.
*
*/
#include "StdAfx.h"
#include <crtdbg.h>
#include "CommCommunicator.h"
CommCommunicator::CommCommunicator(void)
{
}
CommCommunicator::~CommCommunicator(void)
{
}
/* This function package the incoming command string into protocol
standard format and send it to Rfid device
@return status code
@retval #true command written successfully to the port.
@retval #false if writing command to the port failed.
*/
bool CommCommunicator::PackageAndWrite(const char *cCmd)
{
char sendBuf[2048 + 16], hex[2];
size_t len;
bool bRet = true;
if (strlen(cCmd) >= 2048) {
//data too big
return false;
}
memset(sendBuf, 0, 2048+16);
strcpy_s(sendBuf, 2048 + 16, "0100000304"); // 01 (SOF) + Byte Count + 00 + 0304
strcat_s(sendBuf, 2048 + 16, cCmd);
strcat_s(sendBuf, 2048 + 16, "0000"); //EOF
len = strlen(sendBuf) / 2;
ByteToHex(hex, (unsigned char) len);
sendBuf[2] = hex[0];
sendBuf[3] = hex[1];
if (!PortWrite(sendBuf))
{
//PackageAndWrite: Write failed.
bRet = false;
}
return (bRet);
}
/*This function write the command to the COM port.
@return status code
@retval #true command written successfully to the port.
@retval #false if writing command to the port failed.
*/
bool CommCommunicator::PortWrite(const char *sendBuf)
{
DWORD dwCount;
BOOL BRet = WriteFile(mComHandle, sendBuf, (DWORD) strlen(sendBuf), &dwCount, NULL);
if (!BRet) {
//COM port WriteFile failed.
return (false);
}
if (dwCount != strlen(sendBuf)) {
//PortWrite: COM Port WriteFile failed, not all bytes written: GetLastError %i", GetLastError()
//PortWrite: Bytes written: %i Expected: %i", dwCount, strlen(sendBuf)
return (false);
}
return (true);
}
/* This function convert byte data into hexadecimal.
*/
void CommCommunicator::ByteToHex(char *buf, unsigned char ucVal)
{
unsigned char ucLow = (ucVal & 15) + 48;
unsigned char ucHigh = (ucVal >> 4) + 48;
if (ucLow > 57) ucLow += 7;
if (ucHigh > 57) ucHigh += 7;
buf[0] = ucHigh;
buf[1] = ucLow;
}
/*This function read whole response data at one time from COM port.
@return status code
@retval #true able to read the data successfully from the port.
@retval #false if read from COM port failed.
*/
bool CommCommunicator::portRead(char *data, DWORD *len)
{
//DWORD portReadCount = len;
/*~~~~~~~*/
//DWORD nn;
//BOOL BRet;
//bool bRet = false;
///*~~~~~~~*/
//BRet = ReadFile(mComHandle, data, *len, &nn, NULL);
//if(BRet == 0) nn = 0;
//if(nn == 0)
//{
// *data = 0;
//}
//else
//{
// if(nn > *len)
// {
// nn = *len;
// }
// else
// {
// *len = nn;
// }
// data[nn] = 0;
// bRet = true;
//}
//return bRet;
bool bRet = true;
DWORD dwBytesRead;
char cByte[1];
DWORD dwIndex = 0;
DWORD bufferSize = *len;
_ASSERT(data != NULL);
_ASSERT(len != NULL);
_ASSERT(bufferSize > 0);
// Initialize the return value
*len = 0;
while (true)
{
BOOL fileRead = ReadFile(mComHandle, cByte, 1, &dwBytesRead, NULL);
if ((TRUE == fileRead) && (1 == dwBytesRead))
{
if (dwIndex >= bufferSize)
{
//portRead: Buffer not large enough.
bRet = false;
data[dwIndex - 1] = NULL;
break;
}
data[dwIndex] = *cByte;
//Verify if a prompt('> ') is found
if (dwIndex >= 1)
{ // look for this '> '
if ((data[dwIndex] == 32) & (data[dwIndex - 1] == 62))
{
// Found the prompt, return data till ">"
bRet = true;
data[dwIndex] = NULL; *len = dwIndex;
break;
}
}
dwIndex++;
}
else
{
// Read timed out or read failed
data[dwIndex] = NULL;
*len = dwIndex;
bRet = false;
break;
}
}
return bRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -