C# console application talking to Arduino via Bluetooth -
not whole lot here other doesn't work, , have no idea why.
the serial output on arduino nothing. output on c# code goes down waiting response , nothing.
the bluetooth card led on arduino goes green when start c# program there connection being made. nothing else.
arduino code
#include <softwareserial.h> //software serial port #define rxd 8 // pin bluetooth (bt_tx) transmit arduino (rxd) #define txd 7 // pin bluetooth (bt_rx) receive arduino (txd) softwareserial bluetoothserial(rxd,txd); boolean light = false; void setup(){ serial.begin(9600); // allow serial communication via usb cable computer (if required) pinmode(rxd, input); // setup arduino receive input bluetooth shield on digital pin 6 pinmode(txd, output); // setup arduino send data (output) bluetooth shield on digital pin 7 pinmode(13,output); // use onboard led if required. } void loop(){ delay(1000); if(light){ light = false; digitalwrite(13,low); } else{ light = true; digitalwrite(13,high); } //serial.println(bluetoothserial.available()); bluetoothserial.write("im alive"); if(bluetoothserial.read()>0){ serial.write(bluetoothserial.read()); } }
core c# code
static void main(string[] args) { byte[] command = encoding.ascii.getbytes("it works");//{0x00,0x01,0x88}; serialport bluetoothconnection = new serialport(); bluetoothconnection.baudrate = (9600); bluetoothconnection.portname = "com4"; bluetoothconnection.open(); if (bluetoothconnection.isopen) { bluetoothconnection.errorreceived += new serialerrorreceivedeventhandler(bluetoothconnection_errorreceived); // declare 2 bytes vector store message length header byte[] messagelength = { 0x00, 0x00 }; //set lsb length of message messagelength[0] = (byte)command.length; //send 2 bytes header bluetoothconnection.write(messagelength, 0, messagelength.length); // send message system.threading.thread.sleep(2000); bluetoothconnection.write(command, 0, command.length); messages(5, ""); //this last thing prints before waits response. int length = bluetoothconnection.readbyte(); messages(6, ""); // retrieve reply data (int = 0; < length; i++) { messages(7, (bluetoothconnection.readbyte().tostring())); } } }
okay there few issues hard see above attempt explain.
first, rx , tx on bluetooth card not go equals on arduino. go opposites.
example:
bluetooth card rx -> tx on arduino
bluetooth card tx -> rx on arduino
so if in photo of arduino above should painfully obvious pins 7 , 8 incorrect placement.
the next issue having poor connectivity.
the power , ground seem fine in above configuration. however, rx , tx connections need soldered in. otherwise poor connection.
once made changes above code worked flawlessly.
hope helps else having these issues!!
Comments
Post a Comment