Here is what I want to do:
1. set up a web server waiting to http request like : http://localhost:8888/getdata
2. in handler getdata, use jSerial to get data from COM Port
3. response http request with the data
When I run http://localhost:8888/, the root page returns ok. When I create a UI to "getdata", I could get data from com port.
But if I run http://localhost:8888/getdata, trying to getdata then return a web page, Server does not response.
When debug with F8, after page request from browser, I could see getdata returned data, but the web page does not send back to browser. Any hints?
Sample code as below
1. set up a web server waiting to http request like : http://localhost:8888/getdata
2. in handler getdata, use jSerial to get data from COM Port
3. response http request with the data
When I run http://localhost:8888/, the root page returns ok. When I create a UI to "getdata", I could get data from com port.
But if I run http://localhost:8888/getdata, trying to getdata then return a web page, Server does not response.
When debug with F8, after page request from browser, I could see getdata returned data, but the web page does not send back to browser. Any hints?
Sample code as below
B4X:
Sub AppStart (Args() As String)
srvr.Initialize("srvr")
srvr.Port = 8888
srvr.StaticFilesFolder = File.Combine(File.DirApp, "www")
srvr.LogsFileFolder = File.Combine(File.DirApp, "logs")
srvr.AddHandler("/getdata","getdata", False)
srvr.Start
Log("Server started")
StartMessageLoop
End Sub
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
mreq = req
mresp = resp
openport
cmdflag = False
getdata
Do While cmdflag = False
Sleep(500)
Log(cmdflag)
Loop
closeport
Do While cmdflag = False
Loop
resp.SetHeader("Access-Control-Allow-Origin", "*") ' allow cross domain call
resp.Write(cmdrslt)
End Sub
B4X:
Sub openport()
sp.Initialize("")
sp.Open("COM19")
sp.SetParams(sp.BAUDRATE_2400, sp.DATABITS_7, sp.STOPBITS_1, sp.PARITY_EVEN )
astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
End Sub
Sub closeport()
sp.Close
End Sub
Sub astream_NewData (Buffer() As Byte)
cmdrslt = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")
cmdflag = True
End Sub
Sub getdata()
Dim buffer() As Byte
buffer = bc.HexToBytes("020103") ' byte command to get data
astream.Write(buffer)
End Sub