DIM sp as Serial
....
sp.Initialize("")
Try
sp.Open("COM14")
Catch
Log(LastException)
End Try
...
Sub txtInput_Action
astream.Write(txtInput.Text.GetBytes("UTF8"))
txtInput.SelectAll
txtInput.RequestFocus
LogMessage("Gesendet ", txtInput.Text)
End Sub
txtInput.Text = ""
txtInput.Text = "FF 04 00"
txtInput_Action
txtInput.Text = ""
When txtInput.Text = "FF, 04, 01"You are not sending the correct values.
Or:B4X:astream.Write(Array As Byte(0xFF, 0x04, 0))
B4X:astream.Write(bc.HexToBytes("FF0400")) 'bc - ByteConverter
When txtInput.Text = "FF, 04, 01"
and i use
astream.Write(Array As Byte(txtInput))
an error occurs.
The hex's are stored in txtInput.Text
Stupid me....of course it breaks:
Will try the bc-story...
sp.Initialize("")
Try
sp.Open("COM14")
Catch
Log(LastException)
End Try
Sub txtInput_Action
astream.Write(bc.HexToBytes(txtInput.text))
txtInput.SelectAll
txtInput.RequestFocus
LogMessage("Gesendet ", txtInput.Text)
End Sub
txtInput.Text = ("FF0100")
Sub Process_Globals
Private myserial As Serial
Private astream As AsyncStreams
Private MyPort As String
Private BaudRate As Int
End Sub
Sub Activity_Create(FirstTime As Boolean)
myserial.Initialize("")
End sub
Sub btn_Connect_MouseClicked (EventData As MouseEvent)
Log("Com Port: " & MyPort)
Log("Baud: " & BaudRate)
If btn_Connect.Text="Connect" Then
btn_Connect.Text="Disconnect"
myserial.Open(MyPort)
myserial.SetParams(BaudRate,8,1,0) 'Baud normally 9600
astream.Initialize(myserial.GetInputStream,myserial.GetOutputStream,"astream")
Else
btn_Connect.Text="Connect"
astream.Close
myserial.Close
End If
End Sub
'Then I use the following to send data,MyText is a string containing anything.
astream.Write(MyText.GetBytes("UTF8"))
Looks good ;-), will give it a try.I am not an expert but maybe your COM port is not open?
Here is some code which I tried and worked:
B4X:Sub Process_Globals Private myserial As Serial Private astream As AsyncStreams Private MyPort As String Private BaudRate As Int End Sub Sub Activity_Create(FirstTime As Boolean) myserial.Initialize("") End sub Sub btn_Connect_MouseClicked (EventData As MouseEvent) Log("Com Port: " & MyPort) Log("Baud: " & BaudRate) If btn_Connect.Text="Connect" Then btn_Connect.Text="Disconnect" myserial.Open(MyPort) myserial.SetParams(BaudRate,8,1,0) 'Baud normally 9600 astream.Initialize(myserial.GetInputStream,myserial.GetOutputStream,"astream") Else btn_Connect.Text="Connect" astream.Close myserial.Close End If End Sub 'Then I use the following to send data,MyText is a string containing anything. astream.Write(MyText.GetBytes("UTF8"))
Maybe this helps.
astream.Write(bc.HexToBytes(txtInput.text))
Sub Send_Action
txtInput.RequestFocus
txtInput.SelectAll
astream.Initialize(sp.GetInputStream,sp.GetOutputStream,"astream")
astream.Write(txtInput.Text.GetBytes("UTF8"))
'astream.Write(bc.HexToBytes(txtInput.text))
LogMessage("Gesendet ", txtInput.Text)
astream.Close
End Sub
It is not a question of preferences. It depends on what the peripheral device expects.Erel preferred this one:
sp.Initialize("")
Try
sp.Open("COM14")
sp.SetParams (9600, 8, 1, 0)
'sp.close
Catch
Log(LastException)
End Try
Sub txtInput_Action
txtInput.RequestFocus
txtInput.SelectAll
astream.Initialize(sp.GetInputStream,sp.GetOutputStream,"astream")
'astream.Write(txtInput.Text.GetBytes("UTF8"))
astream.Write(bc.HexToBytes(txtInput.text))
LogMessage("Gesendet ", txtInput.Text)
astream.Close
End Sub