计算机控制stm32单片机,单片机与计算机通信(上位机)——基于VS2017和stm32

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

分析

主要就是下面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

);

参数列表

1be9f43f2266ab446a9857066454ce45.png

该函数第一个参数那里可以更改串口号

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值