Dave Chika

Dave Chika

  • NA
  • 70
  • 0

how to write Hex bytes to the serial Port

Jul 12 2010 6:40 PM
please can anyone put me through on how i can send hex values like ( 0x3c,0x01,0x4a,0x6e etc),these values are already programmed on a microcontroller for controlling six stepper motors with different speeds,
 The codes are from 0x01 to 0xfc, each hex value controlls either the motor speed,clockwise or counter clock rotations.
below is my code snippet for clarity



public void toolplate_executeCmd(int trackbarposition, int speedmode)
        {


            if (toolplate_cw_radioButton.Checked == true)
            {
                dir_radiobutton = cw;

            }
            else if (toolplate_ccw_radioButton.Checked == true)
            {
                dir_radiobutton = ccw;


            }


            //      trackbarposition = int.Parse(textBox1.Text);
            //      textBox23.Text = speedmode.Text;
            //      speedmode2 = int.Parse(textBox23.Text);

            switch (dir_radiobutton)  // cw or ccw
            {

                case 1:  // cw

                   
                    switch (speedmode2)   // speed selected by the user
                    {
                        case 1:  // SLOW SPEED

                          
                            switch (trackbarposition)  // checks the position of the trackbar slider
                            {
                                case 0:
                                    temp2trackbarposition = 0x01;

                              

                                    break;

                              

                                case 45:

                                    temp2trackbarposition = "0x02";
                                    break;

                                //      case 60:
                                //          temp2trackbarposition = 0x05;

                                //        break;
                                //      case 75:
                                //         temp2trackbarposition = 0x06;

                                //         break;
                                case 90:
                                    temp2trackbarposition = "0x03";
                                    break;
                                //      case 105:
                                //          temp2trackbarposition = 0x08;

                                //         break;
                                //     case 120:
                                //         temp2trackbarposition = 0x09;

                                //         break;
                                case 135:
                                    temp2trackbarposition = "0x04";
                                    break;
                                case 180:
                                    temp2trackbarposition = "0x05";
                                    break;
                                case 225:
                                    temp2trackbarposition = "0x06";
                                    break;
                                case 270:
                                    temp2trackbarposition = "0x07";
                                    break;
                                case 315:
                                    temp2trackbarposition = "0x08";
                                    break;
                                case 360:
                                    temp2trackbarposition = "0x09";// 15
                                    break;
                            }


               
                            break;

                        case 2: // MEDIUM SPEED
                            // tempspeed = 0x30; // decimal number 48

                            switch (trackbarposition)  // checks the position of the trackbar slider
                            {
                                case 0:
                                    temp2trackbarposition = "0x0B"; // 16

                                    break;

                                //      case 15:
                                //         temp2trackbarposition = 0x11;

                                //        break;

                                //     case 30:
                                //       temp2trackbarposition = 0x12;

                                //       break;

                                case 45:

                                    temp2trackbarposition = "0x0C";
                                    break;

                                //      case 60:
                                //          temp2trackbarposition = 0x14;

                                //         break;
                                //    case 75:
                                //        temp2trackbarposition = 0x15;

                                //        break;
                                case 90:
                                    temp2trackbarposition = "0x0D";
                                    break;
                                //     case 105:
                                //         temp2trackbarposition = 0x17;

                                //        break;
                                //    case 120:
                                //        temp2trackbarposition = 0x018;

                                //        break;
                                case 135:
                                    temp2trackbarposition = "0x0E";
                                    break;
                                case 180:
                                    temp2trackbarposition = "0x0F";
                                    break;
                                case 225:
                                    temp2trackbarposition = "0x10";
                                    break;
                                case 270:
                                    temp2trackbarposition = 0x11;
                                    break;
                                case 315:
                                    temp2trackbarposition = 0x12;
                                    break;
                                case 360:
                                    temp2trackbarposition =  0x13;  // 30
                                    break;
                            }
                            break;

                        case 3: // FAST SPEED
                            // tempspeed = 0x50; // decimal number 80
                            switch (trackbarposition)  // checks the position of the trackbar slider
                            {
                                case 0:
                                    temp2trackbarposition = "0x15";

                                    break;

                                //      case 15:
                                //         temp2trackbarposition = 0x16;

                                //       break;

                                //     case 30:
                                //         temp2trackbarposition = 0x17;

                                //        break;

                                case 45:

                                    temp2trackbarposition = "0x16";
                                    break;

                                //       case 60:
                                //          temp2trackbarposition = 0x19;

                                //         break;
                                //     case 75:
                                //         temp2trackbarposition = 0x1A;

                                //        break;
                                case 90:
                                    temp2trackbarposition = "0x17";
                                    break;
                                //     case 105:
                                //         temp2trackbarposition = 0x1C;

                                //        break;
                                //     case 120:
                                //         temp2trackbarposition = 0x1D;

                                //         break;
                                case 135:
                                    temp2trackbarposition = "0x18";
                                    break;
                                case 180:
                                    temp2trackbarposition = "0x19";
                                    break;
                                case 225:
                                    temp2trackbarposition = "0x1A";
                                    break;
                                case 270:
                                    temp2trackbarposition = "0x1B";
                                    break;
                                case 315:
                                    temp2trackbarposition = "0x1C";
                                    break;
                                case 360:
                                    temp2trackbarposition = "0x1D";
                                    break;
                            }
                            break;
                    }



                    break;

                case 0:  // ccw
                    //  direction = 0x14; // decimal number 20
                    switch (speedmode2)   // speed selected by the user
                    {
                        case 1:  // SLOW SPEED

                            //   tempspeed = 0x10;  // decimal number 16
                            switch (trackbarposition)  // checks the position of the trackbar slider
                            {
                                case 0:
                                    temp2trackbarposition = "0x1F";

                                    break;

                                //     case 15:
                                //        temp2trackbarposition = 0x3F;

                                //        break;

                                //     case 30:
                                //       temp2trackbarposition = 0x40;

                                //       break;

                                case 45:

                                    temp2trackbarposition = "0x20";
                                    break;

                                //      case 60:
                                //         temp2trackbarposition = 0x42;

                                //         break;
                                //     case 75:
                                //        temp2trackbarposition = 0x42;

                                //        break;
                                case 90:
                                    temp2trackbarposition =" 0x21";
                                    break;
                                //      case 105:
                                //          temp2trackbarposition = 0x45;

                                //         break;
                                //       case 120:
                                //          temp2trackbarposition = 0x46;

                                //          break;
                                case 135:
                                    temp2trackbarposition = " 0x22";
                                    break;
                                case 180:
                                    temp2trackbarposition = "0x23";
                                    break;
                                case 225:
                                    temp2trackbarposition = "0x24";
                                    break;
                                case 270:
                                    temp2trackbarposition = "0x25";
                                    break;
                                case 315:
                                    temp2trackbarposition = "0x26";
                                    break;
                                case 360:
                                    temp2trackbarposition = "0x27"; // 76
                                    break;
                            }
                            break;

                        case 2: // MEDIUM SPEED
                            // tempspeed = 0x30; // decimal number 48

                            switch (trackbarposition)  // checks the position of the trackbar slider
                            {
                                case 0:
                                    temp2trackbarposition = "0x29";

                                    break;

                                //     case 15:
                                //        temp2trackbarposition = 0x4E;

                                //        break;

                                //     case 30:
                                //        temp2trackbarposition = 0x4F;

                                //        break;

                                case 45:

                                    temp2trackbarposition = "0x2A";
                                    break;

                                //      case 60:
                                //         temp2trackbarposition = 0x51;

                                //         break;
                                //      case 75:
                                //         temp2trackbarposition = 0x52;

                                //         break;
                                case 90:
                                    temp2trackbarposition = "0x2B";
                                    break;
                                //     case 105:
                                //         temp2trackbarposition = 0x54;

                                //         break;
                                //     case 120:
                                //         temp2trackbarposition = 0x55;

                                //         break;
                                case 135:
                                    temp2trackbarposition = "0x2C";
                                    break;
                                case 180:
                                    temp2trackbarposition = "0x2D";
                                    break;
                                case 225:
                                    temp2trackbarposition = " 0x2E";
                                    break;
                                case 270:
                                    temp2trackbarposition = "0x2F";
                                    break;
                                case 315:
                                    temp2trackbarposition = "0x30";
                                    break;
                                case 360:
                                    temp2trackbarposition = "0x31";  // 91
                                    break;
                            }
                            break;

                        case 3: // FAST SPEED
                            // tempspeed = 0x50; // decimal number 80
                            switch (trackbarposition)  // checks the position of the trackbar slider
                            {
                                case 0:
                                    temp2trackbarposition = "0x33"; // 92

                                    break;

                                //      case 15:
                                //          temp2trackbarposition = 0x5D;

                                //         break;

                                //      case 30:
                                //         temp2trackbarposition = 0x5E;

                                //       break;

                                case 45:

                                    temp2trackbarposition = "0x34";
                                    break;

                                //     case 60:
                                //         temp2trackbarposition = 0x60;

                                //        break;
                                //     case 75:
                                //         temp2trackbarposition = 0x61;

                                //         break;
                                case 90:
                                    temp2trackbarposition = "0x35";
                                    break;
                                //      case 105:
                                //          temp2trackbarposition = 0x63;

                                //          break;
                                //       case 120:
                                //          temp2trackbarposition = 0x64;

                                //          break;
                                case 135:
                                    temp2trackbarposition = "0x36";
                                    break;
                                case 180:
                                    temp2trackbarposition = "0x37";
                                    break;
                                case 225:
                                    temp2trackbarposition = "0x38";
                                    break;
                                case 270:
                                    temp2trackbarposition = "0x39";
                                    break;
                                case 315:
                                    temp2trackbarposition = "0x3A";
                                    break;
                                case 360:
                                    temp2trackbarposition = "0x3B"; // 106
                                    break;
                            }
                            break;
                    }
                    break;
            }
        }

Answers (5)