Przerobienie programu na bibliotekę

0

Witam. Nie potrafię c++ i nauka jego mi słabo wchodzi, natomiast wystarczająco jak dla mnie radzę sobie w Delphi. Mam w c++ program do sterowania serwokontrolerem od producenta.
Czy trudne by było przerobienie go na bilbiotekę tak bym mógł to załadować w Delphi? Jest tam funkcja TrySetTarget i chciałbym bym mógł używać jej w delphi za pomocą .dll i inne funkcje z tego programu jak np. łączenie.

Czy ktoś mógłby to tak przerobić? Bardzo to dla mnie ważne bo serwokontroler mnie trochę kosztował a nie umiem nawet samemu z niego korzystać :/

 /*  MaestroEasyExampleCpp:
 *    Simple example GUI for the Maestro USB Servo Controller, written in
 *    Visual C++.
 *    
 *    Features:
 *       Temporary native USB connection using Usc class
 *       Button for disabling channel 0.
 *       Button for setting target of channel 0 to 1000 us.
 *       Button for setting target of channel 0 to 2000 us.
 * 
 *  NOTE: Channel 0 should be configured as a servo channel for this program
 *  to work.  You must also connect USB and servo power, and connect a servo
 *  to channel 0.  If this program does not work, use the Maestro Control
 *  Center to check what errors are occurring.
 */

#pragma once

namespace Pololu {
namespace Usc {
namespace MaestroEasyExampleCpp {

	using namespace Pololu::UsbWrapper;
	using namespace Pololu::Usc;

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Collections::Generic;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Text;

	/// <summary>
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class MainWindow : public System::Windows::Forms::Form
	{
	public:
		MainWindow(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MainWindow()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  Button2000;
	protected: 
	private: System::Windows::Forms::Button^  Button1000;
	private: System::Windows::Forms::Label^  ChannelLabel;
	private: System::Windows::Forms::Button^  ButtonDisable;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->Button2000 = (gcnew System::Windows::Forms::Button());
			this->Button1000 = (gcnew System::Windows::Forms::Button());
			this->ChannelLabel = (gcnew System::Windows::Forms::Label());
			this->ButtonDisable = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// Button2000
			// 
			this->Button2000->Location = System::Drawing::Point(302, 25);
			this->Button2000->Name = L"Button2000";
			this->Button2000->Size = System::Drawing::Size(118, 23);
			this->Button2000->TabIndex = 7;
			this->Button2000->Text = L"Target=&2000us";
			this->Button2000->UseVisualStyleBackColor = true;
			this->Button2000->Click += gcnew System::EventHandler(this, &MainWindow::Button2000_Click);
			// 
			// Button1000
			// 
			this->Button1000->Location = System::Drawing::Point(178, 25);
			this->Button1000->Name = L"Button1000";
			this->Button1000->Size = System::Drawing::Size(118, 23);
			this->Button1000->TabIndex = 6;
			this->Button1000->Text = L"Target=&1000us";
			this->Button1000->UseVisualStyleBackColor = true;
			this->Button1000->Click += gcnew System::EventHandler(this, &MainWindow::Button1000_Click);
			// 
			// ChannelLabel
			// 
			this->ChannelLabel->AutoSize = true;
			this->ChannelLabel->Location = System::Drawing::Point(12, 30);
			this->ChannelLabel->Name = L"ChannelLabel";
			this->ChannelLabel->Size = System::Drawing::Size(58, 13);
			this->ChannelLabel->TabIndex = 5;
			this->ChannelLabel->Text = L"Channel 0:";
			// 
			// ButtonDisable
			// 
			this->ButtonDisable->Location = System::Drawing::Point(92, 25);
			this->ButtonDisable->Name = L"ButtonDisable";
			this->ButtonDisable->Size = System::Drawing::Size(80, 23);
			this->ButtonDisable->TabIndex = 4;
			this->ButtonDisable->Text = L"&Disable";
			this->ButtonDisable->UseVisualStyleBackColor = true;
			this->ButtonDisable->Click += gcnew System::EventHandler(this, &MainWindow::ButtonDisable_Click);
			// 
			// MainWindow
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(453, 75);
			this->Controls->Add(this->Button2000);
			this->Controls->Add(this->Button1000);
			this->Controls->Add(this->ChannelLabel);
			this->Controls->Add(this->ButtonDisable);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
			this->Name = L"MainWindow";
			this->Text = L"MaestroEasyExample in C++";
			this->Load += gcnew System::EventHandler(this, &MainWindow::MainWindow_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

        /// <summary>
        /// This functions runs when the user clicks the Target=1000us button.
        /// </summary>
        Void Button1000_Click(Object^ sender, EventArgs^ e)
        {
            TrySetTarget(0, 1000 * 4);  // Set the target of channel 0 to 1000 microseconds.
        }

        /// <summary>
        /// This functions runs when the user clicks the Target=2000us button.
        /// </summary>
        Void Button2000_Click(Object^ sender, EventArgs^ e)
        {
            TrySetTarget(0, 2000 * 4);  // Set the target of channel 0 to 2000 microseconds.
        }

        /// <summary>
        /// This function runs when the user clicks the Disable button.
        /// </summary>
        Void ButtonDisable_Click(Object^ sender, EventArgs^ e)
        {
            // Set target of channel 0 to 0.  This tells the Maestro to stop
            // transmitting pulses on that channel.  Any servo connected to it
            // should stop trying to maintain its position.
            TrySetTarget(0, 0);
        }
        
        /// <summary>
        /// Attempts to set the target (width of pulses sent) of a channel.
        /// </summary>
        /// <param name="channel">Channel number from 0 to 23.</param>
        /// <param name="target">
        ///   Target, in units of quarter microseconds.  For typical servos,
        ///   6000 is neutral and the acceptable range is 4000-8000.
        /// </param>
        Void TrySetTarget(Byte channel, UInt16 target)
        {
			Usc^ device;
            try
            {
                device = connectToDevice();           // Find a device and connect.
                device->setTarget(channel, target);
            }
            catch (Exception^ exception)  // Handle exceptions by displaying them to the user.
            {
                displayException(exception);
            }
			finally  // Do this no matter what.
			{
				delete device;  // Close the connection so other processes can use the device.
			}
        }

        /// <summary>
        /// Connects to a Maestro using native USB and returns the Usc object
        /// representing that connection.  When you are done with the
        /// connection, you should delete it using the "delete" statement so
		/// that other processes or functions can connect to the device later.
        /// </summary>
        Usc^ connectToDevice()
        {
            // Get a list of all connected devices of this type.
			List<DeviceListItem^>^ connectedDevices = Usc::getConnectedDevices();

            for each (DeviceListItem^ dli in connectedDevices)
            {
                // If you have multiple devices connected and want to select a particular
                // device by serial number, you could simply add a line like this:
                //   if (dli.serialNumber != "00012345"){ continue; }

                Usc^ device = gcnew Usc(dli); // Connect to the device.
                return device;             // Return the device.
            }
            throw gcnew Exception("Could not find device.  Make sure it is plugged in to USB " +
                "and check your Device Manager (Windows) or run lsusb (Linux).");
        }

        /// <summary>
        /// Displays an exception to the user by popping up a message box.
        /// </summary>
        void displayException(Exception^ exception)
        {
            StringBuilder^ stringBuilder = gcnew StringBuilder();
            do
            {
                stringBuilder->Append(exception->Message + "  ");
                if (exception->GetType() == Win32Exception::typeid)
                {
                    stringBuilder->Append("Error code 0x" + ((Win32Exception^)exception)->NativeErrorCode.ToString("x") + ".  ");
                }
                exception = exception->InnerException;
            }
            while (exception != nullptr);
			MessageBox::Show(stringBuilder->ToString(), this->Text, MessageBoxButtons::OK, MessageBoxIcon::Error);
        }

	private: System::Void MainWindow_Load(System::Object^  sender, System::EventArgs^  e) {
			 }
}; // end class

}}} // end namespaces

Program nie jest trudny bo to w dużej mierze komentarze, no ale ja i tak go nie rozumiem. Bardzo proszę o pomoc

0

Tak krótki kod szybciej przepiszesz na Delphi.

0

Nie jestem pewien. Chodzi o to, czy da się w projekcie delphi używać dll z .NET (uprzedzam, że ja tego nie wiem). Wątek z http://forum.pololu.com/viewtopic.php?t=2568 potwierdza, że ktoś już próbował czegoś podobnego, tylko w natywnym C++. Poza tym ten kawałek kodu to tylko malutka część (GUI) i zasadniczy kod komunikacji z urządzeniem jest w przestrzeni Pololu::Usc i Pololu::UsbWrapper.

0

Tak da się.

0

Co do przepisania - nie potrafię. Nie radzę sobie z tak skomplikowanymi aplikacjami, i by przepisać muszę znać trochę c++.

Co do tego kodu to musi umieć korzystać z bilbiotek, bo to kod od samego producenta a nie tylko użytkownika. Jest też na C# i VB ale nie Delphi. Nie potrafię w Delphi użyć usc.dll tak samo jak w c++ jest to robione, dlatego chcę okrężną drogą zrobić z tego c++ bilbiotekę która z kolei korzysta bilbiotek pololu.

0

Hmm, delphi znam kiepsko, tym bardziej nigdy nie używałem z .NET, ale wujek google po wpisaniu "using .net dll in delphi project", na pierwszym miejscu podaje link http://stackoverflow.com/questions/2132874/delphi-7-using-a-dll-that-is-created-by-vb-net

Próbowałes czegoś podobnego?
Gdybym ja miał się za to zabrać, to bym sobie podarował ten kawalek kodu GUI, który na początku wkleiłeś i jeśli powyższy sposób by działał, to napisałbym sobie swoje GUI (wg własnych potrzeb) w delphi i byłoby po problemie.

0
Tomek napisał(a)

Hmm, delphi znam kiepsko, tym bardziej nigdy nie używałem z .NET, ale wujek google po wpisaniu "using .net dll in delphi project", na pierwszym miejscu podaje link http://stackoverflow.com/questions/2132874/delphi-7-using-a-dll-that-is-created-by-vb-net

Próbowałes czegoś podobnego?
Gdybym ja miał się za to zabrać, to bym sobie podarował ten kawalek kodu GUI, który na początku wkleiłeś i jeśli powyższy sposób by działał, to napisałbym sobie swoje GUI (wg własnych potrzeb) w delphi i byłoby po problemie.

Czytałem to i jest o uruchomieniu jakiegoś regasm w visual studio. Nie mam pojęcia co to bo po prostu nie znam się na tym kompilatorze.

plik regasm.exe znalazłem na dysku i chciałem odpalić by wpisać to polecenie, ale nie idzie, konsola mi się od razu wyłącza, jest tylko na ułamek sekundy z tekstem i znika, mam windows 7

0

To część .NET, u mnie jest w c:\Windows\Microsoft.NET\Framework%WERSJA%

Sprawdź u siebie. Jeśli masz, to w zasadzie rozwiązanie (wg podanego linka) masz na tacy.

0

Tak mam, konsola nie chce się włączyć więc zrobiłem w pliku .bat takie coś:

@echo off
path J:\Windows\Microsoft.NET\Framework\v2.0.50727 regasm Usc.dll /tlb:Usc.tlb
pause

Mam ten Usc.dll w miejscu gdzie odpalam .bat oraz w tym folderze framework, po odpaleniu nie pojawia się żaden Usc.tlb :/

0

Delphi samo ma opcje utworzenia TLB z plików dll lub ocx

0
_13th_Dragon napisał(a)

Delphi samo ma opcje utworzenia TLB z plików dll lub ocx

Nie mogę w sieci znaleźć o tym informacji.

0

Na wstępie zaznaczę, że NIE WIEM, jak załadować bibliotekę dll napisaną w C# bezpośrednio do Delphi. Ale wiem, że można się do kodu w C# odwołać spod C++/CLI, i wykorzystać to do napisania wrappera (opakowania) klas i metod C#-owych. Biblioteka w C++/CLI może eksportować natywny interfejs zgodny z C, a załadowanie tego spod Delphi to już nie problem.

Metoda może wymaga znajomości trzech języków programowania na raz, ale nie trzeba się zagłębiać w bagno jakichś guidów, interfejsów COM i tym podobnych.

Ale jeśli kodu jest mało, to najlepiej go po prostu przetłumaczyć.

0
Azarien napisał(a)

Na wstępie zaznaczę, że NIE WIEM, jak załadować bibliotekę dll napisaną w C# bezpośrednio do Delphi. Ale wiem, że można się do kodu w C# odwołać spod C++/CLI, i wykorzystać to do napisania wrappera (opakowania) klas i metod C#-owych. Biblioteka w C++/CLI może eksportować natywny interfejs zgodny z C, a załadowanie tego spod Delphi to już nie problem.

Metoda może wymaga znajomości trzech języków programowania na raz, ale nie trzeba się zagłębiać w bagno jakichś guidów, interfejsów COM i tym podobnych.

Ale jeśli kodu jest mało, to najlepiej go po prostu przetłumaczyć.

Biblioteka usc.dll która jest użyta w podanym kodzie nie jest raczej bagnem, bo nie wymaga obsługi com bo wszystko odbywa się poprzez funkcje (tak myślę) co widać w programie, jednak nie potrafię czegoś takiego zrobić w delphi. Najchętniej zapłacił bym jakiemuś dobremu programiście by mi to przetłumaczył i dalej bym sobie radził sam, ale zwyczajnie nie mam pieniędzy..... Męczę się z tym ponad tydzień i kolejną bezsensowną próbą było właśnie zrobienie z programu c++ bilbioteki, bo tłuamczenie, vb itp już przerabiałem bez skutku.

0

odezwij się na priva.

0

Odezwałem się i niestety nic :(

1 użytkowników online, w tym zalogowanych: 0, gości: 1