B4J Question astream.write - comm error

fotosettore

Member
Licensed User
Longtime User
hi all !
- scenario -
i must communicate with serial port of my solar inverter. i made a VB6 little program and all works fine.
comm parameters are : 2400,n,8,1
command to send is : QMOD (to receive the status of inverter : L = line B = battery)

the (semplified) working code on VB6 is :
B4X:
Text = "QMOD" & Chr(73) & Chr(193) & Chr(13)  '(command + CRC + cr)
MSComm2.Output = Text
Sleep (200)
Rx = MSComm2.Input
Rx returns L o B as the case

i made the same in BAJ but it does not work
B4X:
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private myserial As Serial
    Private astream As AsyncStreams
    Private uart_list As List
    Private sndStr As String
    Private rcvStr As String
    Private text As String = "QMOD" & Chr(73) & Chr(193) & Chr(13)
   
   
End Sub

Sub AppStart (Args() As String)
    myserial.Initialize("")
    uart_list = myserial.ListPorts
    myserial.Open("COM7")
    myserial.SetParams(2400,8,1,0)
    astream.Initialize(myserial.GetInputStream,myserial.GetOutputStream,"astream")
    Log("Initialized")
    Log("Text --> " & text)
    astream.Write(text.GetBytes("UTF8"))
    Wait(0.2)
   
    StartMessageLoop
   
End Sub

Sub astream_NewData (Buffer() As Byte)
    rcvStr = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    sndStr = "I have received " & rcvStr & Chr(13) & Chr(10)
    Log(sndStr)
End Sub

Sub Wait(Seconds As Int)
   Dim Ti As Long
   Ti = DateTime.Now + (Seconds * 1000)
   Do While DateTime.Now < Ti
   Loop
End Sub


the inverter answers "NAK", that means "not acknowdledge" command.
B4X:
Program started.
Initialized
Text --> QMODIÁ
I have received NAK


this error is the same in VB6 when command is not recognized from inverter due comm error or wrong command sent

i tried to change time of waiting from send to receive, but nothing happened

any idea to solve it ?
many thanks
peppe
 
Top