Com Communication

Hi guys,

Just need a little help with talking to a Com port. Am using Visual C++ (fairly new to it done a fair bit of C though.). The code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
              

           CString str;
                  
            //write to ComPort

		    char szBuff[8 + 1] = "AT";
		    DWORD dwBytesRead2 = 9;
	            WriteFile(hSerial, szBuff, 8, &dwBytesRead2, NULL);
			
			
	    //Read ComPort

			char RBuff[256+1]={0};
			DWORD dwBytesRead = 256;
			ReadFile(hSerial, RBuff, 256, &dwBytesRead, NULL);
                        str.Format("COM 1 Read: %s",RBuff);
		        pMem->SetWindowText(temp+"\n"+str);
			//Close ComPort
			CloseHandle(hSerial);
			}
		
	    
		}


Basically the com port is open and all set up in the code previous to this. I have set szBuff equal to AT (I'm trying to send AT codes to a modem) and then I believe write file is sending that to the com port.

I'm then using ReadFile to read the com port, using RBuff as the buffer to store the data in, when printing RBuff it just displays something like: "iiiiiiiiiiiiiiiiiiiiiiiiiii" depending on the amount of bytes I've set to read.

Any help would be great

Regards,

Simon
What is the value of dwBytesRead after ReadFile? If no bytes are read, RBuff still has uninitialized values.

Shouldn't this be in the windows forum?
Yeah sorry I may well have put this in the wrong forum!,

The value of dwBytesRead is NULL :(!
Also I have &dwBytesRead2 and &dwBytesRead is that correct? Am I right in saying that these just store how many bytes were actually read and in this case NULL meaning none were read!
You are right
ok....well erm got no idea where to go next I cant see anything particularly wrong with the source code?
In serial communication, the problem is not always in your code.

Things to check :
- Are you really connected to the serial port (in software and hardware)?
- Do ReadFile and WriteFile return 0? In that case, what code does GetlastError return?
- Are you connected "synchronously" or "asynchronously"? Read will only wait to recieve the requested number of bytes if you use the sybnchronous mode
- Is the target (the other end of the serial port) ready? Does it have the same serial configuration? Do you see data on the serial wires (with an oscilloscope)?
Topic archived. No new replies allowed.