Writing Data over Bluetooth connection?

colinodowd

Member
Joined
Jan 23, 2020
Messages
12
Programming Experience
1-3
I am attempting to send data from a C# GUI to the Teensy 3.2 via the 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 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:
C#:
Expand Collapse Copy
#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):
C#:
Expand Collapse Copy
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 (true) {
                portBTSend.Write("TEST STRING");
                System.Threading.Thread.Sleep(1000);
            }

         }

My Tx from HC-05 to Rx on Teensy and Rx from HC-05 to Tx on Teensy. I believe all of my wiring is correct because I am receiving data from all my Teensy sensors to my C# GUI through a Bluetooth COM port. However, when I try to send something to the Teensy and read it through Serial1.Read() I just get -1s.

I believe I am missing something on the outgoing connection from my PC to Teensy
.
BT.PNG
 
The screenshot above suggests that COM24 is an incoming port, but you are trying to write out to it?
 
Just because its working correct, doesn't mean its right....

If a port is for reading, then read from it, vice-versa for writing.
 
Using a Win 10 BT terminal from the app store worked and allowed me to send data from PC to Teensy so I believe there is something wrong with my C# code. My code freezes on the BTSend.Write line everytime I run it. This is what I have:

Teensy:
C#:
Expand Collapse Copy
#define HWSERIAL Serial1


void setup() {

  HWSERIAL.begin(9600);

}

void loop() {

  int incomingByte = 0;
  incomingByte = HWSERIAL.read();
  Serial.println(incomingByte);        
  delay(1000);

}

C#:
C#:
Expand Collapse Copy
SerialPort BTSend = new SerialPort("COM23", 9600, Parity.None, 8, StopBits.One);
BTSend.Open()
BTSend.Write("Hi");
BTSend.Close();
 
If the other app works, then time to cross check settings. Which COM port? Is the comm port settings really set to 9600,N,8,1 on the working app?
 
Also why the change :
C#:
Expand Collapse Copy
new SerialPort("COM23", 9600, Parity.None, 8, StopBits.One);
From :
C#:
Expand Collapse Copy
new SerialPort("COM24", 9600, Parity.None, 8, StopBits.One);
 
Back
Top Bottom