求下面微机原理程序的说明解释,就是各个语句的功能文字说明?

来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/23 08:16:10
求下面微机原理程序的说明解释,就是各个语句的功能文字说明?求下面微机原理程序的说明解释,就是各个语句的功能文字说明?求下面微机原理程序的说明解释,就是各个语句的功能文字说明?#include#incl

求下面微机原理程序的说明解释,就是各个语句的功能文字说明?
求下面微机原理程序的说明解释,就是各个语句的功能文字说明?


求下面微机原理程序的说明解释,就是各个语句的功能文字说明?
#include
#include
#include
static HANDLE hPort = INVALID_HANDLE_VALUE;
unsigned char lpBuffer[100];
BOOL OpenPort(const int portNum) { 打开串口
DCB\x09dcb;
COMMTIMEOUTS commtimeouts;
\x09TCHAR tszPort[10] = {0};
\x09_stprintf(tszPort, _T("COM%d:"), portNum);
//hPort = CreateFile("COM1", 创建文件
hPort = CreateFile(tszPort,
\x09\x09\x09 GENERIC_READ | GENERIC_WRITE,
\x09\x09\x09 0,\x09\x09\x09\x09\x09\x09\x09// comm devices must be opened w/exclusive-access
\x09\x09\x09 NULL,\x09\x09\x09\x09\x09\x09\x09// no security attrs
\x09\x09\x09 OPEN_EXISTING,\x09\x09\x09\x09// comm devices must use OPEN_EXISTING
\x09\x09\x09 0,\x09\x09\x09\x09\x09\x09\x09// overlapped I/O
\x09\x09\x09 NULL\x09\x09\x09\x09\x09\x09\x09// hTemplate must be NULL for comm devices
\x09\x09\x09 );
if (hPort == INVALID_HANDLE_VALUE) return FALSE;
//fills in a device-control block with the current control settings //加入设备控制模块当前设置参数
if (!GetCommState(hPort, &dcb))
{
\x09CloseHandle(hPort);
\x09\x09hPort = INVALID_HANDLE_VALUE;
\x09return FALSE; //error
}
//fills in a device-control block //加入设备控制模块
dcb.BaudRate = CBR_19200;
dcb.ByteSize = 8;
dcb.Parity = EVENPARITY;
dcb.StopBits = ONESTOPBIT;
\x09dcb.fRtsControl = TRUE;
\x09dcb.fDtrControl = TRUE;
if (!SetCommState(hPort, &dcb))
{
\x09CloseHandle(hPort);
\x09\x09hPort = INVALID_HANDLE_VALUE;
\x09return FALSE;
}
if(!GetCommTimeouts(hPort, &commtimeouts))
{
\x09CloseHandle(hPort);
\x09\x09hPort = INVALID_HANDLE_VALUE;
\x09return FALSE;
}
commtimeouts.ReadIntervalTimeout = 30;
commtimeouts.ReadTotalTimeoutMultiplier = 0;
commtimeouts.ReadTotalTimeoutConstant = 10;
\x09commtimeouts.WriteTotalTimeoutMultiplier = 1;
\x09commtimeouts.WriteTotalTimeoutConstant = 50;
if(!SetCommTimeouts(hPort, &commtimeouts))
{
\x09CloseHandle(hPort);
\x09\x09hPort = INVALID_HANDLE_VALUE;
\x09return FALSE;
}
\x09PurgeComm(hPort, PURGE_TXCLEAR | PURGE_TXCLEAR);
\x09return TRUE;
}
void V2BFClosePort() { //关闭串口
\x09if (hPort != INVALID_HANDLE_VALUE) {
\x09\x09
\x09\x09
\x09\x09CloseHandle(hPort);
\x09\x09hPort = INVALID_HANDLE_VALUE;
\x09}
}
void RecvStream(void) {
\x09int nRecvedBytes = 0;
\x09DWORD dwRead = 0;
\x09
\x09
\x09\x09if (ReadFile(hPort, lpBuffer, strlen(lpBuffer), &dwRead, NULL)) {
\x09\x09\x09
\x09\x09\x09nRecvedBytes = dwRead;
\x09\x09}
\x09\x09
}
BOOL SendStream(IN unsigned char *sendStream, IN int sendLength) {
\x09BOOL fRet = FALSE;
\x09
\x09if (hPort != INVALID_HANDLE_VALUE) {
\x09\x09DWORD dwWritten = 0;
\x09\x09fRet = WriteFile(hPort, sendStream, sendLength, &dwWritten, NULL);
\x09\x09FlushFileBuffers(hPort);
\x09\x09fRet = fRet && ((unsigned long)sendLength == dwWritten);
\x09}
\x09
\x09return fRet;
}