Author Topic: Serial Communication for IoT  (Read 1219 times)

Biterider

  • Moderator
  • Member
  • *****
  • Posts: 868
  • ObjAsm Developer
    • ObjAsm
Serial Communication for IoT
« on: October 19, 2020, 05:04:24 AM »
Hi
ObjAsm has been a little quiet lately because I've been working on some labor-intensive IoT projects.
The good news, however, is that I have new code to share  :biggrin:

Most of the time, when you want to communicate with an external device like an ESP32 or Arduino, you need a way to send and receive data. In the IoT world, you can usually choose between WiFi, LORA and some type of serial communication. The last one is either implemented with dedicated SPI hardware or emulated in software.

The simplest is serial communication, which can be used to communicate with the PC via the existing USB ports.
One of the first tasks for such an application is to identify the devices connected to the serial interfaces. I've looked at sample code on this forum and some articles on "Code Project" like https://www.codeproject.com/Articles/293273/Ready-to-use-serial-port-enumeration-list-box to write some asm code. I used this to select the COM port in a dialog box (see figure below).

This code is based on Windows SetupApi, which is included in all versions above Win95 and NT.

This implementation is in the ComPortNames.inc file, which contains two procedures: GetComPortNames and FreeComPortNames. The first creates an array of COMPORT_INFO structures, while the last destroys it. The structure definition looks like this:

Code: [Select]
COMPORT_INFO struct
  cName         CHR           COMPORT_NAME_LEN DUP(?)
  pFriendlyName PSTRING       ?
COMPORT_INFO ends

I hope this can be helpful for other IoT enthusiasts  :eusa_dance:

Regards, Biterider

Biterider

  • Moderator
  • Member
  • *****
  • Posts: 868
  • ObjAsm Developer
    • ObjAsm
Re: Serial Communication for IoT
« Reply #1 on: October 24, 2020, 07:56:46 PM »
Hi
I want to share the code from the previous post.

The test sketch is runs on an "Arduino Uno" and its SPI is configured to work with 9600 baud, 8 data bits, 1 stop bit and without a parity check.
It responds to a simple received string that contains "PC is here". If this string is recognized, "Arduino is here too" will be sent back. As simple as that.  :biggrin:

Code: [Select]
void setup() {
  Serial.begin(9600, SERIAL_8N1);                       // Setup serial COM Port
  while (!Serial) {}                                    // Wait for serial port to connect
  //Serial.println("Arduino ready");
}

void loop() {
  String InString;

  if (Serial.available() > 0) {                         // Test if serial data arrived
    InString = Serial.readString();
    if (InString == "PC is here") {
      Serial.println("Arduino is here too...");         // Respond on Serial
      Serial.flush();                                   // Send the message
    }
  }
}

The PC application acts as a controller. The most interesting part here is contained in the DialogComPortSelection.inc file and in the ArduinoApp.OnCommand method in the ArduinoCom_Main.inc file. The rest is pretty standard.
The application in the attachment is compiled for 32 bit and Unicode string targets.

To use this demo application you have to select the COM port, connect to open the port and transmit the string. To read the response, you need to receive the sent back ASCII stream. Finally you can disconnect the port.
These commands are available in the application menu or via shortcuts.

Regards, Biterider
« Last Edit: October 29, 2020, 03:28:17 PM by Biterider »