Quantcast
Viewing all articles
Browse latest Browse all 81

Arduino sending trigger to Nano

So i have connected Nano to the maestro from the tx of nano to the rx cable. I have given them common ground and powersupply with help of breadboard and micro usb board.

In arduino ide, I have written a code that can send the trigger of 0x55:

void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600); // Use the same baud rate as set in the Maestro.
}

void loop() {
 // put your main code here, to run repeatedly:
 Serial.write(0x55); // Example trigger command (adjust as needed).
 delay(20000); // Delay between triggers.
}

In pololu c# code, I have written

public MainWindow()
        {
            InitializeComponent();
            OneDegreeInMicroSec = 11.11;

            InitializeSerialPort(); // Initialize your serial port settings
            AttachDataReceivedHandler();
        }

private void InitializeSerialPort()
        {
            mySerialPort = new SerialPort("COM8");

            mySerialPort.BaudRate = 9600;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None;
            mySerialPort.RtsEnable = true;
            // Initialize the receive buffer
            receiveBuffer = new byte[1024];

            try
            {
                mySerialPort.Open();
                MessageBox.Show("Serial port opened successfully.");
            }
            catch (UnauthorizedAccessException ex)
            {
                MessageBox.Show("Unauthorized access to the serial port. Check if the port is already in use by another application.");
            }
            catch (IOException ex)
            {
                MessageBox.Show("An I/O error occurred while opening the serial port.");
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show("Invalid serial port settings or name.");
            }
        }

        private void AttachDataReceivedHandler()
        {
            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
        }

        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            byte[] buffer = new byte[sp.BytesToRead];
            sp.Read(buffer, 0, buffer.Length);

            // Debug: Print received data as hexadecimal
            string receivedDataHex = BitConverter.ToString(buffer);
            MessageBox.Show("Received Data: " + receivedDataHex);

            // Check if the received data contains a trigger condition
            if (ContainsByte(buffer, 0x55) && mySerialPort.IsOpen)
            {
                // Raise the TriggerReceived event
                OnTriggerReceived();
                MessageBox.Show("Trigger received!");
            }
        }

        private bool ContainsByte(byte[] array, byte value)
        {
            foreach (byte b in array)
            {
                if (b == value)
                {
                    return true;
                }
            }
            return false;
        }

        

        protected virtual void OnTriggerReceived()
        {
            // Check if the event has subscribers (is not null)
            if (TriggerReceived != null)
            {
                TriggerReceived?.Invoke(this, EventArgs.Empty);
                MessageBox.Show("Trigger received!");
            }

            // Optionally, display a message regardless of whether there are subscribers
            MessageBox.Show("Trigger not received!");
        }
private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            if (usc == null)
            {
                // Try connecting to a device.
                try
                {
                    TryToReconnect();
                    OnTriggerReceived();
                }
                catch (Exception e2)
                {
                    Log(e2);
                    Log("Failed connecting to #" + SerialNumberTextBox.Text + ".");
                    usc = null;
                }
            }
            else
            {
                // Update the GUI and the device.
                try
                {
                    DisplayPosition();
                    OnTriggerReceived();
                }
                catch (Exception e2)
                {
                    // If any exception occurs, log it, set usc to null, and keep trying..
                    Log(e2);
                    Log("Disconnected from #" + SerialNumberTextBox.Text + ".");
                    usc = null;
                }
            }
        }

and it keeps saying trigger not recieved. Com10 is TTL port and com 8 is command port of maestro. Please let me know how I can improve my c# code to recieve the trigger from arduino nano

12 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 81

Trending Articles