Principales respuestas

problem in the serial progrmming code!!!

Pregunta

Hello,
I am using Windows 7, the code i have written for serial programming is not receiving the data transmitted :when I connected a null modem to the serial port to read the string i wrote to the serial port but nothing is read on serial port. The program copiles,
builds and executes but i get error reading. can
anybody help me please !!!

Todas las respuestas

You don't have a valid read buffer. The buffRead variable is an uninitialized pointer containing some garbage value. Windows expects it to be the address of some memory that you have allocated. Try this instead:

Ya it worked but if there is no data on RXD pin compiler is not executing readfile function and remaining too.

Unless you have somehow arranged for your source code to be provided on the serial port, it's not the compiler which is 'not executing readfile function' here, but the running program itself.

I interpret your phrase 'not executing readfile function and remaining too' as meaning that the ReadFile is not returning.

This is normal behaviour for a synchronous ReadFile call on such a device. It won't return until there is some data to read (in this case, on RXD pin). If you want asynchronous behaviour, you need to use
ReadFile's
Overlapped mechanism (the final paramater which you have set to NULL), or that of ReadFileEx.

It won't return until there is some data to read (in this case, on RXD pin). If you want asynchronous behaviour, you need to use
ReadFile's
Overlapped mechanism (the final paramater which you have set to NULL), or that of ReadFileEx.

Besides of the Overlapped thing, you can specify read timeout, and any available data (or none) will be returned. See
this.

Another error is in the printf of buffRead. The received string is not nul terminated, so printf will attempt to print more than just the number of bytes received. This will print garbage and it might also cause an access violation. After receiving
some bytes you should add