Skip to content

Instantly share code, notes, and snippets.

@schnello

schnello/cs Secret

Created November 1, 2023 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schnello/815085ad13457da7b6f3f17a46e96d78 to your computer and use it in GitHub Desktop.
Save schnello/815085ad13457da7b6f3f17a46e96d78 to your computer and use it in GitHub Desktop.
Demo C Code
/* ************************************************************************************ */
/*
Name: ViewerDemoForm.cs / "Get image" request example (ethernet port 2006)
Description:
Example program to demonstrate ethernet communication with a VISOR vision sensor
on ethernet.
Example program sending a "TRG" trigger request to the camera and receiving an image using the GIM0 request
Documents:
[1] VISOR Mounting and operating instructions (pdf).
Date: (c) 2015
Version: 1.1.0
Author: (c) Sensopart GmbH
History:
Notes:
Information on necessary camera settings:
* Image recorder has to been activated in job setting (e.g. with setting = "All images").
* Ethernet Interface has to be activated
* Ethernet Communication has to be set to ASCII
* Job has to be set to triggered
------------------------------------------------------------------------
Restricted rights legend.
Use, duplication or disclosure is subject to restrictions.
SensoPart - Industriesensorik GmbH
Am Wiedenbach 1
D-79695 Wieden
------------------------------------------------------------------------
SensoPart Confidential © Copyright 2015 SensoPart Industriesensorik GmbH
------------------------------------------------------------------------
*/
/* ************************************************************************************ */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading;
namespace clientTcp
{
public partial class ViewerForm : Form
{
Thread thread = null;
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
NetworkStream serverStream;
Bitmap imgBmp;
String errorText;
Boolean softAbort;
public ViewerForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void buttonConnect_Click(object sender, EventArgs e)
{
buttonConnect.Enabled = false;
try
{
clientSocket.Connect(textBoxHost.Text, 2006);
serverStream = clientSocket.GetStream();
textBoxHost.ReadOnly = true;
button1.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
buttonConnect.Enabled = true;
}
}
public void WorkThreadFunction()
{
try
{
serverStream.ReadTimeout = 1000;
softAbort = false;
while (true)
{
byte[] outStream;
long zeit = DateTime.Now.Ticks;
int total = 0;
byte[] inStream = new byte[clientSocket.ReceiveBufferSize];
int n;
outStream = System.Text.Encoding.ASCII.GetBytes("TRG");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
n = serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream).Replace("\0", "");
string result;
byte[] readBuffer = new byte[clientSocket.ReceiveBufferSize];
int trys = 20;
do
{
System.Threading.Thread.Sleep(2);
outStream = System.Text.Encoding.ASCII.GetBytes("GIM0");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
System.Threading.Thread.Sleep(1);
n = serverStream.Read(readBuffer, 0, 15);
result = Encoding.UTF8.GetString(readBuffer);
trys--;
}
while (result.Substring(0, 5) == "GIMF2" && (trys > 0));
if (result.Substring(0, 5) == "GIMF1")
{
errorText = "error: GIM0 is sending GIMF1";
this.Invoke(new MethodInvoker(updateError));
break;
}
zeit = DateTime.Now.Ticks;
int row = Convert.ToInt32(result.Substring(7, 4));
int col = Convert.ToInt32(result.Substring(11, 4));
int byteCount = row * col;
MemoryStream imgStream = new MemoryStream();
try
{
do
{
n = serverStream.Read(readBuffer, 0, readBuffer.Length);
if (n <= 0)
{
break;
}
imgStream.Write(readBuffer, 0, n);
total += n;
//System.Threading.Thread.Sleep(1);
} while (total < byteCount);
}
catch (Exception ex)
{
errorText = ex.Message;
Console.WriteLine(ex.Message);
this.Invoke(new MethodInvoker(updateError));
break;
}
drawBmp(imgStream, col, row);
this.Invoke(new MethodInvoker(updateImg));
if (softAbort)
break;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
errorText = ex.Message;
try
{
this.Invoke(new MethodInvoker(updateError));
}
catch (Exception) { }
}
}
private void updateError()
{
MessageBox.Show(errorText,this.Text);
softAbort = true;
while (thread.ThreadState == ThreadState.Running)
{
System.Threading.Thread.Sleep(5);
}
thread = null;
button1.Text = "Start";
}
private void updateImg()
{
pictureBox1.Image = imgBmp;
}
private void drawBmp(MemoryStream imgStream, int bmpWidth,int bmpHeight)
{
imgBmp = new Bitmap(bmpWidth, bmpHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
ColorPalette BitmapPalette = imgBmp.Palette;
for (int i = 0; i < 256 - 1; i++ )
BitmapPalette.Entries[i] = Color.FromArgb(i, i, i);
imgBmp.Palette = BitmapPalette;
imgStream.Position = 0;
BitmapData bmpData = imgBmp.LockBits(
new Rectangle(0, 0, bmpWidth, bmpHeight),
ImageLockMode.WriteOnly, imgBmp.PixelFormat);
//Copy the data from the byte array into BitmapData.Scan0
Marshal.Copy(imgStream.ToArray(), 0, bmpData.Scan0, imgStream.ToArray().Length);
//Unlock the pixels
imgBmp.UnlockBits(bmpData);
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
if (thread == null)
{
softAbort = false;
thread = new Thread(new ThreadStart(WorkThreadFunction));
thread.Start();
button1.Text = "Stop";
}
else
{
softAbort = true;
while (thread.ThreadState == ThreadState.Running)
{
System.Threading.Thread.Sleep(5);
}
thread = null;
button1.Text = "Start Live Image";
}
button1.Enabled = true;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (thread != null)
if (thread.IsAlive)
{
thread.Abort();
}
}
private void textBoxHost_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment