Skip to content

Instantly share code, notes, and snippets.

@peterneorr
Created October 26, 2022 15:57
Show Gist options
  • Save peterneorr/bc4a44df88b43b7f9bd9045d2361aa86 to your computer and use it in GitHub Desktop.
Save peterneorr/bc4a44df88b43b7f9bd9045d2361aa86 to your computer and use it in GitHub Desktop.
test program for BNC 575
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Client
{
/// <summary>
/// Simplest possible socket client to connect and send a SCPI command to
/// an ethernet device
/// </summary>
class Program
{
static void Main(string[] args)
{
ExecuteClient();
}
static void ExecuteClient()
{
try
{
//IP address of our pulse generator
string IP = "192.168.1.124";
string Port = "2101";
IPAddress ipAddr = IPAddress.Parse(IP);
IPEndPoint remoteEndPoint = new IPEndPoint(ipAddr, int.Parse(Port));
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//sender.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
//var localEndPoint = new IPEndPoint(IPAddress.Any, TCPLocalPort);
//sender.Bind(localEndPoint); // bind() is for network _servers_ not clients
//Console.WriteLine("Socket bound to local endpoint -> {0} ", sender.LocalEndPoint.ToString());
// Connect Socket to the remote endpoint using method Connect()
socket.Connect(remoteEndPoint);
// We print EndPoint information once we are connected
Console.WriteLine("Socket connected to -> {0} ", socket.RemoteEndPoint.ToString());
// Send a message to ther server
// This SCPI command create a response
string message = ":PULSE1:STATE:ON\r\n";
Console.Write($"Sending: {message}");
byte[] messageSent = Encoding.ASCII.GetBytes(message);
int byteSent = socket.Send(messageSent);
//sender.SendTo(messageSent, messageSent.Length, SocketFlags.None, endpoint);
// Data buffer
byte[] buffer = new byte[1024];
// This call should block until there's data to read from the socket.
int byteRecv = socket.Receive(buffer);
string asString = Encoding.ASCII.GetString(buffer, 0, byteRecv);
Console.WriteLine($"Message from Server -> {asString}");
// Close Socket
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment