UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Explorer
Explorer
6,047 Views
Registered: ‎11-17-2015

How do I make VC++ 2012 talk to ZC702 via COM port?

I am able to debug with SDK running modules I exported from Vivado block builder. And I am able to send commands through and read back with printf via the SDK Terminal. 

 

Now I wish to have VC++ to communicate to the target so I can dump data to the target CPU/FPGA for processing. 

Is the codes below correct? 

 

// hls_macc.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

// opty.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <atlstr.h>
#include <windows.h>
class opty
{
public:


	bool WriteComPort(CString PortSpecifier,CString data)
	{
		int ss=0;
		DCB dcb;
		DWORD byteswritten;

		HANDLE hPort = CreateFile(
			PortSpecifier,
			GENERIC_WRITE,
			0,
			NULL,
			OPEN_EXISTING,
			0,
			NULL
		);
		Sleep(60);

		if (!GetCommState(hPort,&dcb))
		return false;
		dcb.BaudRate = CBR_115200; //9600 Baud
		dcb.ByteSize = 8; //8 data bits
		dcb.Parity = NOPARITY; //no parity
		dcb.StopBits = ONESTOPBIT; //1 stop
		if (!SetCommState(hPort,&dcb))
			return false;
		//int i=0;
		Sleep(60);

		//int l = CString.GetLength(data);
		//for(int i=0;i<l;i++)
		bool retVal = WriteFile(hPort,data,data.GetLength(),&byteswritten,NULL);
		Sleep(60);

		printf("\nBytes written: %d\n",byteswritten);

		CloseHandle(hPort); //close the handle
		return retVal;
	}


int ReadByte(CString PortSpecifier)
{
	DCB dcb;
	int retVal;
	BYTE Byte;
	DWORD dwBytesTransferred;
	DWORD dwCommModemStatus;

	HANDLE hPort = CreateFile(
		PortSpecifier,
		GENERIC_READ,
		0,
		NULL,
		OPEN_EXISTING,
		0,
		NULL
	);
	Sleep(60);
	if (!GetCommState(hPort,&dcb))
		return 0x100;
	dcb.BaudRate = CBR_115200; //9600 Baud
	dcb.ByteSize = 8; //8 data bits
	dcb.Parity = NOPARITY; //no parity
	dcb.StopBits = ONESTOPBIT; //1 stop
	if (!SetCommState(hPort,&dcb))
		return 0x100;
	Sleep(60);

	SetCommMask (hPort, EV_RXCHAR | EV_ERR); //receive character event
	Sleep(60);
	WaitCommEvent (hPort, &dwCommModemStatus, 0); //wait for character
	Sleep(60);
	if (dwCommModemStatus & EV_RXCHAR)
		ReadFile (hPort, &Byte, 1, &dwBytesTransferred, 0); //read 1
	else if (dwCommModemStatus & EV_ERR)
		retVal = 0x101;
	Sleep(60);

	retVal = Byte;

	CloseHandle(hPort); //close the handle

	CloseHandle(hPort);
	return retVal;
} 

};

int _tmain(int argc, _TCHAR* argv[])
{
	int i = 0;
	opty ob1;


	bool t  = ob1.WriteComPort("\\\\.\\COM2","24");  // write a byte
	i = 0;
	while (i<10) {  // Get some of the strings
		int a = ob1.ReadByte("\\\\.\\COM2");  // ARM echo it back with printf("\nReceived character: %d\n\r", a);
		printf("%c", a);
		i++;
	}
	bool t2 = ob1.WriteComPort("\\\\.\\COM2","25");

	printf("\nAny key to continue %d!\n", i);

	return 0;
}

 

0 Kudos
2 Replies
Explorer
Explorer
6,043 Views
Registered: ‎11-17-2015

回复: How do I make VC++ 2012 talk to ZC702 via COM port?

 

	WaitCommEvent (hPort, &dwCommModemStatus, 0); //wait for character

My ARM routine is, an infinite loop which scans a charater, immediately echo it back with printf. 

I would start the ZC702, try a few SDK terminal iterations, then disconnect SDK Terminal. 

 

Now I start VC++ 2012 debugger and try to run and connect to the same terminal. 

 

The WaitCommEvent is usually where it times out. 

 

 

 

0 Kudos
Explorer
Explorer
5,933 Views
Registered: ‎11-17-2015

回复: How do I make VC++ 2012 talk to ZC702 via COM port?

int _tmain(int argc, _TCHAR* argv[])
{
	int i = 0;
	opty ob1;


	bool t  = ob1.WriteComPort("\\\\.\\COM2","24");  // write a byte
	i = 0;
	while (i<10000) {  // Get some of the strings
		int a = ob1.ReadByte("\\\\.\\COM2");  // ARM echo it back with printf("\nReceived character: %d\n\r", a);
		printf("%c", a);
		i++;
	}
	bool t2 = ob1.WriteComPort("\\\\.\\COM2","25");

	printf("\nAny key to continue %d!\n", i);

	return 0;
}
0 Kudos