Click here to Skip to main content
15,117,318 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
hi all
i have a device called nonin 9560 (pulse oximeter) which communicate bluetooth
i am working on windows 7 visual 2008 c++
ı have a code tries to read data from the device(the data is the oxygen saturation for ex 98)
bur the problem is i can not read the data!
ReadFile does not work!
please help me...........
here is the code:

int main(int argc, char* argv[])
{
	char INBUFFER[5000] = {'\0'};
   
    DWORD        bytes_read    = 0;    // Number of bytes read from port
    HANDLE       comport      = NULL;  // Handle COM port
	int   bStatus;
    DCB          comSettings;          // Contains various port settings
    COMMTIMEOUTS CommTimeouts;
   

    // Open COM port
    if ((comport = CreateFile("COM3:",                // open com5:
                    GENERIC_READ | GENERIC_WRITE, // for reading and writing
                    0,                            // exclusive access
                    NULL,                         // no security attributes
                    OPEN_EXISTING,              
                    0,
                    0)) == INVALID_HANDLE_VALUE)
    {
        printf("cannot open\n");
    }
    
	
	/*Set timeouts in milliseconds*/


		GetCommTimeouts (comport, &CommTimeouts);

		// Change the COMMTIMEOUTS structure settings.
		CommTimeouts.ReadIntervalTimeout = MAXDWORD;  
		CommTimeouts.ReadTotalTimeoutMultiplier = 0;  
		CommTimeouts.ReadTotalTimeoutConstant = 0;    
		CommTimeouts.WriteTotalTimeoutMultiplier = 10;  
		CommTimeouts.WriteTotalTimeoutConstant = 1000;    

		// Set the timeout parameters for all read and write operations
		// on the port. 
		if (!SetCommTimeouts (comport, &CommTimeouts))
		{
		  printf("SetCommTimeouts Error\n");
		}



     //Set Port parameters.
     //Make a call to GetCommState() first in order to fill
     //the comSettings structure with all the necessary values.
     //Then change the ones you want and call SetCommState().

	if (GetCommState(comport, &comSettings)==0){

		printf("GetCommState Error\n");
	}
	else
	{
		comSettings.BaudRate = 9600;
		comSettings.StopBits = ONESTOPBIT;
		comSettings.ByteSize = 8;
		comSettings.Parity   = NOPARITY;
		comSettings.fParity  = FALSE;

		bStatus = SetCommState(comport, &comSettings);
		try {
				if (bStatus == 0)
				{
					printf("SetCommState Error...\n");
				}
				BYTE Byte;
				DWORD dwBytesTransferred;



				// Read the data from the serial port.
				ReadFile (comport, &Byte, 1, &dwBytesTransferred, 0);

				// Display the data read.
				if (dwBytesTransferred == 1){
						ProcessChar (Byte);
				}

			  
					CloseHandle(comport);
		}
		catch( exception e)
		{
			CloseHandle(comport);
		}
	}
 return 0;
}


i have an additional problem now!
i cannot open com3 anymore. smt happened.
any other program like oem evaluation prog, putty, hercules can not open also anymore.
they were openning fine ysterday!!
Posted
Updated 12-Sep-16 3:03am
v4
Comments
Piccadilly Yum Yum 23-Mar-11 5:29am
   
Maybe hardware problem... Be sure that RTS-CTS pin are crossed..
Wendelius 23-Mar-11 5:30am
   
Pre tags added
CPallini 23-Mar-11 6:07am
   
Why don't you check the ReadFile return value?
SnowHow 23-Mar-11 8:54am
   
Have you verified using a terminal program that the device is transmitting data?

Also ... you seem to have an extra semi colon ("COM3:") .... Try ("COM3") Also to be safe I suggest using the form "\\\\.\\COM3"

First, you should use Hercules[^] utility to check that the communication is OK.

If you can't comminucate with Hercules, then you probably have a problem with your device. Otherwise the problem comes from your code:
- Check that all the parameters are correct in your DCB struct.
- Check the return value from ReadFile and if it returns FALSE, call GetLastError to have extra information.

One last thing. How you open your port is strange to me. The safest way to open a COM port is:
//let's open COM3
int comNumber = 3;
//you should add "\\.\" before "COM3"
char comName[10];
sprintf(comName, "\\\\.\\COM%d", comNumber);
comport = CreateFile(comName, ...);


If you don't use "\\.\" you may have problems for COM ports greater than COM9.

-----------

By the way, you should wait a little bit after the port is opened, just to give time for the data to arrive.
For example do a Sleep(2000) (wait 2 seconds) before calling ReadFile.
   
v2
Comments
mstftrn 23-Mar-11 9:33am
   
i did what you said but when i execute: no error...
i see just press any key to continue!
Olivier Levrey 23-Mar-11 9:42am
   
This message just means your program finished.

You have no error? So ReadFile returns TRUE and dwBytesTransferred equals 1? So you probably have a problem in the ProcessChar function, haven't you?
Olivier Levrey 23-Mar-11 9:44am
   
I updated my answer with Sleep
mstftrn 23-Mar-11 9:50am
   
in fact when i use just COM3: (i learn from msdn library) i see press any key...
but when i use "comNumber" port can not open.
here is the processchar

unsigned char buffer[255] = {0};
int nextin = 0; // next spot to add a new charcter
void ProcessChar(unsigned char c)
{

if( nextin < sizeof(buffer)) // make sure there's room for the char

buffer[nextin++] = c;

}
mstftrn 23-Mar-11 9:56am
   
i change like this

if (ReadFile (comport, &Byte, 1, &dwBytesTransferred, 0)){

if(dwBytesTransferred == 1){
resp=Byte;
ProcessChar (Byte);
printf("smt is strange");

return true;
}
}else {printf("cannot read...");}

i dont see any printf output!!
Olivier Levrey 23-Mar-11 9:59am
   
So it is probably because there is nothing to read: dwBytesTransferred is 0.
Try with several values of Sleep (I showed you in my updated solution).
mstftrn 23-Mar-11 10:08am
   
hi i make you busy but
as you said i add Sleep(10000)
and see "smt is strange"
and really strange not to see data read but "smt is strange"
Olivier Levrey 23-Mar-11 10:17am
   
If you see "smt is strange", it means you read 1 byte properly. But I don't see any printf in your ProcessChar function. If you want your program to output the byte you read, then you should printf it...
printf("%c", Byte) or printf("%d", Byte). Or you can also use the debugger to display its value (just put a break point after the ReadFile function.
Peter_in_2780 24-Mar-11 3:17am
   
Olivier said: If you don't use "\\.\" you may have problems for COM ports greater than COM9.
On Vista, that requires "run as adminstrator" privileges... Works fine on XP, don't know about W7.
Olivier Levrey 24-Mar-11 5:00am
   
I encountered problems a few years ago (I don't remember if it was Win2000 or WinXP). Using "\\.\" solved them. Have a look to that page:
http://msdn.microsoft.com/en-us/library/aa363858(v=vs.85).aspx
section Communications Resources
, it is written: To specify a COM port number greater than 9, use the following syntax: "\\.\COM10". This syntax works for all port numbers and hardware that allows COM port numbers to be specified.
Peter_in_2780 24-Mar-11 7:15am
   
Don't want to hijack this thread, so just quickly: I had a program that used Bluetooth to talk to mobile phones using serial emulation. Needed \\.\ under XP to talk to COM13. All good. Then on Vista, it can't CreateFile() to open \\.\COM13 unless the exe is run as admin. Bug reported to M$ years ago, no response.
Olivier Levrey 24-Mar-11 7:20am
   
Good to know, thanks.
mstftrn 24-Mar-11 7:52am
   
hello oliver,
could you please glance solution 3(in fact a question!)
i need more help
thanks
i can not see my last comment so writing again, sorry

- when i use \\.\ cannot open
- when i use \\\\.\\ (as oliver said yesterday) and use my nonin devíce, opens and prints
0 2 0 13 0 14 32
char buf[7];
DWORD read = 0;
//Read the data from the serial port.
if (ReadFile(comport,buf,sizeof(buf),&read,NULL)){
DWORD i;
for (i=0; i<read; i++){
printf("%i ", (unsigned char)buf[i]);
}

(i added above code)

- when i use when i use \\\\.\\ and do not use nonin device cannot open...
- when i use \\\\.\\ , use my nonin devíce and just printf("%c", Byte) insdead of the code above opens and prints just 0.
   
Comments
Olivier Levrey 24-Mar-11 8:56am
   
Since this is a question, you should use "Improve question" instead of posting an answer...

I don't understand your problem:
1- You can open your port successfully with "\\\\.\\COM3" --> great
2- You can read data --> great
3- You can print the data --> great

What do you want more?
mstftrn 24-Mar-11 9:40am
   
sorry for missing information but
the output (buf) 0 2 0 13 0 14 32 is wrong
the output of printf("%c", Byte) which is 0 or null is also wrong
on my nonin device's screen we have O2 saturation and pulse rate for ex 98 and 70
i should get these.
so my output is wrong...

thanks for your time
Olivier Levrey 24-Mar-11 9:53am
   
Do you know the format of the data sent? Maybe your device is sending int values or short or maybe it is sending a more complex paquet. Check its documentation and you will know how to read the data. I think you just need to interpret the data properly.
mstftrn 24-Mar-11 9:56am
   
ok thank you
i will try
I try to read data from the same device. When you are connected you should read a 22 bits buffer.
buffer[17] = HeartRate
buffer[19] = Oxygen saturation

Concerning myself, when I try to connect my device, it's not always working. I have to wait the little animation on the device and then, the 9560 send only one time the information. Do you have the same problem??
   
Comments
mstftrn 30-Mar-11 3:55am
   
In fact my problem was about wrong data format. The device's defoult data format was data format 13.
I needed to change this by sending 6 byte information. We should use data format 8 or 2.
Read the device's documentation
mstftrn 30-Mar-11 6:01am
   
you may look at this
http://www.codeproject.com/Questions/174373/how-to-read-hex-data-stream.aspx
Take a look here for hardwire :

http://www.lammertbies.nl/comm/cable/RS-232.html[^]
   
Comments
Peter_in_2780 24-Mar-11 3:15am
   
The OP said he's using Bluetooth (serial port emulation) so a cable is not going to help!
Piccadilly Yum Yum 24-Mar-11 4:27am
   
yes... i think its better disabling handshake
Did you verify that Windows still assigns the same port name to your device? If all programs have a problem reading from COM3, it might be that Windows has now for example assigned the name COM4 to your device.

You can check this by going to the Device Manager (if you're using Windows XP; Control Panel > System > Hardware > Device Manager) and having a look at the list under Ports (COM & LPT).
   
Comments
mstftrn 25-Mar-11 5:10am
   
hi thanks for your advice
under Ports (COM & LPT) there are
COM1
COM3
COM4
but i tried oem eval prog for COM4, it cannot connect
hercules just open port4
putty can not

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900