0

I'm creating a software in C # that must read the serial port and when recognizing a String (String already known sent by the arduino with BaudRate 38400) it must inform the baudRate used in the reading. My problem is that no matter what value I put in C # BaudRate, it still recognizes the data correctly.

I tried to change the baudRate on the Arduino, but the result is always the same. For example, the arduino sends the word "Hello" to a baudRate of 38400. The C # should read the serial port and when recognizing the word Hello (which should only appear with the correct BaudRate) it should display the used baudRate and end the function . However, even setting the baud rate to 1200, 2400 ... etc, ... C # always reads the word "Hello".

On the arduino serial monitor, when I change the speed, the characters are scrambled when I select a different speed than the one configured (as it should happen in C #).

I need C # to receive the scrambled serial data when the baudRate is incorrect.

Snippet of C# code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;

namespace Baud
{
    public partial class frmSerial : Form
    {

        public static System.IO.Ports.SerialPort serialPort1;
        //private delegate void LineReceivedEvent(string line);

        public frmSerial()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            Int32[] lista_bauds = new Int32[] { 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200 };
            Boolean connected = false;
            System.ComponentModel.IContainer components = new System.ComponentModel.Container();
            serialPort1 = new System.IO.Ports.SerialPort(components); // Creating the new object.



            for (int i = 0; i < lista_bauds.Length & connected == false; i++)
            {

                Console.WriteLine(lista_bauds[i]);

                string ReceivedData = "";
                serialPort1.PortName = "COM" + numCom.Value.ToString(); // Setting what port number.
                serialPort1.BaudRate = 1200; // Setting baudrate.
                serialPort1.ReadTimeout = 4000;

                //serialPort1.DtrEnable = true; // Enable the Data Terminal Ready 

                serialPort1.Open(); // Open the port for use.

                try
                {

                    if (serialPort1.IsOpen == true)
                    {

                        ReceivedData = serialPort1.ReadExisting();


                        Console.WriteLine(ReceivedData);
                        if (ReceivedData.Equals("Hello") == true)
                        {
                            txtDatasend.Text = "Conectado com BaudRate de" + lista_bauds[i]; ;
                            Console.WriteLine(ReceivedData);
                            connected = true;
                            btnConnect.Text = "Conectar";
                            serialPort1.Close();


                        }
                        else
                        {
                            serialPort1.Close();
                        }

                    }
                }
                catch (Exception)
                {

                   throw;

                }

            }


            numCom.Enabled = false;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            // Sends the text as a byte.
            serialPort1.Write(new byte[] { Convert.ToByte(txtDatasend.Text) }, 0, 1);
        }
    }
}

Heres the arduino code:

int LED_Pin = 13;                 // LED connected to digital pin 13

void setup()
{
 Serial.begin(38400);  // Configure the baudrate and data format for serial transmission
 pinMode(LED_Pin,OUTPUT);        // Pin13 as Output
 digitalWrite(LED_Pin,LOW);      // Pin13 LED OFF
}

void loop() 
{
  Serial.println("Autenticar"); // Send the String,Use Serial.println() because we have used SerialPort.ReadLine() on C# side
                                        // SerialPort.ReadLine() on C# side will return only after receiving '/n' character
  Blink_LED();                          // Blink LED to indicate transmission
}

//Function to Blink LED 13 
void Blink_LED()
{
  digitalWrite(LED_Pin,HIGH);
  delay(100);
  digitalWrite(LED_Pin,LOW);
  delay(100);
}
JohnX
  • 1
  • The code is incomplete, for educational purpose. – JohnX Jun 10 '20 at 03:57
  • try to change SerialPort settings in computer management. – Artavazd Jun 10 '20 at 05:54
  • 1
    IN your C# code, you have a for loop to get the different baud rates, but for each of them you just set the rate to a hard coded 1200. You print lista_bauds[i]. But you set the baud rate to 1200. – Delta_G Jun 10 '20 at 07:20

0 Answers0