Sending data from C# to Teensy 3.2 over Bluetooth HC-05 module?

Status
Not open for further replies.
I am attempting to send data from a C# GUI to the Teensy 3.2 via the OUTGOING Bluetooth port on my PC but am having no luck. The data transfer is working in the opposite direction (sending data from Arduino to C# GUI). I am using the INCOMING Bluetooth port on Putty to try to see the "test string" but am getting no results. I am, however, getting a constant string of -1s????

Teensy code:
Code:
#define HWSERIAL Serial1


void setup() {

  HWSERIAL.begin(9600);
  
}

void loop() {

  String command = HWSERIAL.read();
  HWSERIAL.print(command);
  HWSERIAL.println();

}


C# code (I am basically just sending a test string on repeat):
Code:
 private void button11_Click(object sender, EventArgs e)
        {
            portBTSend = new SerialPort("COM24", 9600, Parity.None, 8, StopBits.One);
            portBTSend.RtsEnable = true;
            portBTSend.DtrEnable = true;

            try
            {
                portBTSend.Open();
            }
            catch (Exception e1)
            {
                label8.Text = "Connection to BT Failed. Try Again";
            }


            while () {
                portBTSend.Write("TEST STRING");
                System.Threading.Thread.Sleep(1000);
            }

         }
 
  • Does the code work if you connect the Teensy directly without the bluetooth?
  • Looks like you never close the port and try to reopen whenever you hit the button? The usual way to do it is to store the SerialPort variable somewhere and open/close on demand. Normally you'd open on program start and close when you leave the app. Never forget to close or dispose the port before you quit the application otherwise it will lock the port.
  • The teensy code looks weird. I have no experience with the strange arduino strings, are you sure you can assign a int to a string? Did you mean readString() instead?
 
  • Does the code work if you connect the Teensy directly without the bluetooth?
  • Looks like you never close the port and try to reopen whenever you hit the button? The usual way to do it is to store the SerialPort variable somewhere and open/close on demand. Normally you'd open on program start and close when you leave the app. Never forget to close or dispose the port before you quit the application otherwise it will lock the port.
  • The teensy code looks weird. I have no experience with the strange arduino strings, are you sure you can assign a int to a string? Did you mean readString() instead?

@luni

1. Yes! The board works over USB serial and it works for sending data from multiple sensors to the PC via BT serial. What is not working, is sending data from the PC to the Teensy over BT serial.
2. I have edited the code to look similar to what you are describing, this was just a first attempt at sending out something quickly.
3. When I use readString(), the serial monitor is blank. When i use read() I get either a -1 or a little box.
 
Tried this c# to send Data to the teensy (tested and works fine. I linked the PC with the Teensy via a cheap usb serial converter)

(c# console application)
Code:
using System;
using System.IO.Ports;

namespace SerialTester
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Text, send with RETURN key, Empty line quits");
            using (var port = new SerialPort("COM13", 115200))
            {
                port.Open();

                var line = Console.ReadLine();
                while (line != "")
                {
                    port.Write(line + '\n');
                    line = Console.ReadLine();
                }
            }
        }
    }
}

And here the receiving code on the Teensy

Code:
HardwareSerial hwSer = Serial1; 

void setup()
{
    hwSer.begin(115200);    
}

void loop()
{
    while(hwSer.available())
    {
       char c = hwSer.read();
       Serial.write(c); // echo input on Serial to inspect on SerMon
    }
}

This code will listen on Serial1 (pin1) and echo every line you type on the USB Serial port of the Teensy. So you should be able to see it on SerMon. Hope that helps to track the error down.
 
@luni

Do you have a Windows Form Version of this?

My Visual Studio is not working Console App, I can't build or run it (see picture).
VS.PNG
 
Sure, don't like this ancient thing but here you are

Code:
using System.IO.Ports;
using System.Windows.Forms;

namespace WinFormTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (var port = new SerialPort("COM13", 115200))
            {
                port.Open();
                port.WriteLine("Hello Teensy");
            }
        }
    }
}

Echos "Hello Teensy" on my SerMon when you press the button.
 
Status
Not open for further replies.
Back
Top