简体   繁体   中英

Read from serial port in Windows

I'm trying to read serial data from rs232. My rs232 is connected in comport COM11. I specified COM11 in my serial.h file. below is the code.

void port_open()
{
hSerial = 
CreateFile("COM11",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
..

My main c code is

#include <stdio.h>
#include "serial.h"
void main()
{
while(1)
    {
    port_open();
    printf("%s\n",port_read());
    }
}

After run the c code, im getting...

serial port does not exist
some other error occured. Inform user.
error getting state
error setting serial port state
error setting port state
error reading from input buffer


--------------------------------
Process exited after 0.4768 seconds with return value 0
Press any key to continue . . .

I think my program cant able to find the comport. How to resolve this? Thanks

To get access to serial ports with port number larger then 9 you should use

CreateFile("\\\\.\\COM11",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

More info here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM