Sending a data file over a serial bluetooth

mhewett737

Member
Licensed User
Longtime User
I am very new to basic4android.
I have a folder of .dat files on my pc that contain hex byte values that I can send out in std rs232 format to another device which are recieved and used to control electric trains.

I would like to use my Galaxy 2 to send these data files over bluetooth to a bbluetooth to rs232 adapter to this train device.

I am struggling to read these data files with a RAF and send them out as byte streams through bluetooth.
Can some one get me started how you would attack this problem.

Thanks in advance
 

mhewett737

Member
Licensed User
Longtime User
I have installed the recommended code as follows to read my file into an array file:

Sub ReadFile(File.DirRootExternal,"bell_on.dat")
out.InitializeToBytesArray(100) 'size not really important
File.Copy2(File.OpenInput(File.DirRootExternal, "bell_on.dat"), out)
Return out.ToBytesArray
End Sub
I put the ...Dim out As OutputStream .... in the Sub Process globals

I keep getting the folllowing error:

Parameter name cannot hide global variable name.

Suggestions ?
 
Upvote 0

mhewett737

Member
Licensed User
Longtime User
I have successfully read the .dat file into an array.

Now Iwant to send it in serial format over bluetooth
I have put the following code in to initialize Astreams :


Code:
[AStreams.Initialize(Null,serial1.OutputStream, "AStreams")]

When I run debug it successfully compiles but when running on the tablet it pauses with the following error:

"An error has occurred in sub: main_activity_create(B4A line: 100)
AStreams.initialize(Null,serial1.OutputStream ,"Astreams")
java.lang.NullPointerException "

Can you help me here?
 
Upvote 0

mhewett737

Member
Licensed User
Longtime User
LogCat connected to: B4A-Bridge: samsung GT-P3113-
--------- beginning of /dev/log/main
** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_getcommand (B4A line: 127)
AStreams.Initialize(Null,serial1.OutputStream, "AStreams")
java.lang.NullPointerException
at anywheresoftware.b4a.objects.Serial.getOutputStream(Serial.java:241)
at b4a.example.main._getcommand(main.java:916)
at b4a.example.main._bell_click(main.java:406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:155)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:151)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:59)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17273)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
** Activity (main) Resume **
 
Upvote 0

mhewett737

Member
Licensed User
Longtime User
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_getcommand (B4A line: 127)
AStreams.Initialize(Null,serial1.OutputStream, "AStreams")
java.lang.NullPointerException
at anywheresoftware.b4a.objects.Serial.getOutputStream(Serial.java:241)
at b4a.example.main._getcommand(main.java:965)
at b4a.example.main._sfs_click(main.java:1589)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:155)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:151)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:59)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17273)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
 
Upvote 0

mhewett737

Member
Licensed User
Longtime User
At the risk of showing my ignorance I will try to explain what I am doing.

I have files located in the directory storage/sdcar0/ .
There are 8 folders named engine1....engine8
inside each of these folders are multiple files (23)
that are data files i.e. Bell_on.dat.......SCS.dat....horn_on.dat etc.
These files contain 17 to 23 bytes of data always ending in 4D hex or 77 decimal.

There are event buttons for each of these files assigned to direct the program to a subroutine
called "getcommand" to go find the approriate file and send the 17 to 23 byte data string out through Bluetooth in Rs232(9600,8,NP ,1 SB) to a bluetooth to TTL connection to a model train control computer,
the code for the "getcommand" subroutine is :

[/Code:]
Sub getcommand

raf.initialize(File.DirRootExternal,dirname,False)
count = raf.ReadBytes (arr,0,80,raf.CurrentPosition)
AStreams.Initialize(Null,serial1.OutputStream, "AStreams")
AStreams.Write(arr)
For i = offset To count
Log(arr(i))
If arr(i)= 77 Then
i = count
End If
Next
raf.close
AStreams.close
End Sub]

this program runs correctly and loads the "arr" array and displays them correctly with the log display but when I add the " [ AStreams.Initialize(Null,serial1.OutputStream, "AStreams") ] code it compiles but halts the execution at this line during debug..


What I am attempting is to ; read the file into an array called "arr"
output it in serial format through Astreams.

I know I am misunderstanding something here. Have I loaded the output stream so that it can be sent? Or do I copy "arr" to an inputstream and then copy it to the outputstream?

Im sorry I dont understand your previous comments about using the brackets to display my code.

:sign0013::sign0104::sign0085:
 
Upvote 0

mhewett737

Member
Licensed User
Longtime User
Erel,

I obviously dont know what I am doing. I just want to output a data stream in bytes in a rs232 format over a bluetooth network to a device that is waiting to recieve the data . Right now I send the same data from the pc comport through a rs232 uart to ttl adapter and hardwire connection to this device and it works fine. I want to replace the Pc and hardware with a Android tablet and a bluetooth rs232 to TTL reciever.

I have been looking all over the forum for an example and I am stymied.
I dont really know what a socket is except that it is an endpoint for a server,
So I thought serial was what I needed.

I need a better tutorial on this if that is possible.
Thankyou for your efforts.
 
Upvote 0

mhewett737

Member
Licensed User
Longtime User
EREL

Sorry for the delay my PC computer decided to go south so I have been in the process of getting it running.

I hopefully uploaded my project.
I have worked through your tutorials the best I can. I am trying to communicate with my laptop emulating the device that will eventually being used to recieve the serial data through a blue tooth to ttl adapter.
So at the present my Galaxy 3 loads the program and buttons fine. ( I dont send data out yet as I I am not sure of the async code as yet)
The program runs when I push buttons as expected however when I attempt to connect to a blue tooth it sees the blue tooth devices and when I attempt to connect I get the following error:

"unable to start Service Discovery "

I guess once I get pass this step I can work on sending the data next.

Thanks for your help

Mike
 

Attachments

  • traincontrol1.123.zip
    13.1 KB · Views: 329
Upvote 0

mhewett737

Member
Licensed User
Longtime User
Erel,
It appears I have the Tablet Bluetooth connected to the PC successfully with the program code I sent you.
I created bluetooth input and output ports on my PC. I have a VB program that I run that is waiting to recieve a serial data stream in hex .

So now I made the async initialize in the "Sub Get commandcode" (shown in Green in the file I previously sent you line 154) active but changed socket1 to serial1.
When I press a button to send a command it stops at line 154.

Maybe you can help me here.
 
Upvote 0

mhewett737

Member
Licensed User
Longtime User
Erel.

Through trial and error I have actually been able to send good data from my program android tablet to a PC bluetooth that has a Rs232 program to recieve and send data.
I have set up an inbound and outbound bluetooth com ports
Everything is working perfect except that I have to and close the outbound port and reopen it in between sending the next command
If I dont close and reopen the port and send a new data stream it says signal recieved CTS off and DSR off on this rs232 monitor program. and the data does not arrive from the tablet. Some times I get the error "socket closed"
Is this a peculiarity of the free rs232 program or is there something I need to do in my android program.
 
Upvote 0
Top