**程序说明:**VSwin32命令控制台程序 调用串口,可以根据自己需要配置串口,完成windows与单片机的数据传输。可作为开发参考使用。
直接先贴代码
//32与单片机通信,差不多成功了
#include #include #include #include using namespace std; HANDLE hComm; OVERLAPPED OverLapped; COMSTAT Comstat; DWORD dwCommEvents; char g_UartRxBuffer[6] = { 0x0d,1,2,3,4,0x0a }; //int g_UartRxBuffer[0] = 0x0d;// //int g_UartRxBuffer[1] = 99; //int g_UartRxBuffer[2] = 0; //int g_UartRxBuffer[3] = 0; //int g_UartRxBuffer[4] = 99; //int g_UartRxBuffer[5] = 0x0a; bool OpenPort(); //打开串口 bool SetupDCB(int rate_arg); //设置DCB bool SetupTimeout(DWORD ReadInterval, DWORD ReadTotalMultiplier, DWORD ReadTotalConstant, DWORD WriteTotalMultiplier, DWORD WriteTotalConstant); //设置超时 void ReciveChar(); //接收字符 bool WriteChar(char* szWriteBuffer, DWORD dwSend); //发送字符 bool OpenPort() { hComm = CreateFile(L"COM6",//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!此处更改com口!!!!!!!!!!!!!!! GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); if (hComm == INVALID_HANDLE_VALUE) return FALSE; else return true; } bool SetupDCB(int rate_arg) { DCB dcb; memset(&dcb, 0, sizeof(dcb)); if (!GetCommState(hComm, &dcb))//获取当前DCB配置 { return FALSE; } dcb.DCBlength = sizeof(dcb); /* ---------- Serial Port Config ------- */ dcb.BaudRate = rate_arg; dcb.Parity = NOPARITY; dcb.fParity = 0; dcb.StopBits = ONESTOPBIT; dcb.ByteSize = 8; dcb.fOutxCtsFlow = 0; dcb.fOutxDsrFlow = 0; dcb.fDtrControl = DTR_CONTROL_DISABLE; dcb.fDsrSensitivity = 0; dcb.fRtsControl = RTS_CONTROL_DISABLE; dcb.fOutX = 0; dcb.fInX = 0; dcb.fErrorChar = 0; dcb.fBinary = 1; dcb.fNull = 0; dcb.fAbortOnError = 0; dcb.wReserved = 0; dcb.XonLim = 2; dcb.XoffLim = 4; dcb.XonChar = 0x13; dcb.XoffChar = 0x19; dcb.EvtChar = 0; if (!SetCommState(hComm, &dcb)) { return false; } else return true; } bool SetupTimeout(DWORD ReadInterval, DWORD ReadTotalMultiplier, DWORD ReadTotalConstant, DWORD WriteTotalMultiplier, DWORD WriteTotalConstant) { COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = ReadInterval; timeouts.ReadTotalTimeoutConstant = ReadTotalConstant; timeouts.ReadTotalTimeoutMultiplier = ReadTotalMultiplier; timeouts.WriteTotalTimeoutConstant = WriteTotalConstant; timeouts.WriteTotalTimeoutMultiplier = WriteTotalMultiplier; if (!SetCommTimeouts(hComm, &timeouts)) { return false; } else return true; } void ReciveChar() { bool bRead = TRUE; bool bResult = TRUE; DWORD dwError = 0; DWORD BytesRead = 0; char RXBuff; for (;;) { bResult = ClearCommError(hComm, &dwError, &Comstat); if (Comstat.cbInQue == 0) continue; if (bRead) { bResult = ReadFile(hComm, //通信设备(此处为串口)句柄,由CreateFile()返回值得到 &RXBuff, //指向接收缓冲区 1, //指明要从串口中读取的字节数 &BytesRead, // &OverLapped); //OVERLAPPED结构 std::cout << RXBuff << std::endl; if (!bResult) { switch (dwError == GetLastError()) { case ERROR_IO_PENDING: bRead = FALSE; break; default: break; } } } else { bRead = TRUE; } } if (!bRead) { bRead = TRUE; bResult = GetOverlappedResult(hComm, &OverLapped, &BytesRead, TRUE); } } bool WriteChar(char* szWriteBuffer, DWORD dwSend) { bool bWrite = TRUE; bool bResult = TRUE; DWORD BytesSent = 0; HANDLE hWriteEvent = NULL; ResetEvent(hWriteEvent); if (bWrite) { OverLapped.Offset = 0; OverLapped.OffsetHigh = 0; bResult = WriteFile(hComm, //通信设备句柄,CreateFile()返回值得到 szWriteBuffer, //指向写入数据缓冲区 dwSend, //设置要写的字节数 &BytesSent, // &OverLapped); //指向异步I/O数据 if (!bResult) { DWORD dwError = GetLastError(); switch (dwError) { case ERROR_IO_PENDING: BytesSent = 0; bWrite = FALSE; break; default: break; } } } if (!bWrite) { bWrite = TRUE; bResult = GetOverlappedResult(hComm, &OverLapped, &BytesSent, TRUE); if (!bResult) { std::cout << "GetOverlappedResults() in WriteFile()" << std::endl; } } if (BytesSent != dwSend) { std::cout << "WARNING: WriteFile() error.. Bytes Sent:" << BytesSent << "; Message Length: " << strlen((char*)szWriteBuffer) << std::endl; } return TRUE; } int main(int argc, char** argv) { if (OpenPort()) std::cout << "Open port success" << std::endl; if (SetupDCB(9600))//!!!!!!!!!!!!!!!!!!!此处更改波特率!!!!!!!!!!!!!!!!!!!!!! std::cout << "Set DCB success" << std::endl; if (SetupTimeout(0, 0, 0, 0, 0)) std::cout << "Set timeout success" << std::endl; PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); while (1) { WriteChar(g_UartRxBuffer, 6);//可以自行设置分部发送 // ReciveChar(); cout <<"正在发送"<< endl; } 分析 主要就是下面5个函数构成 bool OpenPort(); //打开串口 bool SetupDCB(int rate_arg); //设置DCB bool SetupTimeout(DWORD ReadInterval, DWORD ReadTotalMultiplier, DWORD ReadTotalConstant, DWORD WriteTotalMultiplier, DWORD WriteTotalConstant); //设置超时 void ReciveChar(); //接收字符 bool WriteChar(char* szWriteBuffer, DWORD dwSend); //发送字符 一、bool OpenPort(); //打开串口 里面主要调用了一个CreateFile()函数 这个函数的功能是创建或者打开一个文件或者I/O设备,通常使用的I/O形式有文件、文件流、目录、物理磁盘、卷、终端流等。如执行成功,则返回文件句柄。 INVALID_HANDLE_VALUE 表示出错,会设置 GetLastError 。 函数的声明定义: HANDLE WINAPI CreateFile( _In_ LPCTSTR lpFileName, _In_ DWORD dwDesiredAccess, _In_ DWORD dwShareMode, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, _In_ DWORD dwCreationDisposition, _In_ DWORD dwFlagsAndAttributes, _In_opt_ HANDLE hTemplateFile ); 参数列表 该函数第一个参数那里可以更改串口号
上一篇:stm32实时时钟——RTC
下一篇:stm32的堆和栈
推荐阅读最新更新时间:2024-11-04 10:16
- 热门资源推荐
- 热门放大器推荐
设计资源 培训 开发板 精华推荐
- NB6L72MNGEVB,2x2 交叉点交换机评估板
- 使用 Richtek Technology Corporation 的 RT7257G 的参考设计
- TWR-S08PT60-KIT,用于 MC9S08PT60 坚固型 5V、8 位 MCU 的开发塔式系统模块,以突出触摸感应和电机控制
- 使用 Analog Devices 的 AD9249BBCZ 的参考设计
- LT1072HVCT 负降压转换器的典型应用
- GD32-DAPlink
- RD-269,适用于笔记本电脑的 90W、19V 交流转直流单输出电源的参考设计
- LT6604IUFF-2.5 双路极低噪声、差分放大器和 2.5MHz 低通滤波器的典型应用电路
- AM2G-4815SH30Z 15V 2 瓦 DC/DC 转换器的典型应用
- 用于便携式的 1.4 至 18V 模拟放大器