Sending text and getting a response from Com Port

I am trying to do basic communication with a PLC I have connected to my Com Port and this just involves sending text and reading the response. I believe I did my code correctly but there must be something wrong because no text is getting sent and I'm reading back nothing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
int main(){

	HANDLE hCom = CreateFile("\\\\.\\COM4",
							GENERIC_READ | GENERIC_WRITE, 
							0, 
							NULL, 
							OPEN_EXISTING, 
							FILE_FLAG_OVERLAPPED, 
							NULL);
	if (hCom == INVALID_HANDLE_VALUE)
	{
		DWORD err=GetLastError();
		std::cout << "Failed\n";
		return 0;
	}

	std::cout << "Didn't Fail\n";
	
	DCB dcbConfig;
	if(GetCommState(hCom, &dcbConfig))
	{
		dcbConfig.BaudRate = CBR_115200;
		dcbConfig.ByteSize = 8;
		dcbConfig.Parity = NOPARITY;
		dcbConfig.StopBits = ONESTOPBIT;
		dcbConfig.fBinary = TRUE;
		dcbConfig.fParity = FALSE;
	}
	if(!SetCommState(hCom, &dcbConfig))
	{
		CloseHandle(hCom);
		return 0;
	}
	//std::cout << dcbConfig.BaudRate << " Baud Rate\n";
	COMMTIMEOUTS commTimeout;

	if(GetCommTimeouts(hCom, &commTimeout))
	{
		commTimeout.ReadIntervalTimeout     = 1;
		commTimeout.ReadTotalTimeoutConstant     = 1;
		commTimeout.ReadTotalTimeoutMultiplier     = 1;
		commTimeout.WriteTotalTimeoutConstant     = 1;
		commTimeout.WriteTotalTimeoutMultiplier = 1;
	}
	if(!SetCommTimeouts(hCom, &commTimeout))
		//Handle Error Condition
		CloseHandle(hCom);

	// Write to the COM
	static char data[22]="%01#RDD0010000107**\r";
	int size = strlen(data);
	DWORD dwWritten, dwReading;
	
	int j;
	WriteFile(hCom,data,(DWORD)size,&dwWritten,NULL);

	char datarecv[1];
	ReadFile(hCom,&datarecv,1,&dwReading,NULL);
	
	std::cout << datarecv << "\n";
	CloseHandle(hCom);
	
	std::cin>>j;
	return 0;
}


EDIT:
I changed FILE_FLAG_OVERLAPPED to 0 and now the command is being sent successfully but still nothing is being read back.
Last edited on
closed account (13bSLyTq)
Hi,

If the command has been sent, the PLC must write the command back to be read (Siemens PLCs do that). Its is either the PLC is not configured properly or the command itself is wrong.
I'm using a Panasonic FP-X C30T Control Unit AFPX-C30T, which I was told would return certain text.
closed account (13bSLyTq)
Hi,

I do not posses Panasonic FP-X C30T Control Unit AFPX-C30T PLC however I do program with PLCs often, with this being said I suggest you check the return values for line 55 & 58 as they are sending the initial command to the PLC, if the return value of line 55 & 58 (WriteFile & ReadFile) the return values must be a nonzero.

If the return values are 0 (NULL) call GetLastError() function to retrieve the error codes then compare them to System Error codes list specified in MSDN Library: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

That being said, its extremely good practice to error check as when similar bugs may arise in applications the developers can easily identify the bug and work on a solution to remove the bug.

Once you error check give me the details and I could help, but for now I am helpless without these details.
Last edited on
After adding the error checking, GetLastError() (both times) returns 0 after the successful transmission of the 20 bit command but still nothing is being read back.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
	// Write to the COM
	static char data[21]="%01#RDD0010000107**\r";
	int size = strlen(data);
	DWORD dwWritten, dwReading;
	DWORD err;
	int j;
	WriteFile(hCom,data,(DWORD)size,&dwWritten,NULL);

	if(!dwWritten)
	{
		err=GetLastError();
		std::cout << err;
	}
	char datarecv[22];
	ReadFile(hCom,datarecv,sizeof(datarecv),&dwReading,NULL);
	if(!dwReading)
	{
		err=GetLastError();
		std::cout << err << "\n";
	}
	std::cout << datarecv << "\n";
	CloseHandle(hCom);
	
	std::cin>>j;
	return 0;
closed account (13bSLyTq)
Hmmm....Strange are you sure the PLC itself is configured properly or the command itself is correct.

Topic archived. No new replies allowed.