Can we connect to the DR through TCP instead of UDP?

CAN to LAN gateway in DIN rail plastic casing
Post Reply
chris@amwgps.com
Posts: 1
Joined: Thu 26. Jul 2018, 03:30

Can we connect to the DR through TCP instead of UDP?

Post by chris@amwgps.com » Thu 26. Jul 2018, 03:34

I was able to communicate to the PCAN Ethernet DR using UDP, but can we do the same with TCP instead?

User avatar
PEAK-Support
Sales & Support
Sales & Support
Posts: 1646
Joined: Fri 10. Sep 2010, 19:34

Re: Can we connect to the DR through TCP instead of UDP?

Post by PEAK-Support » Thu 26. Jul 2018, 10:07

Open a Socket für TCP instead for UDP and follow the Socket Documentation.
TCP need more overhead - but the Data Packages inside are always the same as in UDP
Good Info how to use Sockets with TCP or UDP could be found here
Attached a Sender/Receiver Sample in C which could be used for TCP or UDP.
Attachments
Gateway_C_Sample.zip
(10.19 KiB) Downloaded 2305 times
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

ssidhant
Posts: 2
Joined: Tue 27. Nov 2018, 22:17

Re: Can we connect to the DR through TCP instead of UDP?

Post by ssidhant » Tue 27. Nov 2018, 22:28

Hey,
Could you please provide more information as to how to run the c files. I constantly get this error "handle_loc_cmd(): no such command found:"
thank you!

User avatar
PEAK-Support
Sales & Support
Sales & Support
Posts: 1646
Joined: Fri 10. Sep 2010, 19:34

Re: Can we connect to the DR through TCP instead of UDP?

Post by PEAK-Support » Wed 28. Nov 2018, 09:21

Where and when do you get the "error". Please post the needed information...
The code is tested and work fine. The handle_loc_cmd(); is a function which is declared and available inside the code . So should be a problem with your environment / developer Tool.

Code: Select all

/*******************************************************************************
                          local function prototypes
 ******************************************************************************/
static void print_usage(char *name);
static void print_args(void);
static void printSWinfo(char *name, S_SW_VERSION_INFO *p_info);

static int init_application(void);
static void exit_application(void);
static void signal_handler(int s);
static int handle_loc_cmd(int fd);
int priv_openSocketOut(int port, char* p_ip, int type);
int priv_closeSocket(int sock);
static void init_outgoing_data_socket(void);
and later on in the code you see the function itself

Code: Select all

//------------------------------------------------------------------------------
// static int handle_loc_cmd(int fd)
//------------------------------------------------------------------------------
//! @brief	handles commands from given file descriptor
//------------------------------------------------------------------------------
//! @param	ind 	 file descriptor to read from
//------------------------------------------------------------------------------
//! @return	<0 in case of errors or any return value from called command handler
//------------------------------------------------------------------------------
static int handle_loc_cmd(int fd){
	char cmdbuf[MAX_LOC_CMD_LEN+1];
	int len=0, i=0;

	if (fd != STDIN_FILENO) /* not STDIN_FILENO */
		return -1;

	len = read(fd, &cmdbuf[len], MAX_LOC_CMD_LEN - len);

	if (verbose > 3) {
		printf("got %d chars:\n   ", len);
		for (i = 0; i < len; i++)
			printf("%0x2 ", cmdbuf[i]);
	}

	if (len == 0) {
		if (verbose > 1)
			printf("%s(): nothing to read on %d\n", __func__, fd);
		return -2;
	} else if (len == MAX_LOC_CMD_LEN) {
		if (verbose > 1) {
			printf("%s(): maybe more then %d chars to read on %d\n", __func__,
					MAX_LOC_CMD_LEN, fd);
			return -1;
		}
	}
	cmdbuf[len] = 0;

	if (verbose > 3)
		printf("%s(): cmd line=%s\n", __func__, cmdbuf);

	i = 0;
	while (loc_cmds[i].cmd_text) {
		if (strstr(cmdbuf, loc_cmds[i].cmd_text))
			break;
		else
			i++;
	}

	if (!loc_cmds[i].cmd_text) {
		if (verbose > 2)
			printf("%s(): no such command found: %s\n", __func__, cmdbuf);
		return -3;
	}

	if (!loc_cmds[i].cmd_handler) {
		if (verbose > 2)
			printf("%s(): no handler defined for command: %s\n", __func__,
					cmdbuf);
		return -4;
	}

	return loc_cmds[i].cmd_handler(&cmdbuf[strlen(loc_cmds[i].cmd_text)]);
}
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

ssidhant
Posts: 2
Joined: Tue 27. Nov 2018, 22:17

Re: Can we connect to the DR through TCP instead of UDP?

Post by ssidhant » Thu 29. Nov 2018, 02:04

I am working on windows. Could you please tell me what arguments you give.
For instance: I try to compile it using: g++ host_rx.c -o test
And try to run the executable.

I am trying to receive and send UDP messages via ethernet.

User avatar
PEAK-Support
Sales & Support
Sales & Support
Posts: 1646
Joined: Fri 10. Sep 2010, 19:34

Re: Can we connect to the DR through TCP instead of UDP?

Post by PEAK-Support » Thu 29. Nov 2018, 11:23

We do not care on which OS you work - thats up to you. It is Standard C with Standard Socket LIB, that works on every System where a ANSI C Compiler and the Socket Library is available. We also do not care if you use a Visual Studio or any GCC / MinGW Compiler. This is up to you. The code work out of the box - but you need to create your project by your own and simply copy the code to it.You need basic knowledge on how to include the Socket Header file and how to setup a project on your prefered environment.
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

Post Reply