Bluetooth comms

rigidigital

Senior Member
I have been using uhf chips to send and recive data, using keypads etc. I enjoy the new serial port control in vb.net as it listens all the time for anything coming from the pickaxe and is dead simple to use, also usb to serial port adapters work fine too if you have no serial port on your laptop.

Does anyone know where to go for blue tooth devices instead of using uhf ? Also do you know what the max range of bluetooth is , i imaging it is very short. I know that bluetooth is used a lot in controlling 'robots' from a pc' or so i just read on a ms robotics site.

thanks, mick.
 

Texy

Senior Member
Google will probably tell you, but I believe that there are different classes, the 'common' one being a 10 metre range. Incidently, do you have any vb.net examples, for serial comms?

Texy
 

rigidigital

Senior Member
texy Serial port code

Texy, i dont have my own code i used with me. its back home on my desktop. , but ill post something in a little while that should be enough. just a bit of code i have on my laptop.
I was afraid that blyutooth was around ten meters, that is what i thought it was. I have found some good uhf cheap chips that have a big range so ill stick with them i think.thanks for yr response.
 

rigidigital

Senior Member
serial port comms

texy i just got this off codeproject you can prob find better examples on msdn vb my own code was not so comprehensive but it worked fine!This code below was written to comunicate between two computers using a cable but 'talking to the picaxe' seemed to be a lot easier.

Introduction
A few days ago I said to myself that I wanted to know more about how to communicate via the serial port. When I first started searching the internet about this subject I found out that it�s not many articles that are discussing this subject and those examples that I found was mostly about the earlier VB6 MSComm control and wrappers for this control. Those matters concerning the MSComm control were not very interesting because I had read that in .NET 2.0 Microsoft had come up with a new serial port control.

As a newbie I have been spending some hours of my time to come up with what I now share with you, but as you all know it�s worth every hour when you succeed. It�s actually not a big deal to do it; it�s just a few lines of code.

Fore those of you who are familiar with the serial port communication I want to recommend this article on the Code Project. It is an excellent article about communicating with mobile phones via the serial port, and it is very clear when you fist know the basic.

The task of this example is very simple. We want to send a text string from one computer via the serial port to another computer. First of all you have to bee in the position that you have 2 computers and second you got to have a �null modem cable�. Another option is that you have 2 serial ports on the computer and connecting them with a �null modem cable�

If you don�t know what a �null modem cable� is then search the internet to see how it is configured.

First of all we want to write to the serial port, and here is the basic.

If you have 2 computers have this one on the first computer. If you have one computer make this a separate project.

In this example you got to have a Button control called btnSendText and a textBox control called txtSendText on your form on computer nr1. Just type in some text in the btnSendText control and Click Button send to send it to COM1.

Collapse Copy Code
Imports System
Imports System.IO.Ports

Public Class Form1
Dim WithEvents Port As SerialPort = _
New SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)
Private Sub btnSendText_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSendText.Click
Port.Open()
Port.Write(txtSendText.Text & "!%")
Port.Close()
End Sub
End ClassThis is just how simple it is to send a text string to the serial port.

And now how to receive the text string on the other computer.

If you have 2 computers have the next example one on the second computer. If you have one computer make this a separate project, and then run both projects at the same time.

In this example you got to have a Text control called TextBox1 and a listbox control called ListBox1 on your form on computer nr2. When clicking send on computer nr1 you will receive it in the textbox1 control on computer nr 2. When the buffer is 0 it will be added to the Listbox1 control, and ListBox1 is empty to receive the next incoming text string.

Collapse Copy Code
Imports System
Imports System.IO.Ports

Public Class Form1
Dim WithEvents port As SerialPort = New _
System.IO.Ports.SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)

Private Sub Form1_Load(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles Me.Load
CheckForIllegalCrossThreadCalls = False
If port.IsOpen = False Then port.Open()
End Sub

Private Sub port_DataReceived(ByVal sender As Object, ByVal e As _
System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived
TextBox1.Text = (port.ReadTo("!%"))
If port.ReadExisting.Length = 0 Then
ListBox1.Items.Add(TextBox1.Text)
TextBox1.Text = ""
End If
End Sub
End ClassThe important thing to notice is that you have to declare the port like this �Dim WithEvents port ...�

You also got to have a �CheckForIllegalCrossThreadCalls = False� declaration in the in the form load procedure to prevent it from raising an error when a thread other than the creating thread of a control tries to access one of that control's methods or properties. You also have to check if the port is open, and if it�s not open you have to open it.

As you may see that I have some special characters in both the write and the read statement.

Collapse Copy Code
port.Write(txtSendText.Text & "!%") and port.ReadTo("!%").This is because if I put some special characters in the write statement stream I can ask the readTo statement to read everything until the special character and that is quiet convenient. Just test it.

There are many other options to the serial port communication, and this is only one of them.

I hope it can be of any help to somebody.


License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author
 

rigidigital

Senior Member
more code for serial port

also trhttp://www.devx.com/dotnet/Article/31001
http://www.dreamincode.net/forums/showtopic37361.htmhttp://support.microsoft.com/kb/904795/ this link is the best



these links or just search USING THE SERIAL PORT VB.NET AND YOULL GET HEAPS OF HITS




Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.IO.Ports
_

Class Form1
Inherits Form


Public Sub New()
InitializeComponent()
End Sub 'New

Private stopBits, parity As Array ' arrays to access the enumerations in System.IO.Ports
Private validStopBits As New ArrayList() ' ArrayLists hold the valid values for this machine
Private validParity As New ArrayList()


' Send the text in the text box to the serial port.
' The data should stay in the buffer until received by the event handler.
Private Sub SendButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Try
' Write a line to the serial port
SerialPort1.WriteLine(textBox1.Text)
Catch ex As System.Exception
MessageBox.Show(ex.Message)
End Try
End Sub 'SendButton_Click


' Event handler for data reception
Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
' Read the buffer to the text box.
textBox2.Text = serialPort1.ReadLine()
End Sub 'serialPort_DataReceived


' Load the form and set up parameters from the default serial port.
' Open the port and prepare it for IO.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Try
' Open the serial port
SerialPort1.Open()
' Set the event handler for data reception
AddHandler SerialPort1.DataReceived, AddressOf serialPort_DataReceived
Catch ex As System.Exception
MessageBox.Show(ex.Message)
End Try
label11.Text = SerialPort1.PortName

label2.Text = SerialPort1.BaudRate.ToString()
label4.Text = SerialPort1.StopBits.ToString()
label6.Text = SerialPort1.Parity.ToString()
label8.Text = SerialPort1.DataBits.ToString()
label10.Text = SerialPort1.RtsEnable.ToString()

' Populate the stop bits box with all valid options.
' Note that the serial port must be open to accurately test the options.
comboBox1.SelectedText = SerialPort1.StopBits.ToString()
Dim currentStopBitSetting As System.IO.Ports.StopBits = SerialPort1.StopBits

stopBits = [Enum].GetValues(GetType(System.IO.Ports.StopBits))
Dim sbtype As System.IO.Ports.StopBits
For Each sbtype In stopBits
' test to make sure the machine supports the setting
Try
SerialPort1.StopBits = sbtype
validStopBits.Add(sbtype)
comboBox1.Items.Add(sbtype)
Catch ' ignore this entry as invalid
End Try
Next sbtype
SerialPort1.StopBits = currentStopBitSetting

' Populate the parity with valid options
comboBox2.SelectedText = SerialPort1.Parity.ToString()
Dim currentParitySetting As System.IO.Ports.Parity = SerialPort1.Parity
parity = [Enum].GetValues(GetType(System.IO.Ports.Parity))
Dim ptype As System.IO.Ports.Parity
For Each ptype In parity
' test to make sure the machine supports the setting
Try
SerialPort1.Parity = ptype
validParity.Add(ptype)
comboBox2.Items.Add(ptype)
Catch ' ignore this entry
End Try
Next ptype
End Sub 'Form1_Load


' Respond to the form closing event by closing the SerialPort instance.
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
serialPort1.Close()
End Sub 'Form1_FormClosing


' The following methods demonstrate the ability to set parameters of the serial port
' through the SerialPort instance. The baud rate of the port will be set in response to
' the corresponding button clicks.
Private Sub button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
serialPort1.BaudRate = 1200
label2.Text = serialPort1.BaudRate.ToString()
End Sub 'button3_Click


Private Sub button4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button4.Click
SerialPort1.BaudRate = 4800
label2.Text = SerialPort1.BaudRate.ToString()
End Sub 'button4_Click


Private Sub button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click
serialPort1.BaudRate = 9600
label2.Text = serialPort1.BaudRate.ToString()
End Sub 'button5_Click


Private Sub comboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comboBox1.SelectedIndexChanged
serialPort1.StopBits = CType(validStopBits(comboBox1.SelectedIndex), System.IO.Ports.StopBits)
label4.Text = serialPort1.StopBits.ToString()
End Sub 'comboBox1_SelectedIndexChanged


Private Sub comboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comboBox2.SelectedIndexChanged
serialPort1.Parity = CType(validParity(comboBox2.SelectedIndex), System.IO.Ports.Parity)
label6.Text = serialPort1.Parity.ToString()
End Sub 'comboBox2_SelectedIndexChanged

End Class 'Form1
y ou go to vb forum, or do a search google using the serial port youlll find a downloadable file for this. It came from 101 examples zip file. or if you can give me yr emil i could send you the whole vb.net app i got this.
 
Last edited:

rigidigital

Senior Member
recieving data serial port vb.net

Function ReceiveSerialData() As String
' Receive strings from a serial port.
Dim returnStr As String = ""

Using com1 As IO.Ports.SerialPort = _
My.Computer.Ports.OpenSerialPort("COM1")
Do
Dim Incoming As String = com1.ReadLine()
If Incoming Is Nothing Then
Exit Do
Else
returnStr &= Incoming & vbCrLf
End If
Loop
com1.Close()
End Using

Return returnStr
End Function
 

rigidigital

Senior Member
This is not much to do with picaxe but anyway......

I have been reading alot about ms Robotics studio. It uses VB and c# to send commands to robots etc.
I was wondering about haow the chip in the robot interprets the commands sent . Then I found the chip/circuit board that you have to get , its near $200. So I dont think Ill be pursuing ms Robotics :)
 

BeanieBots

Moderator
Ya pays ya money ya takes ya choice.

Fit a laptop with a "surf anywhere" mobile phone dongle and you can control your robot just about anywhwere from just about anywhere on the planet.
 

hippy

Ex-Staff (retired)
I may be wrong as I only gave it a brief investigation when MS Robotics Studio came out but it seemed to me the intent was to provide comprehensive robot control on a PC / laptop with communications to hardware to achieve what a desk-bound PC cannot.

It's similar to putting a laptop on a chassis and sending "Go Left" etc to a PICAXE controlling the motors but where the software which sends the commands is often ad hoc, VB6, VB.Net, Java etc, MS Robotics Studio offers a full framework within which to do robot related software development, on the PC rather than in the attached hardware.

The framework supplies a protocol for communicating with hardware so ( it seemed at the time ) it isn't just a case of hooking up a PICAXE and you have MS Robotic Studio control of it; you need hardware which can integrate with the software.

It seemed very appropriate for people who want to use their PC as the core of a robot and are more interested in algorithms and control software but less so for the 'grubby, hands-on' end of hardware interfacing or embedded software on a micro.
 
Top