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);
}
The .NET framework already supports converting hex strings. Make your code look like this: