Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a servo controller it can control 16 servos but the code is for arduino

Objective-C
void LEDToggle(void);
void SERVOCTRLSPEED(char Speed);
void SERVOSET(unsigned char LSB1, unsigned char MSB1, unsigned char LSB2, unsigned char MSB2, unsigned char LSB3, unsigned char MSB3, unsigned char LSB4, unsigned char MSB4, unsigned char LSB5, unsigned char MSB5, unsigned char LSB6, unsigned char MSB6, unsigned char LSB7, unsigned char MSB7, unsigned char LSB8, unsigned char MSB8, unsigned char LSB9, unsigned char MSB9, unsigned char LSB10, unsigned char MSB10, unsigned char LSB11, unsigned char MSB11, unsigned char LSB12, unsigned char MSB12, unsigned char LSB13, unsigned char MSB13, unsigned char LSB14, unsigned char MSB14, unsigned char LSB15, unsigned char MSB15, unsigned char LSB16, unsigned char MSB16);

#define LED 13
char LEDState = 0;
void setup()
{
	Serial.begin(115200);
	Serial.write("CCCCC");
	delay(1000);
}

void loop()
{
	SERVOSET(64, 23, 124, 7, 100, 16, 100, 16, 100, 16, 100, 16, 100, 16, 100, 16, 100, 16, 100, 16, 100, 16, 100, 16, 100, 16, 100, 16, 100, 16, 100, 16);
	delay(1000);
// Comment or remove the next 5 lines to run code in loop...
	while(1)
	{
		LEDToggle();
		delay(100);
	}
}

void SERVOCTRLSPEED(char Speed)
{
	int count;
	for (count=0; count <16; count++)
	{
		Serial.write(170);
		Serial.write(8);
		Serial.write(0);
		Serial.write(127-Speed);
	}
}

void SERVOSET(unsigned char LSB1, unsigned char MSB1, unsigned char LSB2, unsigned char MSB2, unsigned char LSB3, unsigned char MSB3, unsigned char LSB4, unsigned char MSB4, unsigned char LSB5, unsigned char MSB5, unsigned char LSB6, unsigned char MSB6, unsigned char LSB7, unsigned char MSB7, unsigned char LSB8, unsigned char MSB8, unsigned char LSB9, unsigned char MSB9, unsigned char LSB10, unsigned char MSB10, unsigned char LSB11, unsigned char MSB11, unsigned char LSB12, unsigned char MSB12, unsigned char LSB13, unsigned char MSB13, unsigned char LSB14, unsigned char MSB14, unsigned char LSB15, unsigned char MSB15, unsigned char LSB16, unsigned char MSB16)
{
	Serial.write(170);	Serial.write(10);
	Serial.write(LSB1); Serial.write(MSB1);Serial.write(LSB2);Serial.write(MSB2);Serial.write(LSB3);Serial.write(MSB3);Serial.write(LSB4);Serial.write(MSB4);
	Serial.write(LSB5);Serial.write(MSB5);Serial.write(LSB6);Serial.write(MSB6);Serial.write(LSB7);Serial.write(MSB7);Serial.write(LSB8);Serial.write(MSB8);
	Serial.write(LSB9);Serial.write(MSB9);Serial.write(LSB10);Serial.write(MSB10);Serial.write(LSB11);Serial.write(MSB11);Serial.write(LSB12);Serial.write(MSB12);
	Serial.write(LSB13);Serial.write(MSB13);Serial.write(LSB14);Serial.write(MSB14);Serial.write(LSB15);Serial.write(MSB15);Serial.write(LSB16); Serial.write(MSB16);
	LEDToggle();
}

void LEDToggle(void)
{
	if (LEDState == 0)
		LEDState = 1;
	else
		LEDState = 0;

	digitalWrite(LED, LEDState);
}




i want to run the servo motors directly from computer serial port please help me to correct the c# code





C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace tes
{
    
   
    public partial class Form1 : Form
    {
       
        public Form1()
        {

            InitializeComponent();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            // 
            textBox3.Text=trackBar1.Value.ToString();
            textBox1.Text = Math.Round((double)((float)(((double)trackBar1.Value) / 32.0))).ToString();
          textBox2.Text =((trackBar1.Value * 4) % 0x80).ToString();

  /*        if (this.sp.IsOpen)
              SERVOSET(this.sp, textBox1.Text, textBox2.Text, " 76", " 22", " 76", "  12", " 108", " 23", " 44", " 20", " 124", " 11", " 84", " 22", " 100", " 16", " 100", " 16", " 100", " 16", " 100", " 16", " 100", " 16", " 100", " 16", " 100", " 16", " 100", " 16", " 100", " 16");


*/


        }

        private void button1_Click(object sender, EventArgs e)
        {
        
   this.sp  = new SerialPort(comboBox1.SelectedItem.ToString(), 19200, Parity.None, 8, StopBits.One);
            // Makes sure serial port is open before trying to write
            try
            {
                if (sp.IsOpen==false)
                {
                  sp.Open();

                  if (sp.IsOpen)
                  {
                      MessageBox.Show("opned");
                  }
                  else {
                      MessageBox.Show("not opened");
                  }
                    
                    sp.Write("CCCCC");
                }
                else { 

             
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error opening/writing to serial port :: " + ex.Message,"Error!");
            }
        
        }
        
        
        



      public void SERVOSET(SerialPort sp, String LSB1,  String MSB1, String LSB2,  String MSB2,  String LSB3,  String MSB3, String LSB4,  String MSB4,  String LSB5,  String MSB5,  String LSB6,  String MSB6,  String LSB7,  String MSB7,  String LSB8,  String MSB8,  String LSB9,  String MSB9,  String LSB10,  String MSB10,  String LSB11,  String MSB11,  String LSB12,  String MSB12,  String LSB13,  String MSB13,  String LSB14,  String MSB14,  String LSB15,  String MSB15,  String LSB16,  String MSB16)
{
	sp.Write("170");	sp.Write("10");
	sp.Write(LSB1); sp.Write(MSB1);sp.Write(LSB2);sp.Write(MSB2);sp.Write(LSB3);sp.Write(MSB3);sp.Write(LSB4);sp.Write(MSB4);
	sp.Write(LSB5);sp.Write(MSB5);sp.Write(LSB6);sp.Write(MSB6);sp.Write(LSB7);sp.Write(MSB7);sp.Write(LSB8);sp.Write(MSB8);
	sp.Write(LSB9);sp.Write(MSB9);sp.Write(LSB10);sp.Write(MSB10);sp.Write(LSB11);sp.Write(MSB11);sp.Write(LSB12);sp.Write(MSB12);
	sp.Write(LSB13);sp.Write(MSB13);sp.Write(LSB14);sp.Write(MSB14);sp.Write(LSB15);sp.Write(MSB15);sp.Write(LSB16); sp.Write(MSB16);

}

        private void Form1_Load(object sender, EventArgs e)
        {

            String []s;
            s = SerialPort.GetPortNames();
            foreach (String x in s)
            {
                comboBox1.Items.Add(x.ToString());
            
            }


           
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.sp.IsOpen)
            {
                try
                {
                    SERVOSET(this.sp,"64","23","124","7","100","16","100","16","100","16","100","16","100","16","100","16","100","16","100","16","100","16","100","16","100","16","100","16","100","16","100","16");
                }catch(Exception df)
                {
                    MessageBox.Show(df.ToString());

                }
            }

        }
    }
}
Posted
Updated 16-Jun-15 16:08pm
v3
Comments
Abhipal Singh 16-Jun-15 23:15pm    
What is wrong/not working in this code?
Did you get any error?
vishnulalr 16-Jun-15 23:22pm    
no error but motor is not moving

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900