Untitled
unknown
plain_text
a year ago
17 kB
11
Indexable
Imports System.ComponentModel Imports System.IO Imports System.Runtime.InteropServices Imports System.Text Public Class Form1 Public Const SENSOR3D_OK As Integer = 0 Public Const SENSOR3D_INVALIDSENSORHANDLE As Integer = -1 Public Const SENSOR3D_SENSORNOTCONNECTED As Integer = -2 Public Const SENSOR3D_TIMEOUT As Integer = -3 Public Const SENSOR3D_CONFIGURATIONERROR As Integer = -4 Public Const SENSOR3D_ARGUMENTNULLPOINTER As Integer = -101 Public Const SENSOR3D_ARGUMENTOUTOFRANGE As Integer = -102 Public Const SENSOR3D_RETURNBUFFERTOOSMALL As Integer = -103 Public Const SENSOR3D_COMMANDNOTFOUND As Integer = -104 Public Const SENSOR3D_SOCKETCOMMUNICATIONERROR As Integer = -106 Public Const SENSOR3D_BUSY As Integer = -201 Public Const SENSOR3D_DATASTREAMERROR As Integer = -301 ' Sensor status Public Const SENSOR_CONNECTED As Integer = &H1 Public Const SENSOR_ACQUISITION_STARTED As Integer = &H4 Public Const SENSOR_OVERHEATED As Integer = &H8 ' Point structs <StructLayout(LayoutKind.Sequential)> Public Structure POINT3D Public x As Double Public y As Double Public z As Double End Structure <StructLayout(LayoutKind.Sequential)> Public Structure ROI Public offset_x As UInteger Public offset_y As UInteger Public width As UInteger Public height As UInteger End Structure <StructLayout(LayoutKind.Sequential)> Public Structure POINT_CLOUD Public point As IntPtr ' Pointer to POINT3D array Public intensity As IntPtr ' Pointer to UShort array Public confidence As IntPtr ' Pointer to UShort array End Structure ' DLL fonksiyonlarını tanımlama <DllImport("Sensor3d.dll", CallingConvention:=CallingConvention.Cdecl)> Public Shared Function Sensor3D_Connect(ByVal ip As String, ByVal timeout As Integer) As IntPtr End Function <DllImport("Sensor3d.dll", CallingConvention:=CallingConvention.Cdecl)> Public Shared Function Sensor3D_Disconnect(ByVal sensor As IntPtr) As Integer End Function <DllImport("Sensor3d.dll", CallingConvention:=CallingConvention.Cdecl)> Public Shared Function Sensor3D_GetSensorStatus(ByVal sensor As IntPtr, ByRef status As Integer) As Integer End Function <DllImport("Sensor3d.dll", CallingConvention:=CallingConvention.Cdecl)> Public Shared Function Sensor3D_GetVersion(ByVal buffer As StringBuilder, ByVal size As Integer) As Integer End Function <DllImport("Sensor3d.dll", CallingConvention:=CallingConvention.Cdecl)> Public Shared Function Sensor3D_ReadData(ByVal sensor As IntPtr, ByVal feature_name As String, ByVal buffer As StringBuilder, ByVal size As Integer, ByVal reserved As Integer) As Integer End Function <DllImport("Sensor3d.dll", CallingConvention:=CallingConvention.Cdecl)> Public Shared Function Sensor3D_WriteData(ByVal sensor As IntPtr, ByVal buffer As String) As Integer End Function <DllImport("Sensor3d.dll", CallingConvention:=CallingConvention.Cdecl)> Public Shared Function Sensor3D_GetPointCloud(ByVal sensor As IntPtr, ByRef buffer As POINT_CLOUD, ByVal buffer_size As Integer, ByRef number_of_points As Integer, ByVal roi As IntPtr, ByVal timeout As Integer) As Integer End Function Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' BackgroundWorker ayarları bgWorker.WorkerReportsProgress = True bgWorker.WorkerSupportsCancellation = True End Sub Private Sub btnAcquire_Click(sender As Object, e As EventArgs) Handles btnAcquision.Click If Not bgWorker.IsBusy Then ' ListBox temizleme ve ilk mesaj ekleme stepListBox.Items.Clear() stepListBox.Items.Add("Sensöre bağlanılıyor...") ' BackgroundWorker başlatma bgWorker.RunWorkerAsync() End If End Sub Private Sub bgWorker_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgWorker.DoWork Dim ip As String = "192.168.100.1" Dim readBuffer As New StringBuilder(&HFFFF) Dim nrScans As Integer = 2 ' Sensöre bağlanma Dim sensorHandle As IntPtr = Sensor3D_Connect(ip, 32001) If sensorHandle = IntPtr.Zero Then bgWorker.ReportProgress(0, "Sensöre bağlanılamadı.") bgWorker.ReportProgress(100, False) Return End If bgWorker.ReportProgress(0, "Sensöre bağlandı. Sensör durumu kontrol ediliyor...") Dim sensorHandlestatus As Integer = 0 If Sensor3D_GetSensorStatus(sensorHandle, sensorHandlestatus) = 0 Then ' 0 == SENSOR3D_OK If (sensorHandlestatus And SENSOR_CONNECTED) <> 0 Then ' 1 == SENSOR_CONNECTED bgWorker.ReportProgress(0, "Bağlantı kuruldu!") bgWorker.ReportProgress(100, True) Dim camera_width As Integer Dim camera_height As Integer ' DLL sürüm numarası okunuyor bgWorker.ReportProgress(0, "DLL sürüm numarası okunuyor...") Dim result As Integer = Sensor3D_GetVersion(readBuffer, readBuffer.Capacity) bgWorker.ReportProgress(0, "DLL sürümü: " & readBuffer.ToString()) ' Piksel sayısı (X) okunuyor readBuffer.Clear() bgWorker.ReportProgress(0, "Piksel sayısı (X) okunuyor...") result = Sensor3D_ReadData(sensorHandle, "GetPixelXMax", readBuffer, readBuffer.Capacity, 0) If result <> 0 Then ' 0 == SENSOR3D_OK bgWorker.ReportProgress(0, "GetPixelXMax okunurken hata oluştu.") CleanupAndDisconnect(sensorHandle) bgWorker.ReportProgress(100, False) Return End If camera_width = Integer.Parse(readBuffer.ToString()) bgWorker.ReportProgress(0, "Kamera Genişliği: " & camera_width) readBuffer.Clear() bgWorker.ReportProgress(0, "Piksel sayısı (Y) okunuyor...") result = Sensor3D_ReadData(sensorHandle, "GetPixelYMax", readBuffer, readBuffer.Capacity, 0) If result <> 0 Then bgWorker.ReportProgress(0, "GetPixelYMax okunurken hata oluştu.") CleanupAndDisconnect(sensorHandle) bgWorker.ReportProgress(100, False) Return End If camera_height = Integer.Parse(readBuffer.ToString()) bgWorker.ReportProgress(0, "Kamera Yüksekliği: " & camera_height) ' Kazanç 40 olarak ayarlanıyor bgWorker.ReportProgress(0, "Kazanç 40 olarak ayarlanıyor...") result = Sensor3D_WriteData(sensorHandle, "SetGain=40" & vbCr) If result <> 0 Then bgWorker.ReportProgress(0, "Kazanç ayarlanırken hata oluştu.") CleanupAndDisconnect(sensorHandle) bgWorker.ReportProgress(100, False) Return End If '' Pozlama süresi ayarlanıyor 'bgWorker.ReportProgress(0, "Pozlama süresi ayarlanıyor...") 'result = Sensor3D_WriteData(sensorHandle, "SetExposure=29900" & vbCr) ' Örneğin 29900 µs 'If result <> SENSOR3D_OK Then ' bgWorker.ReportProgress(0, "Pozlama süresi ayarlanırken hata oluştu.") ' CleanupAndDisconnect(sensorHandle) ' bgWorker.ReportProgress(100, False) ' Return 'End If ' Pozlama süresi ayarlanıyor bgWorker.ReportProgress(0, "Pozlama süresi ayarlanıyor...") result = Sensor3D_WriteData(sensorHandle, "SetExposureTime=10000" & vbCr) ' Örneğin 29900 µs If result <> SENSOR3D_OK Then bgWorker.ReportProgress(0, "Pozlama süresi ayarlanırken hata oluştu.") CleanupAndDisconnect(sensorHandle) bgWorker.ReportProgress(100, False) Return End If bgWorker.ReportProgress(0, "Pozlama süresi ayarlanıyor...") result = Sensor3D_WriteData(sensorHandle, "SetExposureTimeLimit=10100" & vbCr) ' Örneğin 29900 µs If result <> SENSOR3D_OK Then bgWorker.ReportProgress(0, "Pozlama süresi ayarlanırken hata oluştu.") CleanupAndDisconnect(sensorHandle) bgWorker.ReportProgress(100, False) Return End If ' Tamponlar başlatılıyor bgWorker.ReportProgress(0, "Tamponlar başlatılıyor...") Dim nrPixels As Integer = camera_width * camera_height Dim pointCloudBuffer As New POINT_CLOUD() ' POINT3D dizisi için bellek ayırma pointCloudBuffer.point = Marshal.AllocHGlobal(Marshal.SizeOf(Of POINT3D)() * nrPixels) ' Intensity dizisi için bellek ayırma pointCloudBuffer.intensity = Marshal.AllocHGlobal(2 * nrPixels) ' UShort 2 byte ' Confidence dizisi için bellek ayırma pointCloudBuffer.confidence = Marshal.AllocHGlobal(2 * nrPixels) ' UShort 2 byte Dim buffer_size As Integer = (Marshal.SizeOf(Of POINT3D)() + 2 + 2) * nrPixels ' 2 byte for intensity, 2 byte for confidence bgWorker.ReportProgress(0, "Piksel Sayısı: " & nrPixels) Try ' Sensör modu Nokta Bulutu olarak ayarlanıyor bgWorker.ReportProgress(0, "Sensör modu Nokta Bulutu olarak ayarlanıyor...") result = Sensor3D_WriteData(sensorHandle, "SetSensorMode=4" & vbCr) If result <> 0 Then bgWorker.ReportProgress(0, "Sensör modu ayarlanırken hata oluştu.") CleanupAndDisconnect(sensorHandle) bgWorker.ReportProgress(100, False) Return End If ' Tetikleme kaynağı yazılım tetikleyici olarak ayarlanıyor bgWorker.ReportProgress(0, "Tetikleme kaynağı yazılım tetikleyici olarak ayarlanıyor...") result = Sensor3D_WriteData(sensorHandle, "SetTriggerSource=0" & vbCr) If result <> 0 Then bgWorker.ReportProgress(0, "Tetikleme kaynağı ayarlanırken hata oluştu.") CleanupAndDisconnect(sensorHandle) bgWorker.ReportProgress(100, False) Return End If ' LED modeli 28Patterns olarak ayarlanıyor bgWorker.ReportProgress(0, "LED modeli 28Patterns olarak ayarlanıyor...") result = Sensor3D_WriteData(sensorHandle, "SetLEDPattern=16" & vbCr) If result <> 0 Then bgWorker.ReportProgress(0, "LED modeli ayarlanırken hata oluştu.") CleanupAndDisconnect(sensorHandle) bgWorker.ReportProgress(100, False) Return End If ' Veri alımı başlatılıyor bgWorker.ReportProgress(0, "Veri alımı başlatılıyor...") result = Sensor3D_WriteData(sensorHandle, "SetAcquisitionStart" & vbCr) If result <> 0 Then bgWorker.ReportProgress(0, "Veri alımı başlatılırken hata oluştu.") CleanupAndDisconnect(sensorHandle) bgWorker.ReportProgress(100, False) Return End If Dim number_of_points As Integer = 0 ' Nokta bulutları alınıyor bgWorker.ReportProgress(0, "Nokta bulutları alınıyor...") result = Sensor3D_GetPointCloud(sensorHandle, pointCloudBuffer, buffer_size, number_of_points, IntPtr.Zero, 10000) ' 10 saniye timeout If result = SENSOR3D_OK Then bgWorker.ReportProgress(0, $"Nokta bulutu başarıyla alındı. Nokta Sayısı: {number_of_points}") Using writer As New StreamWriter("point_cloud_data.txt") For i As Integer = 0 To number_of_points - 1 Dim point As POINT3D = Marshal.PtrToStructure(Of POINT3D)(IntPtr.Add(pointCloudBuffer.point, i * Marshal.SizeOf(Of POINT3D)())) 'Dim intensity As UShort = Marshal.ReadInt16(IntPtr.Add(pointCloudBuffer.intensity, i * 2)) 'Dim confidence As UShort = Marshal.ReadInt16(IntPtr.Add(pointCloudBuffer.confidence, i * 2)) writer.WriteLine($"Point {i}: X={point.x:F3}, Y={point.y:F3}, Z={point.z:F3} ") ' İlk 10 noktayı ekrana yazdır If i < 10 Then bgWorker.ReportProgress(0, $"Point {i}: X={point.x:F3}, Y={point.y:F3}, Z={point.z:F3}") End If Next End Using ElseIf result = SENSOR3D_TIMEOUT Then bgWorker.ReportProgress(0, "Nokta bulutu okunurken zaman aşımı. Sonuç: " & result) Else bgWorker.ReportProgress(0, "Nokta bulutu okunurken hata oluştu. Sonuç: " & result) End If ' Veri alımını durdurmak için komut gönder If Sensor3D_WriteData(sensorHandle, "SetAcquisitionStop" & vbCr) <> 0 Then bgWorker.ReportProgress(0, "Veri alımı durdurulurken hata oluştu.") End If Finally ' Belleği temizle Marshal.FreeHGlobal(pointCloudBuffer.point) Marshal.FreeHGlobal(pointCloudBuffer.intensity) Marshal.FreeHGlobal(pointCloudBuffer.confidence) CleanupAndDisconnect(sensorHandle) bgWorker.ReportProgress(0, "Temizlik ve bağlantı kesme işlemi tamamlandı.") End Try Else bgWorker.ReportProgress(0, "Sensör bağlı değil.") Sensor3D_Disconnect(sensorHandle) bgWorker.ReportProgress(100, False) End If Else bgWorker.ReportProgress(0, "Sensör durumu alınamadı.") Sensor3D_Disconnect(sensorHandle) bgWorker.ReportProgress(100, False) End If End Sub Private Sub bgWorker_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles bgWorker.ProgressChanged If TypeOf e.UserState Is String Then AddToListBox(CType(e.UserState, String)) ElseIf TypeOf e.UserState Is Boolean Then UpdateLedStatus(CType(e.UserState, Boolean)) End If End Sub Private Sub bgWorker_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bgWorker.RunWorkerCompleted If e.Error IsNot Nothing Then AddToListBox("İşlem sırasında hata oluştu: " & e.Error.Message) Else AddToListBox("İşlem tamamlandı.") End If End Sub Sub UpdateLedStatus(isConnected As Boolean) If isConnected Then ledStatus.BackColor = Color.Green AddToListBox("LED durumu: Bağlı (Yeşil)") Else ledStatus.BackColor = Color.Gray AddToListBox("LED durumu: Bağlı değil (Gri)") End If End Sub Sub CleanupAndDisconnect(ByRef sensorHandle As IntPtr) Sensor3D_Disconnect(sensorHandle) AddToListBox("Sensör bağlantısı kesildi.") End Sub Private Sub AddToListBox(message As String) If stepListBox.InvokeRequired Then stepListBox.Invoke(Sub() stepListBox.Items.Add(message)) Else stepListBox.Items.Add(message) End If End Sub End Class
Editor is loading...
Leave a Comment