Bluetooth модуль HC-05 выключается

столкнулся с проблемой - когда подключаю к ардуино модуль HC-05, то он работает секунд 30, а потом просто отключается или код на компьютере для unity(C#) не принимает передаваемые данные. Может что-то необходимо добавить в код ардуино и что именно?

Код ардуино:


    SoftwareSerial BTSerial(10, 11); // RX, TX
    
    void sendBT(const byte *data, int l)
    {
        byte len[4];
        len[0] = 85; //preamble
        len[1] = 85; //preamble
        len[2] = (l >> 8) & 0x000000FF;
        len[3] = (l & 0x000000FF);
        BTSerial.write(len, 4);
        BTSerial.flush();
        BTSerial.write(data, l);
        BTSerial.flush();
    }
    
    void setup()
    {
        BTSerial.begin(9600);
    }
    
    char *data;
    int data_length;
    int i = 0;
    
    void loop()
    {
      sendBT("HELLOO", 6);
    }

Код C#:


    public class BTConnect : MonoBehaviour
    {
        private BluetoothHelper BTHelper;
        private string x;
    
        void Start()
        {
            try
            {
                x = "";
                BTHelper = BluetoothHelper.GetInstance("HC-05");
                BTHelper.setLengthBasedStream();
                BTHelper.OnConnected += OnBluetoothConnected;
            }
            catch (BluetoothHelper.BlueToothNotEnabledException ex) { }
            catch (BluetoothHelper.BlueToothNotReadyException ex) { }
            catch (BluetoothHelper.BlueToothNotSupportedException ex) { }
            catch (BluetoothHelper.BlueToothPermissionNotGrantedException ex) { }
        }
    
        private void Update()
        {
            byte[] msg = BTHelper.ReadBytes();
            if (msg != null)
            {
                string content = System.Text.Encoding.ASCII.GetString(msg);
                print(content);
            }
        }
    
        void OnBluetoothConnected()
        {
            try
            {
                BTHelper.StartListening();
            }
    
            catch (Exception ex)
            {
                x += ex.ToString();
                Debug.Log(ex.Message);
            }
        }
    
        void OnGUI()
        {
            BTHelper.DrawGUI();
    
            if (!BTHelper.isConnected())
                if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Connect"))
                {
                    if (BTHelper.isDevicePaired())
                        BTHelper.Connect(); // tries to connect
                    print("No Connection");
            }
    
            if (BTHelper.isConnected())
                if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height - 2 * Screen.height / 10, Screen.width / 5, Screen.height / 10), "Disconnect"))
                {
                    BTHelper.Disconnect();
                }
        }

Заранее спасибо.


Ответы (1 шт):

Автор решения: Vanyamba Electronics

Попробуйте

void loop()
{
  sendBT("HELLOO", 7);
}

чтобы '\0' в конце строки тоже передать. В клиент на C# его ожидает.

→ Ссылка