Android Question send udp command

Gianluca Esposito

Member
Licensed User
Longtime User
Hi, I have to send three bytes via UDP to control plc:

This is a 3 byte command:
The first byte is the command, 32 (active means on) or 33 (inactive).
Second byte is the relay number (1-8).
Third byte is the on time. Set this to zero for un-timed operation, or 1-255 for a pulse in 100mS intervals (100mS to 25.5 seconds).
For example: 0x20 - turn the relay on command
0x03 - relay 3
0x32 (50) - 5 seconds (50 * 100ms)
Board will return 0 for success, 1 for failure
Note - All bytes in a command must be sent in one TCP/IP packet .


How can I do?

This is not working .

Dim Packet As UDPPacket
Dim data() As Byte
Dim bc As ByteConverter
Dim pack() As Byte = bc.HexToBytes("210332")

Packet.Initialize(pack, "192.168.1.50", 17494)
UDPSocket1.Send(Packet)

Thanks
 

Gianluca Esposito

Member
Licensed User
Longtime User
Thanks .
I used tcp and sending work !!
I can't capture the answers. Can you help me ?
Thanks

#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: Test Client Socket
#VersionCode: 2
#VersionName: 1.1
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim CltSock As Socket
Dim Astreams As AsyncStreams
' If you install the "SocketServer" on local machine - change this ip to "127.0.0.1"
Dim ip As String : ip = "192.168.1.50"

Dim port As Int: port = 17494
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 btn_client As Button
Dim lbl_status As Label
Dim EditText1 As EditText
Dim txt_out As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("ClientForm")
EditText1.Text = ip
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")

lbl_status.Text = msg

End Sub

Sub btn_client_Click
CltSock.Initialize("Client")
CltSock.Connect(ip,port,20000)

End Sub

Sub Client_Connected(ConStatus As Boolean)
If ConStatus = True Then
Msgbox("Connection Successful","")
Astreams.InitializePrefix(CltSock.InputStream, False, CltSock.OutputStream, "AStreams")
Else
Msgbox(LastException.Message, "Error connecting")

End If

End Sub

Sub EditText1_EnterPressed
ip = EditText1.Text
End Sub
Sub txt_out_EnterPressed
If Astreams.IsInitialized = False Then Return
If txt_out.Text.Length > 0 Then

Dim bc As ByteConverter
Dim pack() As Byte = bc.HexToBytes(txt_out.text)
Astreams.Write(pack)

End If
End Sub
 
Upvote 0

Gianluca Esposito

Member
Licensed User
Longtime User
SorryI didn't know.
Do you have any suggestions?
Thanks

B4X:
#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: Test Client Socket
#VersionCode: 2
#VersionName: 1.1
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim CltSock As Socket
Dim Astreams As AsyncStreams
' If you install the "SocketServer" on local machine - change this ip to "127.0.0.1"
Dim ip As String : ip = "192.168.1.50"

Dim port As Int: port = 17494
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 btn_client As Button
Dim lbl_status As Label
Dim EditText1 As EditText
Dim txt_out As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("ClientForm")
EditText1.Text = ip
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")

lbl_status.Text = msg

End Sub

Sub btn_client_Click
CltSock.Initialize("Client")
CltSock.Connect(ip,port,20000)

End Sub

Sub Client_Connected(ConStatus As Boolean)
If ConStatus = True Then
Msgbox("Connection Successful","")
Astreams.InitializePrefix(CltSock.InputStream, False, CltSock.OutputStream, "AStreams")
Else
Msgbox(LastException.Message, "Error connecting")

End If

End Sub

Sub EditText1_EnterPressed
ip = EditText1.Text
End Sub
Sub txt_out_EnterPressed
If Astreams.IsInitialized = False Then Return
If txt_out.Text.Length > 0 Then

Dim bc As ByteConverter
Dim pack() As Byte = bc.HexToBytes(txt_out.text)
Astreams.Write(pack)

End If
End Sub
 
Upvote 0
Top