Programming Reference:NatusADC
NatusADC module has 3 main classes - NatusPackageInterface, NatusDataServer and NatusClient. The overview of the NatusADC working is shown in the image below.

NatusPackageInterface
This class handles the communication between the server/device and the client. It should be included with both - NatusDataServer and NatusClient Class. The class performs the following functions -constructing TCP and UDP packets, calculating the CRC of a packet and checking the CRC on the receiving side, sending the packets over TCP/UDP port, receiving packets over TCP/UDP Port, reconstructing the data from the packets and listening for connections on device/server-side. Note - The file limits the maximum number of channels for a device (stored in MAX_NUM_CHANNELS in NatusPackageInterface.h) to 100.
The format of the packets constructed along with each field size is as follows:

Commands
The commands through which the NatusDataServer and NatusClient communicate are as follows:
- CMD_Ping
- To check if the connection is successful and data can be sent and received.
- Command Number: 0x01
- Parameters/Payload: 0xff
- Return Value: returns payload
- CMD_GetInformation
- To send the device information such as the number of channels and sampling rate to the client.
- Command Number: 0x02
- Parameters/Payload: NULL
- Return Value: struct NatusDeviceInformation which contains the number of channels, sampling rate, and device identifier.
- CMD_GetChannelNames
- To send the channel names as set by the device to the client. If no channel names are set, NULL is sent.
- Command Number: 0x03
- Parameters/Payload: NULL
- Return Value: struct NatusChannelInformation which contains all the channels and stream names.
- CMD_StartStream
- To start sending channel data over UDP port.
- Command Number: 0x10
- Parameters/Payload: NULL
- Return Value: NULL
- CMD_StopStream
- To stop sending channel data
- Command Number: 0x1F
- Parameters/Payload: NULL
- Return Value: NULL
- CMD_SelectChannels
- To send data only for the channels numbers mentioned in the payload
- Command Number: 0x20
- Parameters/Payload: a list of channel numbers separated by space and stored in a string
- Return Value: NULL
- CMD_SetBlockSize
- To set the number of blocks to send per packet.
- Command Number: 0x21
- Parameters/Payload: number of blocks to send per packet stored in uint32_t datatype.
- Return Value: NULL
- CMD_GetDecimationFactor
- To get the current decimation factor of the device/server.
- Command Number: 0x22
- Parameters/Payload: decimation factor stored in uint32_t datatype.
- Return Value: NULL
- CMD_SetDecimationFactor
- To set the decimation factor of the device/server.
- Command Number: 0x23
- Parameters/Payload: NULL
- Return Value: decimation factor stored in uint32_t datatype but casted into uint8_t datatype for transmission over TCP.
- CMD_ERR
- The command is sent if there is any type of error.
- Command Number: 0xFF
Functions and Variables
The public and protected functions and variables defined in this class are as follows:
- void Poll () -
- void send_data (uint8_t cmd, uint8_t* payload, uint32_t len_payload, bool isError=false, PackageType t = Control) -
- virtual void data_received (CmdStruct * payload) = 0 -
- void printMessage (std::string) -
- CmdStruct* getNextPackage () -
- CmdStruct* getNextUDPPackage () -
- void delete_cmdstruct (CmdStruct* ) -
- void initWSA () -
- SOCKET connection -
- SOCKET udpSocket -
- sockaddr_in _udpReceiver -
NatusDataServer
This class handles the server-side processing. It performs the following functions - starts-up the TCP and UDP server, check for new connections, gracefully disconnects from the client, registers various callbacks on client connection and deregister the callbacks when the client disconnects and responds to the commands (described in the NatusPackageInterface section) from the client via TCP protocol.
Functions and Variables
The public and protected functions and variables defined in this class are as follows:
- void Startup (std::uint16_t port) -
- void CheckForConnection () -
- void RegisterDataStreamCallback (NatusDataStreamRequest, void*) -
- void RegisterInformationCallback (NatusInformationRequest, void*) -
- void RegisterChannelInformationCallback (NatusChannelInfoRequest, void*) -
- void RegisterErrorCallback (NatusResponse, void*) -
- void UnregisterCallbacks () -
- void SendStreamData (float* data, uint32_t numdata) -
- void Disconnect () -
- bool HasClient () -
- int _channelsToStream[MAX_NUM_CHANNELS] -
- int _numberOfChannelsToStream -
NatusClient
This class handles the client-side processing. It connects with the server/device, gets data from the operator module of the NatusADC and sends it to the server/device. It uses various commands as described in the NatusDataServer to set different parameters. Another important function of this class 'interpolateMissingBlocks' linearly interpolates the missing UDP packets which contain the channel data values. Eg.
Consider the scenario where 1 block is sent per packet and decimation factor is 1. Suppose we received packet 1 and 3, packet 2 was lost. Data for packet 2 is interpolated as follows:
Interpolation formula
Interpolated Value = ((Current Packet value – Previous Packet value) / (Current Packet Number – Previous Packet Number)) * (Interpolated Packet Number – Previous Packet Number) + Previous Packet Value

Functions and Variables
The public and protected functions and variables defined in this class are as follows:
- NatusDeviceInformation GetInformation () -
- void Connect (std::string server_name, int port) -
- bool TryConnect (std::string server_name, int port) -
- void Disconnect () -
- void StartStream () -
- void StopStream () -
- uint8_t Ping (uint8_t ping) -
- NatusSampleBlock* ReadStream () -
- NatusSampleBlock* create_natus_sampleblock (uint8_t* payload) -
- void DeleteSampleBlock (NatusSampleBlock*) -
- void SetSampleBlockSize (uint32_t blocksize) -
- void SetDecimationFactor (uint32_t decimationFactor) -
- NatusChannelInformation GetChannelInfo () -
- int GetDecimationFactor () -
- int SelectChannelsToStream (std::string) -
- int _channelsToStream[MAX_NUM_CHANNELS] -
- NatusChannelInformation channelInfo -
See also
Contributions:NatusADC, User Reference:DataIOFilter, Programming Reference:GenericADC Class