Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot send data from pc to raspberry pi #23

Open
reddevil1310 opened this issue Sep 11, 2023 · 6 comments
Open

cannot send data from pc to raspberry pi #23

reddevil1310 opened this issue Sep 11, 2023 · 6 comments
Assignees

Comments

@reddevil1310
Copy link

reddevil1310 commented Sep 11, 2023

i am connect pc to raspberry pi to send a message. but it can't read any character?

this is program from pi:

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <exception>
#include <vector>
#include <memory>
#include <ctime>
#include "DeviceINQ.h"
#include "Enums.h"
#include "BluetoothException.h"
#include "BTSerialPortBinding.h"
#include <QThread>

using namespace std;

string formatDate(const char *format, time_t time)
{
    if (time <= 0)
        return "--";

    char buffer[256] = { 0 };
    tm *timeinfo = localtime(&time);

    if (timeinfo)
        strftime(buffer, sizeof(buffer), format, timeinfo);

    return buffer;
}

int main()
{
    try
    {
        unique_ptr<DeviceINQ> inq(DeviceINQ::Create());
        vector<device> devices = inq->Inquire();

        for (const auto& d : devices)
        {
            cout << "\tname: " << d.name << endl;
            cout << "\taddress: " << d.address << endl;
            cout << "\tclass: " << GetDeviceClassString(d.deviceClass) << endl;
            cout << "\tmajor class: " << GetDeviceClassString(d.majorDeviceClass) << endl;
            cout << "\tservice class: " << GetServiceClassString(d.serviceClass) << endl;
            cout << "\tlast seen: " << formatDate("%c", d.lastSeen) << endl;
            cout << "\tlast used: " << formatDate("%c", d.lastUsed) << endl;
            cout << "\tchannel ID: " << inq->SdpSearch(d.address) << endl;
            cout << endl;
            if(d.name == "DESKTOP-B1U1RIH") {

                BTSerialPortBinding *bt = BTSerialPortBinding::Create(d.address, 1);
                bt->Connect();
                char readC[100];
                while(true) {

                    if(bt->IsDataAvailable()) {
                        std::cout<<"start read";
                        bt->Read(readC, 100);
                        std::cout<<"read: "<<readC<<std::endl;
                    }
                    else {
                        std::cout<<"IsDataAvailable not available"<<std::endl;;
                    }
                    QThread::sleep(1);
                }
            }
        }

        cout << endl << "done, found " << devices.size() << " device(s)" << endl;
    }
    catch (const BluetoothException& e)
    {
        cout << e.what() << endl;
    }

    return 0;
}

This is program in pc:

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <exception>
#include <vector>
#include <memory>
#include <ctime>
#include "DeviceINQ.h"
#include "Enums.h"
#include "BluetoothException.h"
#include "BTSerialPortBinding.h"
#include <QThread>

using namespace std;

string formatDate(const char *format, time_t time)
{
    if (time <= 0)
        return "--";

    char buffer[256] = { 0 };
    tm *timeinfo = localtime(&time);

    if (timeinfo)
        strftime(buffer, sizeof(buffer), format, timeinfo);

    return buffer;
}

int main()
{
    try
    {
        unique_ptr<DeviceINQ> inq(DeviceINQ::Create());
        vector<device> devices = inq->Inquire();

        for (const auto& d : devices)
        {
            cout << "\tname: " << d.name << endl;
            cout << "\taddress: " << d.address << endl;
            cout << "\tclass: " << GetDeviceClassString(d.deviceClass) << endl;
            cout << "\tmajor class: " << GetDeviceClassString(d.majorDeviceClass) << endl;
            cout << "\tservice class: " << GetServiceClassString(d.serviceClass) << endl;
            cout << "\tlast seen: " << formatDate("%c", d.lastSeen) << endl;
            cout << "\tlast used: " << formatDate("%c", d.lastUsed) << endl;
            cout << "\tchannel ID: " << inq->SdpSearch(d.address) << endl;
            cout << endl;

            if(d.name == "raspberrypi") {
                BTSerialPortBinding *bt = BTSerialPortBinding::Create(d.address, 5);
                bt->Connect();
                while(true) {
                        std::string data = "test send serialport";
                        bt->Write(data.c_str(), data.length());
                        QThread::sleep(1);
                }
            }
        }

        cout << endl << "done, found " << devices.size() << " device(s)" << endl;



    }
    catch (const BluetoothException& e)
    {
        cout << e.what() << endl;
    }

    return 0;
}

Where did I go wrong? Anyone can help?
Thank you.

@Agamnentzar
Copy link
Owner

Agamnentzar commented Sep 11, 2023

BTSerialPortBinding *bt = BTSerialPortBinding::Create(d.address, 5);

Did you try it with 1 instead of 5 ?

You can also try without if(bt->IsDataAvailable()) { and see if you get something in that case.

@Agamnentzar Agamnentzar self-assigned this Sep 11, 2023
@reddevil1310
Copy link
Author

BTSerialPortBinding *bt = BTSerialPortBinding::Create(d.address, 5);

Did you try it with 1 instead of 5 ?

You can also try without if(bt->IsDataAvailable()) { and see if you get something in that case.

@Agamnentzar I try with 1 but it's not working. I also try without if(bt->IsDataAvailable()), it's still failed.

@Agamnentzar
Copy link
Owner

Oh ok I see it now, you're connecting as a client on both sides, I don't think that will work. I think one side has to be a server that's listening for a connection. This library was created to connect to devices that already host COM port service so I'm not sure how to setup that up. I think you might need to write some custom code for that.

@reddevil1310
Copy link
Author

@Agamnentzar How to create a server for connection? Thank you

@Agamnentzar
Copy link
Owner

I don't know, I never had to do that.

@reddevil1310
Copy link
Author

Thank you @Agamnentzar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants