Asyncstream prefix not working

dddvvv

Member
Licensed User
Longtime User
hi everyone

ive been fighting with this issue on and off for the last 3 weeks. im trying to send bytes from a pic microcontroller.

im using the bluetooth chat example. if i use it in non prefix mode, i receive the bytes/text, although its all broken up. heres the non-prefix code

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("2")
   If AStream.IsInitialized = False Then
      AStream.Initialize(Main.serial1.InputStream, Main.serial1.OutputStream, "AStream")
   End If
   txtLog.Width = 100%x
End Sub

of course, the solution is to use the prefix mode, and this is what im trying, unmodified from the bluetooth chat;

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("2")
   If AStream.IsInitialized = False Then
      AStream.InitializePrefix(Main.serial1.InputStream, true, Main.serial1.OutputStream, "AStream")
   End If
   txtLog.Width = 100%x
End Sub

the app crashes after connection, which leads me to beleive that im sending the wrong prefix info, from the microcontroller.

here is what im trying to send;4 bytes only. all variables are bytes;

prefix1 = 4
prefix2 = 4
prefix3 = 4
prefix4 = 4 ;;these are the prefix bytes

data1 = 34
data2 = 65
data3 = 232
data4 = 45 ;;;this is the data i need.

here's my microcontroller output, as displayed by the asynchstream with no prefix ;
4444346523245
this is all out of order, and broken up, although its arriving, so hardware bluetooth is ok.

ive gone over all messages pertaining to prefix mode in this forum , but still cannot come up with a solution. prefix mode still keeps on crashing.

any help will be appreciated, and i can change the bytes from the microcontroller.

thanks
 

derez

Expert
Licensed User
Longtime User
The prefix is wrong. If you send 4 bytes then the prefix should be 4, 0, 0, 0.
If the prefix number is larger then the message the reciever will keep waiting for the rest and will not issue newdata event.
You can also try to change true to false in the initialization.
 
Last edited:
Upvote 0

dddvvv

Member
Licensed User
Longtime User
thanks for the reply. i got around to working on this today, and its functioning now. among other problems, the microchip was sending "dec" bytes.

really appreciate the help.

another tiny issue is that if i send bytes to the microcontroller, they are the ascii representation of the text, :

send 123, the micro gets it as 49 50 51 .

i want them as bytes, so im trying the byteconverter lib.
here's my code

B4X:
Dim strbytes() As Byte ' in sub global

Sub btnSend_Click
'   AStream.Write(BConv.STRINGToByteS(txtInput.Text, "utf8"))
'   AStream.Write(txtInput.Text.GetBytes("UTF8"))
   strbytes = BConv.stringtobytes(txtInput.Text, "utf8")
   AStream.Write(strbytes)
   txtInput.SelectAll
   txtInput.RequestFocus
   LogMessage("SEND", txtInput.Text)
End Sub

im still getting the same ascii bytes at the microcontroller, even after using this code with byte converter,

what im i doing wrong?

thanks for reading.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Try "UTF-8" instead of "utf-8" (a guess ?!)
check the bytes that you send (log(strbytes(0) & " " & strbytes(1) ...)
If this is ok - you have to check in the microcontroller.
 
Upvote 0

Graham

Member
Licensed User
Longtime User
DDDVVV
Can you give me some guidance. I can talk from XOOM to microchip but from microchip to XOOM isn't working. I have tried BIN, HEX and DEC with no luck.


B4X:
Send_BT:
  Prefix1=0
  Prefix2=0
  Prefix3=0
  Prefix4=4
  RPM1T=34
  RPM2T=65
  RPM3T=232
  RPM4T=45
  debug hex Prefix1,hex Prefix2,hex Prefix3,hex Prefix4,hex RPM1T,hex RPM2T,hex RPM3T,hex RPM4T
Return

 
Upvote 0

KitCarlson

Active Member
Licensed User
Longtime User
Post #5 by Erel states the problem. There is a difference between "1" the character and 1 a (byte) number. Perhaps looking at an ascii chart will help.
 
Upvote 0

dddvvv

Member
Licensed User
Longtime User
@ Graham

I see you are using picbasic. Problem is, if you use hex, dec, then the pic will send an unsigned byte , that is, 0 to 255. The chart program is expecting a signed byte, -127 to 127.

The solution is to send just the prefix without the hex modifier. That is,

Debug prefix1, prefix2, prefix3, prefix4, dec rpm1t, dec rpm2t ....or hex rpm1t, hex rpm2t....etc.

Prefix1 should have the 4 value, and the rest of the prefixes should have 0. You have it as prefix1=0.

Thats what worked for me.
 
Upvote 0

Graham

Member
Licensed User
Longtime User
Thank you!!

 
Upvote 0

dddvvv

Member
Licensed User
Longtime User
Another Issue

Im trying on stage 2 of this project and im stuck.

Im trying to receive 8 unsigned bytes from the pic micro, and im trying it with this code

B4X:
Sub AStream_NewData (Buffer() As Byte)
   Label2.TEXT = "PIC18F"  & Buffer(0) 
End Sub

i get the pic18f and 48 (ascii 0) displayed. the byte representing 48 is supposed to increment by 1 every second, but its not. what im i doing wrong?

eventually, the 8 unsigned bytes will be loaded into two integers, on the android side. i've been playing around with byte converter, and im nearly done figuring it out.

i was wondering if its possible, and how to do this the easiest way, in the same subroutine:

PHP:
receive the 8 unsigned bits from astream prefix
convert them to 2 integers of 4 bytes each
display the two integers in a label

any help/code snippet will be appreciated.

thanks
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…