E22 SX1268 LoRa module transmission test (fixed-point mode) sending and receiving serial port messages C language code under windows and linux Environment


Recently, due to the needs of the project, the LoRa transparent transmission module has been tested and learned.

The module tested this time is the LoRa module of Chengdu ebyte company, with the model of SX1268. The module adopts serial port transparent transmission mode.

This blog mainly records the fixed-point transmission mode of the module. The transparent transmission mode is relatively simple. You can refer to most serial communication codes.

What is serial transmission?

The so-called serial port transparent transmission is regardless of the transmitted content and data protocol form. Just take the content to be transmitted as a set of binary data and transmit it to the receiving end perfectly. Do not process the data to be transmitted.

mcu has serial port, which is also the simplest and most common mcu communication mode. In fact, serial port transparent transmission uses other communication methods, such as Ethernet, Bluetooth, zigbee, gprs and so on, and serial port data is used as the data area in these protocol frames. It is essentially other communication methods. However, for embedded developers, it is actually programming the serial port, and the serial port is used for data receiving and sending. The data transmission is actually through other ways. The transparent transmission module (or chip) is responsible for sending the serial port data in other ways (such as Bluetooth) and converting the received data into serial port data at the same time. In this way, these modules are transparent to embedded mcu (embedded programmer). The use of transparent module can simplify programming (without implementing complex protocol stack), increase flexibility (modularization, changing transparent module can change the communication mode), and make the old equipment have new communication mode (the previous equipment may not have source code, and the current advanced network can be used only by adding transparent module).

Therefore, this paper has a certain reference value for the modules that also communicate through serial port.

Implementation process

Module configuration

Four LoRa modules are used in this experiment. Use the configuration software provided by the official. The official configuration software can be obtained through the following channels:
Log in to ebyte official website / data download page: http://www.ebyte.com/data-download.html?page=3&id=37&cid=31#load
Select: software tools / parameter setting software in the web page, find the corresponding version of E22 series and download it

Configure the four modules respectively. The parameters to be modified include transmission mode, module address and module channel. As shown in the figure below:

The configuration parameters of the four modules are set according to the following table:

Module debugging

In order to test whether the function of the module is normal, use the serial port debugging assistant for debugging before use.
The serial debugging assistant can also be downloaded from the resource list of the website above. Most versions of serial debugging assistant are applicable.
Connect all the four LoRa modules to the notebook computer, open their serial ports and carry out transmission test. The following results show that the module can work normally according to the configured parameters.

In the fixed-point transmission function, unlike the transparent transmission mode, the data will be broadcast to all terminals under the channel, but will only be transmitted to the pre-defined target address. The relationship between the sender and receiver of the four LoRa modules under this test is shown in the figure below:

Code writing

This blog gives the communication demo of windows and linux system, which is relatively simple for students who have just got the module to expand their learning.

Effect of fixed-point transmission mode:
The module with address 2 sends content (channel number 5) to the module with address 3. Relevant parameters can be modified flexibly according to their own requirements.
windows version code (VS2019)


using namespace std;

int main()
    HANDLE hcom;
    hcom = CreateFile(L"COM4", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING //The COM4 port of the computer is connected
    if (hcom == INVALID_HANDLE_VALUE)

        fprintf(stderr, "Failed to open serial port!\n");
    SetupComm(hcom, 1024, 1024);
    DCB dcb;
    GetCommState(hcom, &dcb);
    dcb.BaudRate = 9600;
    dcb.ByteSize = 8;
    dcb.Parity = 0;
    dcb.StopBits = 1;
    SetCommState(hcom, &dcb);
    char v2vbuff[] = { 0x00, 0x03, 0x05, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };  //Send hexadecimal content AA BB CC DD EE FF to the target with address 3 and channel 5
    DWORD dwWrittenLen = 0;
    int k = 0;
    for (;;) {
        if (!WriteFile(hcom, v2vbuff, 9, &dwWrittenLen, NULL))
            fprintf(stderr, "Failed to send data!\n");


        printf("Sending data to serial port succeeded! The first%d second\n", k);
        Sleep(1000);  //Send once in 1s

    return 0;

linux version code (based on raspberry pie)

#include <stdio.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

int main()
    int running = 1;
    void sig_handle(int sig)
        if(sig == SIGINT)   
        running = 0;
    signal(SIGINT, sig_handle);    
    int fd;
    if(wiringPiSetup() < 0)
        printf("wiringPi setup failed.\n");
        return 1;

    int baudrate = 9600;
    if((fd = serialOpen("/dev/ttyUSB0",baudrate)) < 0)
        printf("serial open failed.\n");
        return 1;

    serialPrintf(fd, "Connect with LoRa success"); 
        char buff[] = {0,3,5,170,187,204,221,238,255};	//Corresponding to hex 0x00, 0x03, 0x05, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
        printf("send message success\n");

    printf("close serial.\n");

    return 0;

Connect the LoRa module with address 3 on the computer, use the serial port debugging assistant to open the corresponding serial port and run the corresponding code. The following results can be displayed:

So far, the fixed-point transmission mode test of LoRa module has been completed, and more subsequent functions need to be developed.

Reference link


Tags: C Linux Embedded system windows10 lora

Posted by Solemn on Sat, 30 Apr 2022 02:59:20 +0300