C ++, Bluetooth serial read and write, and further in communication with microcontroller

In doing the project need to implement such a feature, not found a good answer on the Internet, suddenly one day accidentally achieved. Here the record about, it can easily achieve.

Remarks:

1, MCU module: HC-05 Bluetooth module;
2, Bluetooth communication actually use a serial port, so here to tell us about reading and writing to the serial port:
3, mainly used API: CreateFile (), WriteFile ( ), ReadFile () .

Code shows:

(Defined herein provides functions to read and write to the serial port, can be directly transplanted)

Head function definition

# ifndef __uart_
#define __uart_

#include<windows.h>

#define RtlZeroMemory(Destination,Length) memset((Destination),0,(Length))
#define ZeroMemory RtlZeroMemory

BOOL Write(char *write_data, DWORD contentLen, DCB &dcb);
BOOL Read(char *read_data, DCB &dcb);
void DCB_init(DCB &dcb);

#endif // !__uart_

.c file definition

#include "uart.h"
#include <iostream>

void DCB_init(DCB &dcb)
{
 dcb.BaudRate = 9600;
 dcb.ByteSize = 8;
 dcb.Parity = 0;
 dcb.StopBits = 1;
}

BOOL Write(char *write_data, DWORD contentLen,DCB &dcb)
{
 HANDLE hcom;
 char *tmpBuf;
 DWORD dwBytesWrite = 0;
 int dwBytesToWrite;

 hcom = CreateFile(L"COM6", GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if (hcom == INVALID_HANDLE_VALUE)
 {
  printf("create file error!\n");
  CloseHandle(hcom);
  return FALSE;
 }

 SetupComm(hcom, 1024, 1024);
 GetCommState(hcom, &dcb);
 SetCommState(hcom, &dcb);

 dwBytesToWrite = contentLen;
 dwBytesWrite = 0;

 tmpBuf = write_data;
 do {                                       //循环写文件,确保完整的文件被写入  
  WriteFile(hcom, tmpBuf, dwBytesToWrite, &dwBytesWrite, NULL);
  dwBytesToWrite -= dwBytesWrite;
  tmpBuf += dwBytesWrite;
 } while (dwBytesToWrite > 0);

 if (hcom != INVALID_HANDLE_VALUE)
 {
  CloseHandle(hcom);
  hcom = INVALID_HANDLE_VALUE;
 }

 return TRUE;
}

BOOL Read(char *read_data,DCB &dcb)
{
 HANDLE hcom;
 DWORD fileSize;
 char *buffer;
 DWORD dwBytesRead, dwBytesToRead, tmpLen;

 hcom = CreateFile(L"COM5", GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

 if (hcom == INVALID_HANDLE_VALUE)
 {
  printf("open file error!\n");
  CloseHandle(hcom);
  return FALSE;
 }

 fileSize = GetFileSize(hcom, NULL);          //得到文件的大小
 buffer = (char *)malloc(fileSize);
 ZeroMemory(buffer, fileSize);
 dwBytesToRead = fileSize;
 dwBytesRead = 0;
 read_data = buffer;

 do {                                       //循环读文件,确保读出完整的文件    
  ReadFile(hcom, read_data, dwBytesToRead, &dwBytesRead, NULL);
  if (dwBytesRead == 0)
   break;
  dwBytesToRead -= dwBytesRead;
  read_data += dwBytesRead;
 } while (dwBytesToRead > 0);

 //  TODO 处理读到的数据 buffer
 free(buffer);
 CloseHandle(hcom);

 return TRUE;
}

Computer Bluetooth serial port settings

1. Turn on the computer Bluetooth settings screen, check the Bluetooth option more
Here Insert Picture Description

2, check the following options arrow pointing
Here Insert Picture Description
3, click Add
Here Insert Picture Description
4, set up a virtual serial port incoming and outgoing
Here Insert Picture Description

Finally, remember to match the serial port settings to be specified in the above code CreateFile!

Readers want to be helpful, like it can look at my public, then I will study notes made in the above, we will be able to learn together!

Here Insert Picture Description

Alt

Published 19 original articles · won praise 12 · views 6102

Guess you like

Origin blog.csdn.net/Rosen_er/article/details/104548145