Read commands from the browser

… visits
Parent Previous Next

Choose which input slot means which action. Read it in the main loop, check its value, do the work and publish an answer. A receipt from USB only says the command is waiting.


An input slot is a mailbox for one whole number, positive or negative. It holds the number until your code reads it. Choose Browser input slots = 1 for one command destination, or up to 16 for several. Input and output slots are separate: input 0 could be the wanted temperature, while output 0 is the measured temperature.


The write request has this exact form:

POST /write?index=0&value=123

X-USB-Write: 1


(empty request body)

index is zero-based and must refer to an enabled input slot. value is a decimal integer in the signed 32-bit range, −2147483648 through 2147483647. Use the shown query order, an empty body and the required header. A normal HTML form with a body, or a GET request to this address, is not the command protocol.


When the mailbox receives the number, the device returns HTTP 202 Accepted, for example:

{"accepted":true,"index":0,"sequence":17}

Accepted is not finished. A 202 receipt says the number reached the mailbox. Your program still has to read it and do the work. Another write to that same unread slot gets HTTP 409 Conflict and is not added to a queue. Other slots can hold their own unread values. Invalid requests get HTTP 400. Writes are disabled when input capacity is 0.

Read a command in the PIC

In the main loop, call USB_NetReadValue(pIndex As Byte) for each slot your application uses. On USB_OK, copy USB_NetRxValue (SDword) and USB_NetRxSequence (Word) to your own variables immediately. The call consumes that slot's pending input. USB_NO_DATA is normal when nothing is waiting; an out-of-range index gives USB_BAD_LENGTH. With input disabled, the call gives USB_NOT_SUPPORTED.


USB_NetRxPending is a Word bitmask: bit 0 represents unread slot 0, and so on. It can help diagnostics, but normally just call USB_NetReadValue(); do not clear pending bits yourself. Check USB_Result before using the returned values, since a call without data clears the returned value and sequence.

Match the completed answer

For an answer, publish a result and the input's sequence number as ordinary named outputs. The browser can then wait for a matching answer. Receipt numbers run from 1 to 65535, then wrap to 1. A firmware restart or USB_Init() resets the counter. USB/link resets discard unread commands but preserve the counter, so a 202 receipt does not guarantee that a command survives a disconnect before the application reads it. Use sequences to match the current exchange, not as permanent transaction identifiers. Avoid concurrent browser tabs or clients competing for this single-connection interface. Never automatically repeat a timed-out write: the PIC may already have accepted it.

See also

Created with the Personal Edition of HelpNDoc: Effortlessly bring your documentation online with HelpNDoc