29-404, LPU, Jal-Del G.T. Road - Phagwara, PB, India
09:30 am – 05:30 pm

USB Communication with IoT Devices

Published On: Tuesday, June 18, 2019 198 views


        IoT devices are taking over the world by being always connected and extremely functional to serve as a solution for variety of technical requirements. I’ve been experimenting with them for a couple of months and found them technically intriguing and worth great value. However, if you’re experimenting with an IoT device or wish to communicate with the same using a regular desktop-pc or laptop running any modern OS, there will be a need to communicate with the device after deployment to debug issues or collect sensors data.

This communication with the IoT Device usually happens through the famous and extremely versatile Universal Serial Bus (USB) communication protocol found in modern electronics hardware. In order to use an IoT device to communicate with a pc, one can simply connect the same via regular UART/USB interface cable and use following code (written in C#) to communicate with the device:

Code Snippet:

[DllImport("user32.dll")]    //Required for the Serial Communication

static void Main(string[] args)




private static void Initialize()


SerialPort mySerialPort = new SerialPort("COM3"); //Port Selection

           mySerialPort.BaudRate = 115200; //Set as per setting on Device

           mySerialPort.Parity = Parity.None;

           mySerialPort.StopBits = StopBits.One;

           mySerialPort.DataBits = 8;

           mySerialPort.Handshake = Handshake.None;

           mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);


           Console.WriteLine("Awaiting for the data...");




private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)




              SerialPort sp = (SerialPort)sender;

              string indata = sp.ReadLine();

              Console.WriteLine(indata); //Print received information


            catch (Exception ex)





Post by: Sarabpreet Singh
Contact author: click here

Key Words

data experiment functional layout Hello always WriteLine solution extremely indata Serial modern SerialPort height normal device sender found DataReceivedHandler same mySerialPort string green Console requirement StopBit Parity autospace connect communicate communication regular technical Handshake serve code blue static world variety