Binary Data Question

comptrmedic

Member
Licensed User
Longtime User
Hi All,

I'm trying to send a binary string (0011110101010100000011000) using AsyncStreams / EditText.txt as shown in the documentation through a Socket to a remote serial server that will be eventually be keying a tiny transmitter. It's all working just fine except the log is showing "Sending: 1.11101010101E22". Anybody know what that's about? I assume it's being converted for a reason but don't understand why? Thanks, Jerry
 

Beja

Expert
Licensed User
Longtime User
It looks like it's being converted to numeric value. I would suggest that you first convert it to hex using bin2hex function, you will only have 4 bytes, and then convert it back to binary at the destination. In vb6 I was using off-the-shelf functions from the Internet!
good luck.
 
Upvote 0

comptrmedic

Member
Licensed User
Longtime User
Beja: thats the problem, it needs to be the raw 1's and 0's when it reaches the other end, once the socket is opened the data goes straight to the data pin of the transmitter.

Erel: here's the code I'm using, thanks for your help guys!!

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim AStreams As AsyncStreams
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim Socket1 As Socket
   Dim Timer1 As Timer
   Dim Button1 As Button
   Dim EditText1 As EditText
   Dim EditText2 As EditText
   Dim Button2 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("serialcom")
   Socket1.Initialize("Socket1")
   Socket1.Connect("192.168.1.152" , 10001, 0)
   Log ("Socket Connect")

End Sub

Sub Socket1_Connected (Successful As Boolean)
    If Successful = False Then
        Msgbox(LastException.Message, "Error connecting")
        Return
      Else
      AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")
    End If
   Log ("Connected No Error")
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")
    ToastMessageShow(msg, False)
    Log(msg)
End Sub

Sub AStreams_Error
    ToastMessageShow(LastException.Message, True)
End Sub

Sub Button1_Click
   EditText1.Text = (0011110101010100000011000)
    If AStreams.IsInitialized = False Then Return
    If EditText1.Text.Length > 0 Then
        Dim buffer() As Byte
        buffer = EditText1.Text.GetBytes("UTF8")
       AStreams.Write(Array As Byte(EditText1.Text))
        EditText1.SelectAll
        Log("Sending: " & EditText1.Text)
   End If
   EditText1.Text = (0011110101010100000011000)
    If AStreams.IsInitialized = False Then Return
    If EditText1.Text.Length > 0 Then
        Dim buffer() As Byte
        buffer = EditText1.Text.GetBytes("UTF8")
       AStreams.Write(Array As Byte(EditText1.Text))
        EditText1.SelectAll
        Log("Sending: " & EditText1.Text)
   End If

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button2_Click
   Socket1.Close
   AStreams.Close
   Log ("Socket Disconnected")
   Log ("Astreams Disconnected")
   Activity.Finish
   Log ("Program Closed")
End Sub
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
once the socket is opened the data goes straight to the data pin of the transmitter
That needs clarification as to how you are interfacing the received data to the transmitter pin. Is it being serialised somehow so all the bits in the byte are used, in which which case what endianness is assumed? Or is it for example, that each byte is being latched for a period with just one specific bit of the byte being associated with the pin?
 
Upvote 0

merlin2049er

Well-Known Member
Licensed User
Longtime User
That's great. I need to send a binary string via bluetooth to an arduino.

I need to calculate a checksum first, and include it in my string.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…