Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C code in win7 to use uart???

Status
Not open for further replies.

c1235366

Newbie level 5
Newbie level 5
Joined
May 26, 2012
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,339
Hi all:

I want to receive data from rs232 port in PC.

I used dev C++ 4.9.9.2 to write code.

I'll use a another device to send data.

The part of code is following:

FILE *dpg;

int main(void) {

system("mode com1:115200,N,8,1,X");


if ((dpg = fopen("COM1", "rb+")) == NULL) {
printf("fail to open COM port\n");
system("pause");
return 1;
}

int comm1;
printf("GET:",comm1);

while ((comm1= fgetc(dpg)) != EOF) {
printf("%d,%d\n",comm1,sizeof(dpg));
}

printf("\rget-ok\n");
system("pause");
return 0;

}

The question is:

When the code run on fgetc(dpg), code will stop (not broke, just stop ).

If the device just send a byte of data, the code won't print it.


If I want the code continue running, the device have to send data consistently, and it will print all datas.

The feel like have a big buffer.

I have to send data until fill the buffer, the code will continue run.


I just want to receive a byte of data.

Please help me.

Thanks all.
 

Are you sure that stream IO for COM ports is supported by your compiler? I have been using CreateFile(), ReadFile() and WriteFile() with Windows since ever.
 

Are you sure that stream IO for COM ports is supported by your compiler? I have been using CreateFile(), ReadFile() and WriteFile() with Windows since ever.


Thanks for your reply.

Sorry, my ability not good.

I only could describe my current situation.

Compiler not appear error.

I sure PC can receive data from com port, and the data was right, but I have to fill the buffer before.
 

Try to avoid use of system() as much as you can. there are several calls which can cause security issues.
Please get hold of Petzold's windows programming book. it will help.

For low level access to system devices, knowledge of OS internal and Driver framework is essential.
 

Maybe that c code example will help you to have more parameters on hands for what you want to do.


**broken link removed**
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top