C-Specific Syntax Sugar
This page only covers C SDK A Unique and Convenient Way to Write. They Not a new feature. ..it simply repackages existing entries into shorter macros.
Optional; leaving it out does not affect the standard entry. ——
PnSServiceStart/PnSServiceStop,PnSServiceReadInt16/PnSServiceWriteInt16Use them as usual. Syntax sugar is just another way of writing code.
Header Files ——
#include "pns.h"That's it; there's no need to include it separatelypns_sugar.h.
PNS_TRY / PNS_START_OR_RETURN
Write short macros for checking return codes and starting/stopping. These must be used within the function pns_result_t that returns the value.
| Writing Style | Call |
|---|---|
| Standards API | if (rc != PNS_OK) return rc; |
| Syntax Sugar | PNS_TRY(rc); |
| Standards API | rc = PnSServiceStart(session); if (rc != PNS_OK) return rc; |
| Syntax Sugar | PNS_START_OR_RETURN(session); |
#include "pns.h"
pns_result_t run(void)
{
pns_service_handle_t session = NULL;
pns_result_t rc;
rc = PnSServiceConnectRealtime(&session);
PNS_TRY(rc);
PNS_START_OR_RETURN(session);
/* Write "Q", read "I" */
PnSServiceStop(session);
PnSServiceClose(session);
return PNS_OK;
}
PNS_START_OR_RETURN Failure means return. Do not close the session; call PnSServiceStop / yourself when it ends PnSServiceClose.
When to Use
- Short functions—return an error if there's a mistake: Use
PNS_TRY/PNS_START_OR_RETURN. - Running for a long time: Call
PnSServiceStart/ directlyPnSServiceStop; do not use a macro.
List of Skills
| Grouping | Content |
|---|---|
| Return Code | PNS_TRY(rc): Failure means return this error code |
| Start-Stop | PNS_START_OR_RETURN(session): PnSServiceStart Failure means return. Do not close the session. |