Change the config in Serial.begin(speed, config)

Hi, I want to change the serial config by user's command as follow:

char user_config = "SERIAL_8N1" ; (changed by user's input)
Serial.begin(9600,user_config);

But I got a error like: invalid conversion from 'char*' to 'uint8_t'
And if I do Serial.print(SERIAL_8N1), I got "6". Serial.print(SERIAL_8N2) will give "14"
So there's obviously an inner library doing this conversion. My question is how can user change the config without knowing such conversions. (User can only give the config through UI)

wxs:
My question is how can user change the config without knowing such conversions. (User can only give the config through UI)

Or a better way to ask the question might be:

How to pass the parameter ‘config’ as a variable

You are not passing a string there. If you write it out, it is

Serial.begin(9600,SERIAL_8N1);

not

Serial.begin(9600,"SERIAL_8N1");

SERIAL_8N1 is a constant defined in some header file and gets replaced by the precompiler.
This is again one of those examples that should why it is a bad idea that the Arduino documentation does not state the type of function parameters.

You find the definition in /hardware/arduino/avr/cores/arduino/HardwareSerial.h
If you want to offer this to users, I guess you have to write and maintain your own conversion table using those definitions.
You can write the mapping as “SERIAL_8N1”->SERIAL_8N1, so the precompiler replaces the latter, in case this is ever changed in the underlying lib.

Edit: are the code boxes huge for everyone? Is there a way to fix this?

ElCaron:
You can write the mapping as “SERIAL_8N1”->SERIAL_8N1, so the precompiler replaces the latter, in case this is ever changed in the underlying lib.

Could you please explain more on how to write the mapping? Thanks.

From HardwareSerial.h:

#define SERIAL_5N1 0x00
#define SERIAL_6N1 0x02
#define SERIAL_7N1 0x04
#define SERIAL_8N1 0x06
#define SERIAL_5N2 0x08
#define SERIAL_6N2 0x0A
#define SERIAL_7N2 0x0C
#define SERIAL_8N2 0x0E
#define SERIAL_5E1 0x20
#define SERIAL_6E1 0x22
#define SERIAL_7E1 0x24
#define SERIAL_8E1 0x26
#define SERIAL_5E2 0x28
#define SERIAL_6E2 0x2A
#define SERIAL_7E2 0x2C
#define SERIAL_8E2 0x2E
#define SERIAL_5O1 0x30
#define SERIAL_6O1 0x32
#define SERIAL_7O1 0x34
#define SERIAL_8O1 0x36
#define SERIAL_5O2 0x38
#define SERIAL_6O2 0x3A
#define SERIAL_7O2 0x3C
#define SERIAL_8O2 0x3E

In a more plentiful environment than a microcontroller, you would probably use some sort of dictionary.
ArduinoJson seems to have a neat implementation that lets you access data for the keyword "foo" with
doc["foo"].

The simple solution would be an if-ladder

uint32_t serialConfigFromString(const char* config) {
  if( 0 == strcmp(config,"SERIAL_8N1"))
    return SERIAL_8N1;
  else if( 0 == strcmp(config,"SERIAL_8N2"))
    return SERIAL_8N2;
  ...
}

Or you could write an object that holds a constant array of the strings, and a constant array of the ints, overload the -operator with a function that loops through the array and output the respective int in case of a match.
This would have the advantage that the list of strings is still programatically accessible, e.g. to built the menu.
Don't ask me how to really properly built that with progmem and so on.

Did you solve this? I am having exactly the same problem.

What have you tried ?

Do you understand the relationship between the parameters and the defined values in reply #4 ?

I tried the following:

char Comsetting[60];
uint32_t ComValue; // don't know if this is the correct type

void comsetup_setup()
{
if (strcmp(Comsetting,"7E1") == 0 ){
#define ComValue SERIAL_7E1
}
if (strcmp(Comsetting,"8N1") == 0){
#define ComValue SERIAL_8N1
}
}

COM[1]->begin(BAUD, ComValue, SERIAL1_RXPIN, SERIAL1_TXPIN, true);

but if I do:
Serial.print(ComValue);

the ComValue returned is always a strange number like 22048423. I'm trying to solve this for several days but it just won't work. I also tried it like this:

uint32_t ComValue; // don't know if this is the correct type

if (strcmp(Comsetting,"7E1") == 0 ){
#define ComValue SERIAL_7E1
}
if (strcmp(Comsetting,"8N1") == 0){
#define ComValue SERIAL_8N1
}

but then the ComValue would always return 0

I tried the following:

Sorry, but I am no longer sure that I want to help people who do not do the best to post code in a way that makes it easy to provide help

For goodness sake, please look at this sticky thread from the top of the page of questions Read this before posting a programming question and follow its recommendations on posting code

You would also be well advised to read up on exactly what #define does and particularly when it does it

What is on the sending end? Somewhere, you are choosing a setting to transfer to the Arduino. If it is a user program, it could index the table shown in reply#4, and simply pass the uint8_t value instead of the text string.

Sometimes you have to back up and look at the big picture. Surely, I hope you don't have some user consulting a book to look up, "7E1" to type in...

Wat do you mean? That I have to post all the code? Please be patient, I am still learning. I tried to shorten it in the first question because it's quite big (15000 characters). I can only post a message of max 9000 so I added it as attachments.

The program is for an esp32 wich you can configure with a webpage to join a wifi ssid. The esp connects to a serial port wich can be read out over a telnet session. That works perfect when i configure the hardwareserial config with static defines like 115200 BAUD and SERIAL_8N1.

I want to be able to configure the com port settings by a web form. This works for the baudrate but not for the other configuration like SERIAL_8N1. In the attached code the ComValue is something like 123432 when it has to be SERIAL_xxx.

What I also did is the following:

uint32_t ComValue;  // don't know if this is the correct type

void comsetup_setup()
{
    if (strcmp(Comsetting,"7E1") == 0 ){
    #define ComValue SERIAL_7E1
  }
  if (strcmp(Comsetting,"8N1") == 0){
    #define ComValue SERIAL_8N1
  }
}

Then it will always return 0.

Also here

Do not cross-post or necro-jack.
Both waste time.

#define ComValue SERIAL_7E1

That's just plain nonsense. How can you have a 15KB program and not know how to assign a value to a variable?

I showed you in Reply #3 to your duplicate thread.

gfvalvo:

#define ComValue SERIAL_7E1

That's just plain nonsense. How can you have a 15KB program and not know how to assign a value to a variable?

I showed you in Reply #3 to your duplicate thread.

If you really showed me I had a working program by now. It also would have helped if the original poster in this post also posted the answer. As I told I am trying to learn so I was hoping at a little bit of understanding and patience. Looks like I have to google for a couple of days when you can probably show me with your knowledge.

It also would have helped if the original poster in this post also posted the answer.

That was five months ago.

It would help if you didn’t keep starting new threads on the same topic.

So what do I have to do here to find the right answer?

rwanrooy:
So what do I have to do here to find the right answer?

Stop diluting the effort that people are willing to donate. I, for one, am not willing to pick up the threads of your topic from disparate sources.

Stop cross-posting.

Post code. In one place.

TheMemberFormerlyKnownAsAWOL:
Stop diluting the effort that people are willing to donate.

Stop cross-posting.

Post code. In one place.

Thanks for your help man, you made my day!

rwanrooy:
If you really showed me I had a working program by now. It also would have helped if the original poster in this post also posted the answer. As I told I am trying to learn so I was hoping at a little bit of understanding and patience. Looks like I have to google for a couple of days when you can probably show me with your knowledge.

Which part of this didn't you understand:

gfvalvo:
So:

  uint8_t settings;

settings = SERIAL_8N1;
 Serial.begin(115200, settings);



You can use 'if' statements to control which value is assigned to the 'settings' variable.

And, if you didn't understand it, why didn't you ask for further explanation?