Electronics > Microcontrollers

ch32v307 Modbus + Web UI firmware

<< < (12/12)

S. Petrukhin:
If it's not difficult, please tell me how it works?
Does ch32v307 issue a Modbus exchange web page on an http request?

tellurium:

--- Quote from: S. Petrukhin on February 03, 2024, 07:08:08 pm ---If it's not difficult, please tell me how it works?
Does ch32v307 issue a Modbus exchange web page on an http request?

--- End quote ---

Correct. This is how it works:

1. A button click on the Web UI page triggers a REST API call, /api/modbus/exec:
https://github.com/cesanta/mongoose/blob/c8d45c9ff1b3042919d19ba823415d35ce1b0fbc/examples/modbus-dashboard/net.c#L262-L263

2. Which in turn creates a modbus TCP connection to the target device:
https://github.com/cesanta/mongoose/blob/c8d45c9ff1b3042919d19ba823415d35ce1b0fbc/examples/modbus-dashboard/net.c#L187-L188

The original HTTP REST request waits, but starts a software timer: https://github.com/cesanta/mongoose/blob/c8d45c9ff1b3042919d19ba823415d35ce1b0fbc/examples/modbus-dashboard/net.c#L192-L193

3. The modbus connection (https://github.com/cesanta/mongoose/blob/c8d45c9ff1b3042919d19ba823415d35ce1b0fbc/examples/modbus-dashboard/net.c#L144C40-L145) sends a request to a target device (https://github.com/cesanta/mongoose/blob/c8d45c9ff1b3042919d19ba823415d35ce1b0fbc/examples/modbus-dashboard/net.c#L147-L181)
and when it receives a response (https://github.com/cesanta/mongoose/blob/c8d45c9ff1b3042919d19ba823415d35ce1b0fbc/examples/modbus-dashboard/net.c#L104-L107),
it finds an original REST request (https://github.com/cesanta/mongoose/blob/c8d45c9ff1b3042919d19ba823415d35ce1b0fbc/examples/modbus-dashboard/net.c#L109-L111) and notifies it about the modbus response. An original REST request then sends a reponse to the Web UI (https://github.com/cesanta/mongoose/blob/c8d45c9ff1b3042919d19ba823415d35ce1b0fbc/examples/modbus-dashboard/net.c#L285-L292)

4. In case if modbus request times out, the original REST connection returns failure (https://github.com/cesanta/mongoose/blob/c8d45c9ff1b3042919d19ba823415d35ce1b0fbc/examples/modbus-dashboard/net.c#L281-L283)

S. Petrukhin:

--- Quote from: tellurium on February 10, 2024, 12:22:13 am ---Correct. This is how it works:

--- End quote ---

Thanks for the detailed answer!
I have never used REST technology.  :)

That is, do you have your own application that exchanges with MK?

I'm asking because my devices have a WEB interface targeted for using the browser.
I did not find in JS the possibility of a direct binary request for Modbus exchange and made Modbus/HTTP.
The browser sends get requests, usint fetch request.
For example, /read?bank=4&startreg=10&count=2 and receives responses in the body in the form of values=34,56 or error=4

tellurium:

--- Quote from: S. Petrukhin on February 11, 2024, 04:38:18 pm ---I did not find in JS the possibility of a direct binary request for Modbus exchange and made Modbus/HTTP.
The browser sends get requests, usint fetch request.
For example, /read?bank=4&startreg=10&count=2 and receives responses in the body in the form of values=34,56 or error=4

--- End quote ---

JS inside the browser is limited only to HTTP and Websocket protocols. It cannot create arbitrary TCP/UDP connections.
Therefore, the Web UI talks (via HTTP REST) to a device, and a device in turn creates arbitrary connections. In our case, it is Modbus.

REST means "representational state transfer", but basically, if you think that "HTTP", "REST", or "Ajax" is the same - you won't be mistaken. It's the same stuff. That is, making HTTP requests to get some data.

S. Petrukhin:
Thanks! I had a doubt that I was behind the times and missed the opportunities of JS.  :)

Navigation

[0] Message Index

[*] Previous page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod