Page 1 of 1

Socket Communication Setup

Posted: Fri Feb 01, 2019 6:02 pm
by samir13k
Hello,

I'm trying to write a fairly simple program, I just want to press one button and have it send out a string (in this case "power on") to a universal robot using socket communications.

My robot IP address is 192.168.1.10
Socket I would like to communicate to is 29999
Script to send is "Power On"

Does anyone have any advice as to where to get started? I'm assuming that I will have to open a socket in C# in the backend of the beijer, however I'm lacking direction... Can anyone point me to a program using socket communications in the Beijer? It seems like most code I see makes this far more complicated than it has to be. Your help is appreciated in advance!

Thanks,
Samir

Re: Socket Communication Setup

Posted: Mon Dec 23, 2019 5:30 pm
by Chris T.
Hi Samir13k,
Here is an example script you can use to create a socket and send data at an interval:

**************************************************************

//--------------------------------------------------------------
// Press F1 to get help about using script.
// To access an object that is not located in the current class, start the call with Globals.
// When using events and timers be cautious not to generate memoryleaks,
// please see the help for more information.
//---------------------------------------------------------------

namespace Neo.ApplicationFramework.Generated
{
using System.Windows.Forms;
using System;
using System.Drawing;
using Neo.ApplicationFramework.Tools;
using Neo.ApplicationFramework.Common.Graphics.Logic;
using Neo.ApplicationFramework.Controls;
using Neo.ApplicationFramework.Interfaces;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Text;


public partial class Screen1
{

static System.Windows.Forms.Timer mytimer = new System.Windows.Forms.Timer();
static Thread t;

void Screen1_Opened(System.Object sender, System.EventArgs e)
{
//t.IsBackground = true;
mytimer.Tick += Run_Timer;
mytimer.Interval = 40;
mytimer.Enabled = true;
}

private void Run_Timer(System.Object sender, System.EventArgs e)
{
// MessageBox.Show("Test");
t = new Thread(new ThreadStart(Connect));
t.Start();

//MessageBox.Show("Test");
}


private void Connect()
{
//MessageBox.Show("Test2");

try {


// Establish the remote endpoint
// for the socket. This example
// uses port 11111 on the local
// computer.
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = IPAddress.Parse("192.168.1.112");

IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 3000);

// Creation TCP/IP Socket using
// Socket Class Costructor
Socket sendersock = new Socket(ipAddr.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);

try {

// Connect Socket to the remote
// endpoint using method Connect()
sendersock.Connect(localEndPoint);

// We print EndPoint information
// that we are connected


//Console.WriteLine("Socket connected to -> {0} ",
// sender.RemoteEndPoint.ToString());

// Creation of messagge that
// we will send to Server
byte[] messageSent = Encoding.ASCII.GetBytes("This is the data that gets sent<EOF>");
int byteSent = sendersock.Send(messageSent);

// Data buffer
byte[] messageReceived = new byte[1024];

// We receive the messagge using
// the method Receive(). This
// method returns number of bytes
// received, that we'll use to
// convert them to string
int byteRecv = sendersock.Receive(messageReceived);
Console.WriteLine("Message from Server -> {0}",
Encoding.ASCII.GetString(messageReceived,
0, byteRecv));

// Close Socket using
// the method Close()
sendersock.Shutdown(SocketShutdown.Both);
sendersock.Close();
t.Abort();
}

// Manage of Socket's Exceptions
catch (ArgumentNullException ane) {

//MessageBox.Show("ArgumentNullException : {0}", ane.ToString());

}

catch (SocketException se) {

//MessageBox.Show("SocketException : {0}", se.ToString());

}

catch (Exception e) {
//MessageBox.Show("Unexpected exception : {0}", e.ToString());

}
}

catch (Exception e) {

//MessageBox.Show(e.ToString());

}

/*
All examples in this support document are only intended to improve understanding of the functionality and handling of the equipment.
Beijer Electronics AB cannot assume any liability if these examples are used in real applications. In view of the wide range of applications for this equipment, users must acquire sufficient knowledge themselves in order to ensure that it is correctly used in their specific application.
Persons responsible for the application and the equipment must themselves ensure that each application is in compliance with all relevant requirements, standards and legislation in respect to configuration and safety.
Beijer Electronics AB will accept no liability for any damage incurred during the installation or use of this equipment.
Beijer Electronics AB prohibits all modification, changes or conversion of the equipment.
*/