Device communication basic for beginners

Agenda

  • What is device communication?
  • Common communication channel
  • Resource Management for device communication
  • Packet analysis
  • Third party DLL support for devices
  • Thread safe architecture
  • Best practices of device communication
  • Few sniffing tools

What is device communication?

  • The communication between the process created by our code and read the device through a communication channel.
  • Commonly used communication channels:-
    • Serial port (Comport communication)
    • Socket
    • USB
    • Bluetooth
    • Wi-fi
  • All devices which are directly connected to PC requires Device Driver to work well with Operation System.
  • Device Drivers helps in communicating with individual devices.

Common Communication Channel

  • Serial Port Communication
    • Serial ports, also called communication (COM) ports, are bi-directional. Bi-directional communication allows each device to receive data as well as transmit it.
    • Here we deal with RS232 port(Mostly available with all PC,)
    • Nowadays PCs are not coming with inbuilt serial ports.
    • Unique identification is port number like com1, com2 etc.
  • Socket Communication
    • Local socket address: Local IP address and port number
    • Remote socket address: Only for established TCP sockets. This is necessary since a TCP server may serve several clients concurrently. The server creates one socket for each client, and these sockets share the same local socket address from the point of view of the TCP server.
    • Protocol: A transport protocol (e.g., TCP, UDP, raw IP, or others). TCP port 53 and UDP port 53 are consequently different, distinct sockets.
    • To identify it uniquely we need to combine it by IP address and Port.
  • USB
    • Defines the cables, connectors and communication protocols used in a bus for connection, communication, and power supply between computers and electronic devices.
    • Some times device with USB connection creates a separate serial port for communication.
  • Bluetooth
    • It is a wireless technology standard for exchanging data over short distances from fixed and mobile devices and building personal area networks (PANs).
    • It can connect several devices.
  • Wi-fi
    • It is a local area wireless technology that allows an electronic device to exchange data or connect to the internet using 2.4 GHz UHF(Ultra high frequency) and 5 GHz SHF(Super high frequency) radio waves.

Resource Management

  • What are the resources, which we are going to manage here?
  • Examples:-
    • For Serial Port, resource is Comport.
    • For Socket, a resource is Port number.
  • Poor resource management can cause performance and system crash issues.
  • Point to keep in mind before utilizing these resources:-
    • The quantity of the resource: - like how many Comport is present in your PC.
    • Is there any other process that can use the similar resources?
    • Is that resource detachable?
    • Is it possible to identify it at run-time?
    • Identification of safe resource.

Packet Analysis

  • Common packet structure:- STX + DATA + ETX + LRC
  • Packet structure may vary based on the protocol defined for that device.
  • Packet analysis helps in resolving any issue related to Device communication.
  • We can verify the protocol defined for a particular communication.
  • Data can come in multiple chunks; we need to extract the correct packet.
  • How do we do that?
    • We can make use of any of the packet sniffing tools. Like: Device monitoring studio for monitoring data sent and receive through Comport.
    • Based on STX, ETX and LRC we can identify the correct packet.
  • What are the protocols?
    • Set of rules defined by our customer for a particular device or some Universal standard.
    • Customer Protocols:- Mostly defined by our customers or device manufacturers
    • Standard Protocols:- Defined by a governing body like IEEE

Third party DLL support

  • Sometimes device owner itself provides a Dynamically linked library (DLL) for communicating with their devices.
  • Points to keep in mind while working with third-party DLLs:-
    • These DLLs may or may not provide support for communicating with multiple devices at a time.
    • Check that DLL’s are multi-thread supported or not. If not we have to do some synchronization from our side.
    • We may need to have multiple instances of these DLLs to communicate with multiple devices.
    • No need to worry about packet extraction.
    • Are they supported for both 32-bit and 64-bit kind of architectures?
    • Are they managed or unmanaged?

Thread Safe Architecture

  • Most of the device communication architectures are Event Driven.
  • The event-driven approach will improve the performance of the system.
  • When dealing with multiple devices at the same time, we might need to design our architecture thread safe.
  • We must design our architecture in a thread-safe manner while dealing with multiple threads of the application.
  • It will also increase the testability of the system.
  • Need to take care of synchronization.
  • One example of a thread-safe architecture for device communication based on my experience:- 
    Thread Safe Architecture

Best Practices of Device Communication

  • Manage the resources well. If not in use release it. This will reduce system crash issues due to poor resource management.
  • Be very care full about the Numbers like Hexadecimal or decimal. Mostly we use the hexadecimal number for all device related stuff. Like defining packet or command structure.
  • Use thread safe and event-driven architecture for handling multiple devices at the same time. (In C# we can make use of AutoResetEvent or ManualResetEvent)
  • To improve performance where the user needs to transfer lots of data to devices, use caching techniques. Make sure to cache at least 10 next commands. Use some maximum device count to be in action at the same time. Otherwise, CPU utilization may go high.
  • While extracting data received from device be very careful. Make sure you are not losing any data.
  • With the serial port, communication makes sure you are using the correct baud rate.
  • Make sure to use the proper delay between two command executions.
  • Before starting the implementing, read the Protocol document carefully.
  • Try to understand all types of possible error cases for different devices and plan the recovery mechanisms.
  • Don’t think that all similar devices will behave in the same way or similar. There are chances that the command acceptance will be different for different devices.
  • If possible implement health monitoring and recovering mechanisms in the case of the poor health of the system.

Few Sniffing Tools

  • SocketSniff:- SocketSniff allows you to watch the Windows Sockets (Winsock) activity of the selected process. This utility is released as freeware. Download Link: http://littletools.net/socketsniffer.aspx
  • Wireshark:- Wireshark is a network protocol analyzer. It lets you capture and interactively browse the traffic running on a computer network. It has a rich and powerful feature set and is the world's most popular tool of its kind. It runs on most computing platforms including Windows, OS X, Linux, and UNIX. Network professionals, security experts, developers, and educators around the world use it regularly. It is freely available as open source and is released under the GNU General Public License version 2.  Download Link: http://www.tomsguide.com/us/download/Ethereal-Wireshark,0301-20912.html
  • Device Monitoring Studio:- This tool can be used to monitor any kind of data flow from PC(request and response both). It can monitor serial port, USB, Socket etc. Download Link: http://www.hhdsoftware.com/device-monitoring-studio
Thanks for Reading, Please subscribe to my blog if you like to receive the weekly update on my blog.  Drop your comment or queries below if any and share this if you like it.

Comments