2 weeks, 3 days ago.

How to get an int and string form serial input

I am getting an input from a serial port that will start with a capital letter such as "S" or "T" etc. The capital letter will sometimes (not every time) be followed by an integer such as "215" or "3" etc. I need to be able to store the capital letter in a char and depending if that letter is followed by an integer i need to store it in an int. However i'm struggling to determine how to implement this. Any help will be appreciated.

Comment on this question

1 Answer

1 week, 6 days ago.

Hi Benjamin,

Something like this: Serial pc(USBTX, USBRX); tx, rx

int main() { uint8_t ch ; uint8_t i=0; while(1) { if(pc.readable()) { ch = pc.getc(); if(ch >= 'A' && ch<='Z') {

do something
} else if(ch<=0 && ch <= '9') {
this is number
} }

Something like this. I don't want to write whole example. Use ch[4] (table) where you read characters. Then you will need index what you increase always when you received number. When you next time receive letter you can convert previous characters in ch[] to interger.

Regards, Pekka


You need to log in to post a question