Read call in serialdriver doesn’t work after updating termio

We am trying to read large data in loop for this I have created test case which takes baud rate as input.
Steps followed:

  1.  Device entry already exists in the system
  2.  Open the device using open system call
  3.  Update the baud rate using  “cfsetispeed” and “cfsetospeed” followed by “tcsetattr”( TCSANOW option is used)
  4.  Read the data from the terminal using read system call in loop and read size is 255 bytes and with loop count of 100

Issues faced:
Read call becomes no blocking and returns “0” and perror says “No Error” with above steps and if the step 3 is not carried out read call waits and read happens in default baudrate(115200)

To check what exactly happens I used stty to check the terminal status and I see the it does print data completely post updating the termios structure

Stty results before step 3:
Name: /dev/ser2
Type: serial
Opens: 1
intr=^C quit=^\ erase=^? kill=^U eof=^D start=^Q stop=^S susp=^Z
lnext=^V min=01 time=00 pr1=^[ pr2=5B left=44 right=43 up=41
down=42 ins=40 del=50 home=48 end=59
par=none bits=8 stopb=1 baud=115200 rows=0,0

Stty results before step 4 which is not fully printed and I guess some corruption as well
Name: /dev/ser2
Type: serial
Opens: 2
min=00 time=00
par=none bits=5 stopb=1 baud=9600 rows=0,0

let me know the possible reason for such behavior and point out any steps that I have missed to carry out and i see that data bits bits are changed to 5 from 8 which i did not try to modify.

You’ll need to post your code.

If you just want to change the baud rate there is a simple way in code to do it:

int baud = 9600;
int port = 1;
char buffer[80];

sprintf(buffer, “stty baud=%d </dev/ser%d”, baud, port);
system (buffer);