Determining "Standard Serial over Bluetooth Link" Outgoing COM port and reading from it

109 Views Asked by At

I have a HC-05 bluetooth module that is connected to my Arduino which is sending data to the COM port on my laptop via bluetooth.

I am looking to create a script using C to build into an executable file.

With the script, I am trying to first check which COM ports are "Standard Serial over Bluetooth Link" and then see which one of those are the "Outgoing" COM port. Then, I am aiming to open this port and read the data from it.

Can anyone please provide some guidance as to how to go about this? Is this possible using C code? I have seen some C# code posted online linked to this.

Regarding the OS, I am looking to use this in Windows.

I have had some success using WINDOWS API to open and read ports. But the issue I'm facing is that I don't know a way to determine which of the COM ports are Standard Serial over Bluetooth Link and Outgoing.

1

There are 1 best solutions below

0
petzval On

I have found it easier to use sockets rather than COM ports. This way, it is not necessary to pair the device - in fact it must not be paired, so unpair it first. This works for me:

#include <ws2bth.h>    
#include <winsock2.h>  

  struct WSAData wsadata;
  int errnum,retval;
  unsigned char *dp;
  SOCKADDR_BTH  remoteadd;  
  SOCKET LocalSocket;
  GUID *uuid;
  u_long arg;
  fd_set wrset;
  fd_set exset;
  struct timeval tv;
  
  
    // Start WSA once only - not on each connection
  WSAStartup((WORD)(2 + (2 << 8)), &wsadata);
  
    // connect to listening classic server
 
  dp = (char*)&remoteadd.btAddr; // length=8
                            
  // connect to Bluetooth board address 11:22:33:44:55:66
  // set dp[] in reverse order
  
  dp[0] = 0x66;
  dp[1] = 0x55;
  dp[2] = 0x44;
  dp[3] = 0x33;
  dp[4] = 0x22;
  dp[5] = 0x11;
  dp[6] = 0;
  dp[7] = 0;
        
  remoteadd.addressFamily = AF_BTH;
  
  uuid = &remoteadd.serviceClassId;

  // HC-05 listens on channel 1 so there
  // is no need to go via a UUID
  
  remoteadd.port = 1;   // RFCOMM channel 1  
  uuid->Data1 = 0;  
  uuid->Data2 = 0;
  uuid->Data3 = 0;
  uuid->Data4[0] = 0;
  uuid->Data4[1] = 0;
  uuid->Data4[2] = 0;
  uuid->Data4[3] = 0;
  uuid->Data4[4] = 0;
  uuid->Data4[5] = 0;
  uuid->Data4[6] = 0;
  uuid->Data4[7] = 0;
 
  LocalSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
  arg = 1;
  ioctlsocket(LocalSocket,FIONBIO,&arg);  // set non-blocking
  
  if(SOCKET_ERROR == connect(LocalSocket,
          (struct sockaddr *)&remoteadd,sizeof(SOCKADDR_BTH)) )   
    {
    errnum = WSAGetLastError();
    if(errnum == WSAEWOULDBLOCK)
      {  // connect() non blocking so returns immediately
         // and it may be necessary to wait for connect to complete
         // Wait with 30s time out until select() sees connection        
      FD_ZERO(&wrset);
      FD_ZERO(&exset);

      FD_SET(LocalSocket, &wrset);
      FD_SET(LocalSocket, &exset);

      //wait 30 sec for connect to finish
      tv.tv_sec = 30;
      tv.tv_usec = 0;
      retval = select(0, 0, &wrset,0,&tv);
      if(retval > 0 && retval < 8)
        errnum = 0;   // OK 
      }
    if(errnum != 0)
      {
      // Connect fail
      closesocket(LocalSocket);
      return;
      }
    }
 
   // LocalSocket is now an open serial channel to the server

   // read/write serial data
   
  recv(LocalSocket,buf,count,0);   
  send(LocalSocket,buf,count,0);

   // close connection
   
  closesocket(LocalSocket);