Socket Messaging with Basic IO Map (2020-03-24)
Socket Messaging with Basic IO Map (2020-03-24)
This document describes how to configure a Coherix sensor to communicate with a Robot, PLC, or PC
using TCP/IP Socket Messaging. The Coherix sensor is configured as a TCP/IP server and the Robot, PLC,
or PC is configured as a TCP/IP client. The communication takes place using a custom protocol called
“P3D Link”.
This document also provides an example C program which demonstrates how to program a Windows PC
to communicate with the Coherix sensor.
Click Add
Select P3D Link
Use these
Heart Beat
settings
For more information on the values shown in the above tables, see the document titled “General Setup
Instructions for Predator3D and Robot Communication.xlsx”. The file can be found by going to
filetransfer.coherix.com and clicking “Communication Setup”.
Example C Program
This section presents an example C program that runs on a Windows PC. This example program will
communicate with the Coherix sensor using TCP/IP socket messaging. This example was successfully
compiled with Microsoft Visual Studio 2017.
#include <stdio.h>
#include <stdbool.h>
#include <Windows.h>
#pragma comment(lib,"ws2_32.lib")
int main() {
char server_reply[64];
int recv_size;
u_short status_bits = 0;
bool status_pass = false;
bool status_fail = false;
bool status_results_valid = false;
bool status_automatic_mode = false;
bool status_heartbeat = false;
bool status_repairable = false;
bool status_repair_gun = false;
u_short failure_code = 0;
// Startup Winsock2
WSADATA wsa;
// "MAKEWORD(2,2)" represents a minimum Winsock version number.
// "wsa" will contain info on Winsock, but it is not used in the example below.
WSAStartup(MAKEWORD(2,2), &wsa);
// Connect to Server
// IP address of the Coherix sensor
char server_address[] = "192.168.10.140";
// Port used for P3DLink
u_short server_port = 12345;
struct sockaddr_in server;
// "AF_INET" indicates the server will use Internet Protocol version 4 (IPv4).
server.sin_family = AF_INET;
// "inet_addr" converts ip address from string to long.
server.sin_addr.s_addr = inet_addr(server_address);
// "htons" sets correct endianness.
server.sin_port = htons(server_port);
// Define output_message
#define OUTPUT_MESSAGE_WORD_COUNT 10 // number of 16-bit words
#define OUTPUT_MESSAGE_BYTE_COUNT (2*OUTPUT_MESSAGE_WORD_COUNT)
u_short output_message[OUTPUT_MESSAGE_WORD_COUNT];
// Loop forever
while(true) {
} // End of loop
closesocket(s);
WSACleanup();
}
When you run the program, you should see an output like the following:
Testing
To test the communication, go to File Digital I/O. Use the dialog boxes shown below to view Coherix
input data and send Coherix output data.