I've seen the examples for handling multiple asyncstreams connections at the server end, e.g.
,
But I'm struggling to convert what's needed to this particular situation so any help or pointers would be appreciated....
I'm trying to communicate with a number of devices that are attached via a serial link to the same host (the host is just a transparent serial to Ethernet adaptor).
The devices are passive in that they only respond to specific ASCII string commands that are addressed to them - each has a unique ID which is included in the string command.
If they receive a command they recognise with the correct ID, then they respond with an ASCII string.
I have a class that handles the comms to each individual device;
In the main module I have;
Talking to only one of the devices works fine - I can see the request go out in the logs & in txtRequest, and a valid response come back in the logs and txtReply.
But if I instigate more than one instance of the Comms class, the requests still seem to go out correctly, but the response I get in ast_NewText (and in astreams_NewData in the AsyncStreamsText class) is garbled nonsense.
Presumable that's because the replies from both devices are getting scrambled in the same asyncstream?
I've tried use a connections map & the Sender object to ensure that the stream goes to the correct class instance (see the commented bits in the code above), but it makes no difference. I've tried a similar thing within the AsyncStreamsText class.
Is there any way I can split up the comms lines so that each class instance only receives the reply to its own request?
MJPEG / CCTV Server
A few days ago I've helped a developer with implementing a MJPEG client: https://www.b4x.com/android/forum/threads/73702/#content It was a good experience as while implementing it I've become familiar with the MJPEG protocol. MJPEG is quite simple. Each frames look like: --myboundary...
www.b4x.com
[B4X] FTP Server implemented with Socket and AsyncStreams
(Note that the FTP client above is not part of this solution. It only demonstrates how you can use a standard FTP client to communicate with the server.) This is an example of using low level network features to implement a high level protocol. It is an implementation of a standard FTP server...
www.b4x.com
B4J Tutorial - B4J CCTV example
This example is similar to the previous example: http://www.basic4ppc.com/android/forum/threads/android-based-closed-circuit-tv-cctv-example.23601/#content The server is now implemented in B4J. In order to run this example you will need to allow incoming connections on port 17178 (or other...
www.b4x.com
But I'm struggling to convert what's needed to this particular situation so any help or pointers would be appreciated....
I'm trying to communicate with a number of devices that are attached via a serial link to the same host (the host is just a transparent serial to Ethernet adaptor).
The devices are passive in that they only respond to specific ASCII string commands that are addressed to them - each has a unique ID which is included in the string command.
If they receive a command they recognise with the correct ID, then they respond with an ASCII string.
I have a class that handles the comms to each individual device;
Class Comms:
Sub Class_Globals
Private fx As JFX
Private IsConnected As Boolean
Private ast As AsyncStreamsText
Private socket As Socket
Private mCallback As Object
Private mEventName As String
' Private connections As Map
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize (Callback As Object, EventName As String, lid As Int, did As Int)
mCallback=Callback
mEventName=EventName
' connections.Initialize
Dim str As String = BuildCommsStr(lid, did) 'BuildCommsStr just builds the test request string I'm using.
TCPConnect(str)
End Sub
Private Sub TCPConnect(msg As String)
If ast.IsInitialized Then ast.Close
If socket.IsInitialized Then socket.Close
socket.Initialize("socket")
socket.Connect("192.168.1.99", 4050, 3000)
Wait For Socket_Connected (Successful As Boolean)
If Successful Then
ast.Initialize(Me, "ast", socket.InputStream, socket.OutputStream)
' connections.Put(ast, mCallback)
ast.Write(msg)
CallSubDelayed2(mCallback, mEventName & "_DataSent", msg)
Wait For Reply_Complete' (success As Boolean)
End If
ast.Close
socket.Close
setState(False)
End Sub
Private Sub ast_NewText (Text As String)
Log(Text)
' Dim a As AsyncStreamsText = Sender
CallSubDelayed2(mCallback, mEventName & "_Reply", Text)
' CallSubDelayed2(connections.Get(a), mEventName & "_Reply", Text)
CallSubDelayed(Me, "Reply_Complete")
End Sub
In the main module I have;
Main module:
Sub Button1_Click
Dim com1 As Comms
com1.Initialize(Me, "Comm1", 1, 20)
' Dim com2 As Comms
' com2.Initialize(Me, "Comm2", 2, 20)
End Sub
Sub Comm1_DataSent(msg As String)
txtRequest.Text=msg
End Sub
Sub Comm1_Reply(msg As String)
txtReply.Text=msg
End Sub
Sub Comm2_DataSent(msg As String)
txtRequest2.Text=msg
End Sub
Sub Comm2_Reply(msg As String)
txtReply2.Text=msg
End Sub
Talking to only one of the devices works fine - I can see the request go out in the logs & in txtRequest, and a valid response come back in the logs and txtReply.
But if I instigate more than one instance of the Comms class, the requests still seem to go out correctly, but the response I get in ast_NewText (and in astreams_NewData in the AsyncStreamsText class) is garbled nonsense.
Presumable that's because the replies from both devices are getting scrambled in the same asyncstream?
I've tried use a connections map & the Sender object to ensure that the stream goes to the correct class instance (see the commented bits in the code above), but it makes no difference. I've tried a similar thing within the AsyncStreamsText class.
Is there any way I can split up the comms lines so that each class instance only receives the reply to its own request?