Thank You! I will plug away at this and see if I can get it to work.
Thank You! I will plug away at this and see if I can get it to work.
Shawn
I can't get this to workHoping you can help me! When I run this very simple android program with prefix I get an almost immediate error "Unfortunately, Bluetooth Example has stopped" and the program closes. I am using the Bluetooth chat example to simply display my data in the chat window. I have very limited serial communication experience. With the prefix disabled I get the data as expected, so the communication is good.
This is the Android routine where I switch between prefix or no prefix mode.
I tried a few variations here with no luckCode:Sub Activity_Create(FirstTime As Boolean) Activity.LoadLayout("2") If AStream.IsInitialized = False Then AStream.InitializePrefix(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream") ' AStream.Initialize(Main.Serial1.InputStream, Main.Serial1.OutputStream, "AStream") 'NO PREFIX MODE End If txtLog.Width = 100%x End Sub
My code on an 18F4620 with internal 8mhz osc. I assume I have the prefix correct.Code:Sub AStream_NewData (Buffer() As Byte) LogMessage("You", BytesToString(Buffer, 0, Buffer.Length, "UTF8")) ' Incoming= BytesToString(Buffer, 0, Buffer.Length, "ISO8859_1") ' LogMessage("You", BytesToString(Buffer, 0, Buffer.Length, "ISO8859_1")) End Sub
Code:define DEBUG_REG PORTC define DEBUG_BIT 6 define DEBUG_BAUD 9600 define DEBUG_MODE 0 A1 var byte A2 var byte A3 var byte A4 var byte prefix1 var byte prefix2 var byte prefix3 var byte prefix4 var byte A1=11 A2=22 A3=33 A4=44 Prefix1=23 'http://mothereff.in/byte-counter 11,22,33,44,11,22,33,44 Prefix2=0 Prefix3=0 Prefix4=0 main: pause 1000 debug Prefix1,Prefix2,Prefix3,Prefix4,dec2 A1,",",dec2 A2,",",dec2 A3,",",dec2 A4,",",dec2 A1,",",dec2 A2,",",dec2 A3,",",dec2 A4 pause 1000 goto main end
Shawn
Use this line
AStream.InitializePrefix(serial1.InputStream, False, serial1.OutputStream, "AStream")
instead of
AStream.InitializePrefix(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream")
The False part I bet fixes your problem.
My very simple working code:
Code:OSCCON=%01110000 ' SET TO 8 MHZ internal oscillator DEFINE OSC 08 ' 8MHZ clock speed DEFINE HSER_BAUD 9600 ' Set baud rate DEFINE HSER_RCSTA 90h DEFINE HSER_CLROERR 1 ' Clear overrun error upon DEFINE HSER_TXSTA 24h ' Set transmit status and control register DEFINE HSER_SPBRG 51 ' 9600 Bauds RPM var word SPEED var word TPS var byte volts var byte adc1 var word adc2 var word prefix1 var byte prefix2 var byte prefix3 var byte prefix4 var byte rpm = 3800 speed = 60 tps = 10 volts = 122 ADC1 = 512 ADC2 = 200 Prefix1 = 22 Prefix2 = 0 Prefix3 = 0 Prefix4 = 0 main: pause 500 hserout [Prefix1,Prefix2,Prefix3,Prefix4,dec5 rpm, dec3 speed, dec3 TPS, dec3 volts, dec4 adc1, dec4 adc2] goto main endCode:Sub Process_Globals Dim AStream As AsyncStreams Dim Incoming As String Dim RPM As Int Dim SPEED As Int Dim TPS As Double Dim VOLTS As Int Dim ADC1 As Int Dim ADC2 As Int End Sub Sub Activity_Create(FirstTime As Boolean) Activity.LoadLayout("2") If AStream.IsInitialized = False Then AStream.InitializePrefix(Main.Serial1.InputStream, False, Main.Serial1.OutputStream, "AStream") End If Update_Display End Sub Sub AStream_NewData (Buffer() As Byte) Log( BytesToString(Buffer, 0, Buffer.Length, "ISO8859_1")) Incoming = BytesToString(Buffer, 0, Buffer.Length, "ISO8859_1") RPM = (Incoming.SubString2(0,5)) SPEED = (Incoming.SubString2(5,8)) TPS = (Incoming.SubString2(8,11)) VOLTS = (Incoming.SubString2(11,14)) ADC1 = (Incoming.SubString2(14,18)) ADC2 = (Incoming.SubString2(18,22)) Update_Display End Sub
Shawn
Thanks for the working example. Can you explain the complete setup? That HSEROUT goes to what, an external bluetooth module?
This would make an excellent article with some narrative explaining what is going on, hardware/software requirements, etc.
Robert
The hserout goes to a RN42 bluetooth modem. Could be done with debug as shown in an earlier post. Once paired it isn't much different than any other serial connection, but the Android side of things takes some time to wrap your head around. The prefix is a pain, but works very well once working. It is basically just a 32 bit header detailing the number of bytes in the serial string.
Shawn
Just thought I should bring this thread up to date... In the end I gave up on the alternatives, and I broke down and bought Basic4Android since it seems to be the most mature thing out there. If anybody is interested in a 50% off coupon code, you can use this " dqqshs " You go to their purchase page and hit the "Plimus" button, then enter the code.
I'm debating which version to get, Enterprise $99 at 50%?
Bang for buck looks good.
Which are you getting?
(Just checking so I can copy your code)
Robert
Bookmarks