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)
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.
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"].
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.
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
}
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
}
}
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.
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.