Serial Transmit and Serial Receive Example

This example shows how to write a simple application that will send and receive serial
data via a Bluetooth connection between a Pocket PC PDA and the BlueSMiRF Module.


To write a program that can send and receive serial data we need 4 program statements and 1 subroutine.

serial.New2 (8,9600,"N",8,1)                        "This statement will Initialize the Serial Com Port"
serial.PortOpen = true (or)  = false             "This statement will Open or Close the Serial Com Port"
serial.Output ("A")                                        "This statement will send 1 character or a string of characters"
serial.EnableOnComm = true (or) = false    "This statement will Enable or Disable Serial Com Port reception"

Sub serial_OnCom                                         "This subroutine is used to handle the Serial Data that is received"
      
(... Put Program Statements here ...)
End Sub


To Initialize the Serial Com Port we need the following program statement:
 
                                     serial.New2 (8,9600,"N",8,1)

 
To Open the Serial Com Port we need the following program statement:


                                      serial.PortOpen=true

To Close the Serial Com Port we need the following program statement:

                                      serial.PortOpen=false

To Send data out the Serial Com Port we need the following program statement:

                                      serial.Output ("A")

To Enable the Receiving of Serial Data we need the following program statement:

                                      serial.EnableOnComm=true

To Disable the Receiving of Serial Data we need the following Program statement:

                                      serial.EnableOnComm=false

To handle the Serial Data once it has been Received we need the following Subroutine:

                                      Sub serial_OnCom

                                             (... Put Program Statements here ...)

                                      End Sub

For this example, the BlueSMiRF Module will have its TX pin connected to its RX pin. 
This will put the BlueSMiRF Module into a "loopback" mode where every byte that it
receives it will send back out via the wireless Bluetooth connection with the PDA.  The
PDA will send bytes to the BlueSMiRF Module and the BlueSMiRF will send those bytes
right back to the PDA.  The schematic of how the BlueSMiRF is connected is shown below.

 
Here is the application program running on the Pocket PC PDA.
Below is the code listing for example: Serial TX | RX

Sub Globals
x=0
ULfLAG=1
portflag = 0
strlabel0="COM 8: 9600,N,8,1"
strlabel1=" [OFF]"
strlabel2=" [ ON]"
strlabel3=" = "
strlabeltemp=""
End Sub

Sub App_Start
Form1.show
label1.Text = strlabel0 & strlabel1 & strlabel3
strlabeltemp  = strlabel0 & strlabel1 & strlabel3
serial.New2 (8,9600,"N",8,1)
textbox1.FontSize=13
End Sub

Sub OpenPort_Click
If portflag = 0 Then
serial.PortOpen=true
portflag = 1
label1.Text = strlabel0 & strlabel2 & strlabel3
strlabeltemp=label1.Text
End If
serial.enableoncomm=true
End Sub

Sub ClosePort_Click
If portflag = 1 Then
TextBox1.Text=""
serial.enableoncomm=false
serial.PortOpen=false
portflag = 0
label1.Text = strlabel0 & strlabel1 & strlabel3
strlabeltemp=label1.Text
End If
End Sub

Sub ButtonA_Click
If ULflag=0 Then
serial.Output ("a")
label1.Text = strlabeltemp & "a"
End If
If ULflag=1 Then
serial.Output ("A")
label1.Text = strlabeltemp & "A"
End If
End Sub

Sub ButtonB_Click
If ULflag=0 Then
serial.Output ("b")
label1.Text = strlabeltemp & "b"
End If
If ULflag=1 Then
serial.Output ("B")
label1.Text = strlabeltemp & "B"
End If
End Sub

Sub serial_OnCom
TextBox1.Text=TextBox1.Text & serial.Inputstring
End Sub

Sub ClearWindow_Click
TextBox1.Text=""
End Sub

Sub ButtonC_Click
If ULflag=0 Then
serial.Output ("c")
label1.Text = strlabeltemp & "c"
End If
If ULflag=1 Then
serial.Output ("C")
label1.Text = strlabeltemp & "C"
End If
End Sub

Sub ButtonD_Click
If ULflag=0 Then
serial.Output ("d")
label1.Text = strlabeltemp & "d"
End If
If ULflag=1 Then
serial.Output ("D")
label1.Text = strlabeltemp & "D"
End If
End Sub

Sub ButtonE_Click
If ULflag=0 Then
serial.Output ("e")
label1.Text = strlabeltemp & "e"
End If
If ULflag=1 Then
serial.Output ("E")
label1.Text = strlabeltemp & "E"
End If
End Sub

Sub ButtonF_Click
If ULflag=0 Then
serial.Output ("f")
label1.Text = strlabeltemp & "f"
End If
If ULflag=1 Then
serial.Output ("F")
label1.Text = strlabeltemp & "F"
End If
End Sub

Sub ButtonG_Click
If ULflag=0 Then
serial.Output ("g")
label1.Text = strlabeltemp & "g"
End If
If ULflag=1 Then
serial.Output ("G")
label1.Text = strlabeltemp & "G"
End If
End Sub

Sub ButtonH_Click
If ULflag=0 Then
serial.Output ("h")
label1.Text = strlabeltemp & "h"
End If
If ULflag=1 Then
serial.Output ("H")
label1.Text = strlabeltemp & "H"
End If
End Sub

Sub ButtonI_Click
If ULflag=0 Then
serial.Output ("i")
label1.Text = strlabeltemp & "i"
End If
If ULflag=1 Then
serial.Output ("I")
label1.Text = strlabeltemp & "I"
End If
End Sub

Sub ButtonJ_Click
If ULflag=0 Then
serial.Output ("j")
label1.Text = strlabeltemp & "j"
End If
If ULflag=1 Then
serial.Output ("J")
label1.Text = strlabeltemp & "J"
End If
End Sub

Sub ButtonK_Click
If ULflag=0 Then
serial.Output ("k")
label1.Text = strlabeltemp & "k"
End If
If ULflag=1 Then
serial.Output ("K")
label1.Text = strlabeltemp & "K"
End If
End Sub

Sub ButtonL_Click
If ULflag=0 Then
serial.Output ("l")
label1.Text = strlabeltemp & "l"
End If
If ULflag=1 Then
serial.Output ("L")
label1.Text = strlabeltemp & "L"
End If
End Sub

Sub ButtonM_Click
If ULflag=0 Then
serial.Output ("m")
label1.Text = strlabeltemp & "m"
End If
If ULflag=1 Then
serial.Output ("M")
label1.Text = strlabeltemp & "M"
End If
End Sub

Sub ButtonN_Click
If ULflag=0 Then
serial.Output ("n")
label1.Text = strlabeltemp & "n"
End If
If ULflag=1 Then
serial.Output ("N")
label1.Text = strlabeltemp & "N"
End If
End Sub

Sub ButtonO_Click
If ULflag=0 Then
serial.Output ("o")
label1.Text = strlabeltemp & "o"
End If
If ULflag=1 Then
serial.Output ("O")
label1.Text = strlabeltemp & "O"
End If
End Sub

Sub ButtonP_Click
If ULflag=0 Then
serial.Output ("p")
label1.Text = strlabeltemp & "p"
End If
If ULflag=1 Then
serial.Output ("P")
label1.Text = strlabeltemp & "P"
End If
End Sub

Sub ButtonQ_Click
If ULflag=0 Then
serial.Output ("q")
label1.Text = strlabeltemp & "q"
End If
If ULflag=1 Then
serial.Output ("Q")
label1.Text = strlabeltemp & "Q"
End If
End Sub

Sub ButtonR_Click
If ULflag=0 Then
serial.Output ("r")
label1.Text = strlabeltemp & "r"
End If
If ULflag=1 Then
serial.Output ("R")
label1.Text = strlabeltemp & "R"
End If
End Sub

Sub ButtonS_Click

If ULflag=0 Then
serial.Output ("s")
label1.Text = strlabeltemp & "s"
End If
If ULflag=1 Then
serial.Output ("S")
label1.Text = strlabeltemp & "S"
End If
End Sub

Sub ButtonT_Click
If ULflag=0 Then
serial.Output ("t")
label1.Text = strlabeltemp & "t"
End If
If ULflag=1 Then
serial.Output ("T")
label1.Text = strlabeltemp & "T"
End If
End Sub

Sub ButtonU_Click
If ULflag=0 Then
serial.Output ("u")
label1.Text = strlabeltemp & "u"
End If
If ULflag=1 Then
serial.Output ("U")
label1.Text = strlabeltemp & "U"
End If
End Sub

Sub ButtonV_Click
If ULflag=0 Then
serial.Output ("v")
label1.Text = strlabeltemp & "v"
End If
If ULflag=1 Then
serial.Output ("V")
label1.Text = strlabeltemp & "V"
End If
End Sub

Sub ButtonW_Click
If ULflag=0 Then
serial.Output ("w")
label1.Text = strlabeltemp & "w"
End If
If ULflag=1 Then
serial.Output ("W")
label1.Text = strlabeltemp & "W"
End If
End Sub

Sub ButtonX_Click
If ULflag=0 Then
serial.Output ("x")
label1.Text = strlabeltemp & "x"
End If
If ULflag=1 Then
serial.Output ("X")
label1.Text = strlabeltemp & "X"
End If
End Sub

Sub ButtonY_Click
If ULflag=0 Then
serial.Output ("y")
label1.Text = strlabeltemp & "y"
End If
If ULflag=1 Then
serial.Output ("Y")
label1.Text = strlabeltemp & "Y"
End If
End Sub

Sub ButtonZ_Click
If ULflag=0 Then
serial.Output ("z")
label1.Text = strlabeltemp & "z"
End If
If ULflag=1 Then
serial.Output ("Z")
label1.Text = strlabeltemp & "Z"
End If
End Sub

Sub Button31_Click
serial.Output ("0")
label1.Text = strlabeltemp & "0"
End Sub

Sub Button32_Click
serial.Output ("1")
label1.Text = strlabeltemp & "1"
End Sub

Sub Button33_Click
  serial.Output ("2")
   label1.Text = strlabeltemp & "2"
End Sub

Sub Button34_Click
serial.Output ("3")
label1.Text = strlabeltemp & "3"
End Sub

Sub Button35_Click
serial.Output ("4")
label1.Text = strlabeltemp & "4"
End Sub

Sub Button36_Click
serial.Output ("5")
label1.Text = strlabeltemp & "5"
End Sub

Sub Button37_Click
serial.Output ("6")
label1.Text = strlabeltemp & "6"
End Sub

Sub Button38_Click
serial.Output ("7")
label1.Text = strlabeltemp & "7"
End Sub

Sub Button39_Click
serial.Output ("8")
label1.Text = strlabeltemp & "8"
End Sub

Sub Button40_Click
serial.Output ("9")
label1.Text = strlabeltemp & "9"
End Sub

Sub Button51_Click
serial.Output ("$")
label1.Text = strlabeltemp & "$"
End Sub

Sub Button52_Click
serial.Output ("#")
label1.Text = strlabeltemp & "#"
End Sub

Sub Button53_Click
serial.Output ("%")
label1.Text = strlabeltemp & "%"
End Sub

Sub Button54_Click
serial.Output ("@")
label1.Text = strlabeltemp & "@"
End Sub

Sub Button55_Click
serial.Output ("!")
label1.Text = strlabeltemp & "!"
End Sub

Sub Button56_Click
serial.Output ("?")
label1.Text = strlabeltemp & "?"
End Sub

Sub ButtonLower_Click
ULflag=0
buttonA.Text="a"
buttonB.Text="b"
buttonC.Text="c"
buttonD.Text="d"
buttonE.Text="e"
buttonF.Text="f"
buttonG.Text="g"
buttonH.Text="h"
buttonI.Text="i"
buttonJ.Text="j"
buttonK.Text="k"
buttonL.Text="l"
buttonM.Text="m"
buttonN.Text="n"
buttonO.Text="o"
buttonP.Text="p"
buttonQ.Text="q"
buttonR.Text="r"
buttonS.Text="s"
buttonT.Text="t"
buttonU.Text="u"
buttonV.Text="v"
buttonW.Text="w"
buttonX.Text="x"
buttonY.Text="y"
buttonZ.Text="z"
End Sub

Sub ButtonUpper_Click
ULflag=1
buttonA.Text="A"
buttonB.Text="B"
buttonC.Text="C"
buttonD.Text="D"
buttonE.Text="E"
buttonF.Text="F"
buttonG.Text="G"
buttonH.Text="H"
buttonI.Text="I"
buttonJ.Text="J"
buttonK.Text="K"
buttonL.Text="L"
buttonM.Text="M"
buttonN.Text="N"
buttonO.Text="O"
buttonP.Text="P"
buttonQ.Text="Q"
buttonR.Text="R"
buttonS.Text="S"
buttonT.Text="T"
buttonU.Text="U"
buttonV.Text="V"
buttonW.Text="W"
buttonX.Text="X"
buttonY.Text="Y"
buttonZ.Text="Z"
End Sub
BlueSMiRF wired up in LoopBack Mode.
We can use the ImageButton control in Basic4ppc
to improve the look of the software application
running on the Pocket PC PDA.