Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7861509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:45:56+00:00 2026-06-02T22:45:56+00:00

I am doing me university project and facing a big issue. i am developing

  • 0

I am doing me university project and facing a big issue. i am developing a UI on C# visual studio 2010. I need to open a serial connection and feed some values into the micro controller. basically i have nodes with 24 bits addresses (3 bytes) e.g 0x78A26F.

i take my input from user in the GUI in a text Box. the user shall enter this 78A26F and i want that the serial port sends the following data 0x6F 0xA2 0x78.

But the user input is saved as a string and when i send it through serial connection, the ASCII are sent e.g 0x37 0x38 0x41 0x32 0x36 0x46. i can do the processing in the uC and perform some checks to subtract 0x30 if between 0x30 and 0x39 or subtract 0x37 if between 0x41 and 0x46. but i do not want to use the uC for this calculations. i want to implement some algorithm in the GUI c# for sending the correct HEX value. So i wrote the following program. but i am getting an error. “Value was either too large or too small for an unsigned byte.” at code line stating (data[2 – i] = Convert.ToByte(x*16 + y)).

I am unable to figure out this issue and fed up now as this should not happen in any case.
if anyone can help me in this regard, either with this code or any other algo/method, i will be really thankful. please, I would prefer never to implement some algo on uC.

Thanks

Maleeha

    /***********************************************************************/
    /*The code for opening the serial connection and converting it appropriately*/
    /***********************************************************************/
    byte[] conversion()
    {
        string text = label16.Text.ToUpper(); // if user enters not capital letters
        byte[] data = new byte[3];

        int x, y;
        bool valid;

        for (int i = 2; i >= 0; i--)
        {
            valid = true;
            x = (Convert.ToInt32(Convert.ToChar(text[2 * i + 1]))); 
            y = (Convert.ToInt32(Convert.ToChar(text[2 * i]))); // I want to first send the 0x6F then 0xA2 and finally 0x78. ToChar is so that i can check for A-F

            if(x >= 48 || x <= 57) // i,e it is already a number
                x = Convert.ToInt32(text[2 * i + 1]);
            else if (x >= 65 || x <= 70) // it is between A-F
                x = x - 55; // in case of A, i get 10
            else // any other value i.e greater than F or any other.
            {
                valid = false;
                break;
            }

            if (y >= 48 || y <= 57) // same for y
                y = Convert.ToInt32(text[2 * i]);
            else if (y >= 65 || y <= 70)
            y = y - 55;
            else
            {
                valid = false;
                break;
            }

            if (valid == true)
               data[2 - i] = Convert.ToByte(x*16 + y); // max will be 15*16 + 15 = 255 which should be OK for convert.ToByte.  
        }
        return data;
    }

    void serial(byte[] text)
    {
        SerialPort serialPort1 = new SerialPort();
        //configuring the serial port
        serialPort1.PortName = "COM1";
        serialPort1.BaudRate = 9600;
        serialPort1.DataBits = 8;
        serialPort1.Parity = Parity.None;
        serialPort1.StopBits = StopBits.One;

        //opening the serial port
        serialPort1.Open();

        //write data to serial port
        serialPort1.Write(text, 0, 3);

        //close the port
        serialPort1.Close();
    }
    /***********************************************************************/

    private void button3_Click(object sender, EventArgs e)
    {
        byte[] data = conversion();
        serial(data);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:45:57+00:00Added an answer on June 2, 2026 at 10:45 pm

    The .NET framework already supports converting hex strings. Make your code look like this:

            uint value;
            if (!uint.TryParse(label16.Text,     // Ought to be a text box
                   System.Globalization.NumberStyles.HexNumber, null, out value)) {
                MessageBox.Show("Invalid hex value");
                return;
            }
            serialPort1.Write(BitConverter.GetBytes(value), 0, 3);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a university project at the minute where I feed information in
For a little university project i'm doing, i need to extract code samples from
For this university project I'm doing (for which I've made a couple of posts
I'm doing a project on filesystems on a university operating systems course, my C
I'm doing a Java software-project at my university that mainly is about storing data-sets
I am doing a project in the university which requires running of multiple instances
im doing a simple website for a university project, and one of the requirements
I'm doing this project for university, which is basically a movie database and for
I am doing a project in the university and it includes a MySQL database.
I'm doing a project in a course at my university on distributed systems. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.