Android Question Serial via Bluetooth with dsPic30F

diegogubellini

Member
Licensed User
Longtime User
Hello all,

I'm trying to use the bluetooth to send and receive data by the UART of dsPic30F. I used the code of this tutorial http://www.b4x.com/android/forum/threads/android-bluetooth-bluetoothadmin-tutorial.14768/

On micro side I use a dsPic30F and a MIKROELEKTRONIKA Basic compiler. The code on dsPic is very easy:

B4X:
inizio:

      delay_ms(1)
      if (UART1_Data_Ready() = 1) then
        receive = UART1_Read()
        UART1_Write(receive)
      end if
      goto inizio

Basically the micro send back what it receive on the UART
The app side I used this code to send the char "f":

B4X:
Sub AStream_NewData (Buffer() As Byte)
    LogMessage("ECU", BytesToString(Buffer, 0, Buffer.Length, "ASCII"))
End Sub

Sub AStream_Error
    ToastMessageShow("Connection is broken", True)
    btn_send.Enabled = False
End Sub

Sub AStream_Terminated
    AStream_Error
End Sub

Sub btn_send_Click

    pb_avanzamento.Progress=0
    AStream.Write("f".GetBytes("ASCII"))
    LogMessage("Phone", "f")
    pb_avanzamento.Progress=100

End Sub

Sub LogMessage(From As String, Msg As String)
    et_log.Text = et_log.Text & From & ": " & Msg & CRLF
    et_log.SelectionStart = et_log.Text.Length
End Sub

On the editText (et_log) everything works properly and every time I push the btw_send I can see :

Phone: f
ECU: f

The second step is send back something different. the variable "receive" is a byte, it represent the ASCII value of char "f". If I add 1 the char sent back should be "g". If I try to change the code of dsPic with :

B4X:
inizio:

      delay_ms(1)
      if (UART1_Data_Ready() = 1) then
        receive = UART1_Read()
          receive = receive + 1
        UART1_Write(receive)
      end if
      goto inizio

it doesn't work anymore. I aspect to receive "g" instead of "f" but for some reason the app doesn't receive nothing. Any idea

Thanks
 

diegogubellini

Member
Licensed User
Longtime User
This is the code:

B4X:
Sub Serial1_Connected (Success As Boolean)
    ProgressDialogHide
    Log("connected: " & Success)
    If Success = False Then
        Log(LastException.Message)
        ToastMessageShow("Error connecting: " & LastException.Message, True)
    Else
        'attiva le operazioni Bluetooth
        Msgbox("ECU connected", "BLUETOOTH")
        btn_send.Enabled=True
        btn_read.Enabled=True
        pb_avanzamento.Progress = 0
        If AStream.IsInitialized = False Then
            AStream.InitializePrefix(serial1.InputStream, True, serial1.OutputStream, "AStream")
        End If

    End If
End Sub
 
Upvote 0

diegogubellini

Member
Licensed User
Longtime User
I change it with this :

B4X:
Sub Serial1_Connected (Success As Boolean)
    ProgressDialogHide
    Log("connected: " & Success)
    If Success = False Then
        Log(LastException.Message)
        ToastMessageShow("Error connecting: " & LastException.Message, True)
    Else
        'attiva le operazioni Bluetooth
        Msgbox("ECU connected", "BLUETOOTH")
        btn_send.Enabled=True
        btn_read.Enabled=True
        pb_avanzamento.Progress = 0
        If AStream.IsInitialized = False Then
            'AStream.InitializePrefix(serial1.InputStream, True, serial1.OutputStream, "AStream")
            AStream.Initialize(serial1.InputStream, serial1.OutputStream, "AStream")
        End If

    End If
End Sub

Now it is working!!!

Thanks Erel
 
Upvote 0
Top