Comunication Between Bluetooths through COMs in C# Part I
yuibox April 1st, 2008
I was trying to send information, strings, files and the most important, custom packets between 2 bluetooth. One listening and the other one sending(Master <-> Slave). In order to test this in an easy way, both bluetooh are connected to a Serial Port, in this case, COM1 and COM2. In order to test this in 1 single computer and avoid the hassle, I Connected a Null Modem Adapter or RS-232 Serial Cable. I wired one COM port into another, so both will be connected together, transmitting and receiving in the same line cross linked (TX, similar to RX).
This tutorial will show the first step to start a connection and to get the general idea of how this puppy works
In Part I of this tutorial I will explain how to send a simple string and receive it
easy ne? ^.^ we all love baby steps
Requirements : - Null Modem Adapter
- VS.NET 2005 (or any other C# compiler)
- Basic Knowledge of C#
- and lots of time and patient
First of all, Here is how my serial ports currently work:
COM1 -> Is my Master,in order words, this COM will send information to the listener.
COM2-> Is my Slave. This COM will receive information form the sender.
Create 1 Window App project and a Console Project: 1.- C# Project COM! and 2.- C# Project COM2
1.- C# Project COM1: Master
Opens Serial Port COM1, sends string and closes port
[code lang="c++"]
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
public partial class Form1 : Form
{
SerialPort m_port; //class member, so it can be access by any other class function ^.^
public Form1()
{
InitializeComponent();
//SerialPort(string portname, int baudrate, Parity parity, int dataBits, StopBits stopbits)
m_port = new SerialPort(”COM1″, 9600, Parity.None, 8, StopBits.One);
m_port.Open();//Opens the port
}
private void btnPrint_Click(object sender, EventArgs e)
{
m_port.Write(”Hello COM2 from COM1 !”); // Writes to port
}
private void Form1_Closed(object sender, EventArgs e)
{
m_port.Close();//and closes port after its done
}
}
[/code]
2.- C# Project COM2: Slave
Opens COM2 and reads the information that is in the buffer, in other words, receive whatever COM1 is sending
[code lang="c++"]
class SerialPortProgram
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort(”COM2″,
9600, Parity.None, 8, StopBits.One);
[STAThread]
static void Main(string[] args)
{
// Instatiate this class
new SerialPortProgram();
}
private SerialPortProgram()
{
Console.WriteLine(”Incoming Data:”);
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);//When received data will jump tp the port_DataReceived function port.Open();//opens the port
Application.Run();
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Console.WriteLine(port.ReadExisting());//Prints in console the information that is in the buffer, in other words, the information that he is receiving
}
}
[/code]
Done!
Run the COM2 Project then COM1 and send the information along.
Simple eh? ^.^
Stumble it!
Recent Comments