C# Windows forms app - not receiving serial data from nucleo32 L432 with STM32duino

Viewed 92

I'm working on windows forms app with communication with microcontroler. Previously my project was based on arduino nano and it worked flawlessly. Right now I'm switching to STM32 with arduino core (STM32duino) and I have got problem with receiving data through serial in windows forms app. Right now I'm testing that with nucelo32 L432 board while being connected to Serial(0) port of that board. Let's start with my arduino code:

extern "C" void SystemClock_Config(void) {
  RCC_OscInitTypeDef RCC_OscInitStruct = {};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {};

  /* Initializes the RCC Oscillators according to the specified parameters
     in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 40;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
    Error_Handler();
  }
  /* Initializes the CPU, AHB and APB buses clocks */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
                                RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1;
  PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_MSI;
  PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
  PeriphClkInit.PLLSAI1.PLLSAI1N = 24;
  PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV7;
  PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
  PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
  PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_48M2CLK;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
    Error_Handler();
  }
  /* Configure the main internal regulator output voltage */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) {
    Error_Handler();
  }
}


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    Serial.println("A");
    digitalWrite(LED_BUILTIN, HIGH);
  }
}

As you can see first it initialize clocks so that serial is visible as Virtual com port in PC then initialize serial with baud rate of 9600, and LED_BUILTIN pin. Then when it will receive any serial data it should output to serial letter "H" and turn LED ON.

I upload that to board and test with arduino serial monitor and PuTTy - works as expected - as soon as it received anything through serial it outputs "h" and onboard LED turns ON.

Now let's get to my windows forms test app. It's just a simple form with comboBox to select com port that I'd like to connect to, refresh button to refresh com ports list, connect button that will initialize connection, send button that will send "H" through serial and label to show "Received something" when it will receive anything through serial: Form layout

And code for that form:

public partial class Form1 : Form {

    string IncomingData;
    public Form1()
    {
        InitializeComponent();
        serialPort1.DataReceived += serialPort1_DataReceived;

        foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
        {

            comboBox1.Items.Add(s);
        }
    }
    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        IncomingData = serialPort1.ReadLine();
        this.Invoke(new EventHandler(ReceivedDataHandle));temperatury

    }

    private void ReceivedDataHandle(object sender, EventArgs e)
    {

        label1.Text = "Received something";

        
    }

    private void button1_Click(object sender, EventArgs e)
    { //connect button
        try
        {

            serialPort1.BaudRate = 9600;
            serialPort1.PortName = comboBox1.Text;
            serialPort1.NewLine = "\r\n";
            serialPort1.Open();
            Thread.Sleep(1000);
            MessageBox.Show("Connected");
        }
        catch (Exception)
        {
            MessageBox.Show("Error - connection problem");
        }
    }

    private void button2_Click(object sender, EventArgs e)
    { //Send button
        try
        {
            serialPort1.WriteLine("H");
        }
        catch (Exception)
        {
            MessageBox.Show("Error - connection problem");
        }
    }

    private void button3_Click(object sender, EventArgs e)
    { //refresh button - get com ports
        comboBox1.Items.Clear();
        foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
        {

            comboBox1.Items.Add(s);
        }
    }
}

I have tested that with Arduino NANO and it works as it should - after arduino receive "H" through serial it sends through serial "A", turns onboard LED on and label1 text changes to "Received something", but with nucleo board something weird happens... After pressing send button, LED on nucleo board turns on, so it has received "H", but label1 forms app doesn't receive anything back - label1 doesn't change text. Also I have put breakpoint in line:

IncomingData = serialPort1.ReadLine();

Then with arduino app stops and goes into "debug checking" and with nucleo it doesnt, so I assume forms doesn't receive any data. Then... what I can try? How to solve that?

@@UPDATE: Problem probably solved. I had to add serialPort1.DTREnabled = true and serialPort1.RTSEnabled = true. I forgot that stm32duino doesn't handshake... Will do more testing and raport back if there will be any problems.

1 Answers

As suggested - here is solution: I had to add serialPort1.DtrEnabled = true and serialPort1.RtsEnabled = true, because STM32duino doesn't do handshake. Now it is working as it should.

Related