When I send 5
through a serial terminal, recv()
outputs the sent data, and then corrupted garbage (5╠╠╠╠╠╠╠╠☺0
). This is my code:
#include <winsock2.h>
#include <ws2bth.h>
#include <Windows.h>
#include <iostream>
#include <string.h>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
int i;
unsigned int aaddr[6];
void send2(string in) {
WSADATA wsa;
memset(&wsa, 0, sizeof(wsa));
int error = WSAStartup(MAKEWORD(2, 2), &wsa);
SOCKET btSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
SOCKADDR_BTH sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.addressFamily = AF_BTH;
sockAddr.serviceClassId = RFCOMM_PROTOCOL_UUID;
sockAddr.port = BT_PORT_ANY;
BTH_ADDR tmpaddr = 0;
sscanf_s("7C:9E:BD:4C:BF:B2", "x:x:x:x:x:x", &aaddr[0], &aaddr[1], &aaddr[2], &aaddr[3], &aaddr[4], &aaddr[5]);
*&sockAddr.btAddr = 0;
for (i = 0; i < 6; i ) {
tmpaddr = (BTH_ADDR)(aaddr[i] & 0xff);
*&sockAddr.btAddr = ((*&sockAddr.btAddr) << 8) tmpaddr;
}
connect(btSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr));
char charIn[28];
strcpy_s(charIn, in.c_str());
send(btSocket, charIn, (int)strlen(charIn), 0);
closesocket(btSocket);
}
void recv2() {
WSADATA wsa;
memset(&wsa, 0, sizeof(wsa));
int error = WSAStartup(MAKEWORD(2, 2), &wsa);
SOCKET btSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
SOCKADDR_BTH sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.addressFamily = AF_BTH;
sockAddr.serviceClassId = RFCOMM_PROTOCOL_UUID;
sockAddr.port = BT_PORT_ANY;
BTH_ADDR tmpaddr = 0;
sscanf_s("7C:9E:BD:4C:BF:B2", "x:x:x:x:x:x", &aaddr[0], &aaddr[1], &aaddr[2], &aaddr[3], &aaddr[4], &aaddr[5]);
*&sockAddr.btAddr = 0;
for (i = 0; i < 6; i ) {
tmpaddr = (BTH_ADDR)(aaddr[i] & 0xff);
*&sockAddr.btAddr = ((*&sockAddr.btAddr) << 8) tmpaddr;
}
connect(btSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr));
const int outLen = 1;
char charOut[outLen];
recv(btSocket, charOut, outLen, 0);
cout << charOut;
closesocket(btSocket);
cout << WSAGetLastError();
}
int main() {
recv2();
}
CodePudding user response:
You should NOT reinitialize Winsock, or recreate the Bluetooth socket, on every send and read. Initialize Winsock one time, preferably at app startup. And then create 1 socket and reuse it as needed.
Also, you don't need the charIn[]
buffer in send2()
at all, as you can pass in
to send()
:
send(btSocket, in.c_str(), (int)in.size(), 0);
In any case, your garbage issue is because you are not sending a null terminator after the sent data, and you are not null-terminating the buffer you are reading into, but you are displaying the buffer as if it were null-terminated. You need to pay attention to the return value of recv()
and display only as many bytes as you actually receive, eg:
const int outLen = 1;
char charOut[outLen 1];
int numBytes = recv(btSocket, charOut, outLen, 0);
if (numBytes > 0) {
charOut[numBytes] = '\0';
cout << charOut;
}
Or:
const int outLen = 1;
char charOut[outLen];
int numBytes = recv(btSocket, charOut, outLen, 0);
if (numBytes > 0) {
cout.write(charOut, numBytes);
}