This program is use to convert decimal to hex and send through serial port in ASCII characters. For example: I try to send a string variable in Hex format which i need to convert to a standart ASCII string; eg "70d9b7" where "b7" is the checksum of 2's complement. i have some problem in convert the "d9" and "b7" to the correct ASCII string as the ASCII is overflow from 8 till F, i think... If anyone can help me to debug the problem, i would be very grateful... Thanks a lot...
# region using directive
using System;
using System.IO.Ports;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
# endregion
namespace WindowsApplication1
{
public partial class Form1 : Form
{
SerialPort sp = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
int Val1, Val2;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private string HexAsciiConvert(string hexValue1)
{
StringBuilder sb = new StringBuilder();
try
{
for (int i = 0; i <= hexValue1.Length - 2; i += 2)
{
sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hexValue1.Substring(i, 2),
System.Globalization.NumberStyles.HexNumber))));
}
for (int i = 8; i <= hexValue1.Length - 2; i++)
{
sb.Append(Convert.ToChar(Int32.Parse(hexValue1, System.Globalization.NumberStyles.HexNumber)));
}
}
catch(System.OverflowException exception)
{
// return sb.ToString;
}
return sb.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
Val1 = Val2 = 0;
if (H0_1.Checked == true)
{
Val1 = Val1 + 1;
}
if (H1_1.Checked == true)
{
Val1 = Val1 + 2;
}
if (H2_1.Checked == true)
{
Val1 = Val1 + 4;
}
if (H3_1.Checked == true)
{
Val1 = Val1 + 8;
}
if (H4_1.Checked == true)
{
Val1 = Val1 + 16;
}
if (H5_1.Checked == true)
{
Val1 = Val1 + 32;
}
if (H6_1.Checked == true)
{
Val1 = Val1 + 64;
}
if (H7_1.Checked == true)
{
Val1 = Val1 + 128;
}
if (D0.Checked == true)
{
Val2 = Val2 + 1;
}
if (D1.Checked == true)
{
Val2 = Val2 + 2;
}
if (D2.Checked == true)
{
Val2 = Val2 + 4;
}
if (D3.Checked == true)
{
Val2 = Val2 + 8;
}
if (D4.Checked == true)
{
Val2 = Val2 + 16;
}
if (D5.Checked == true)
{
Val2 = Val2 + 32;
}
if (D6.Checked == true)
{
Val2 = Val2 + 64;
}
if (D7.Checked == true)
{
Val2 = Val2 + 128;
}
{
textread.Text = "";
int checksum = (byte)~(Val1 + Val2) + 1;
string a = checksum.ToString("x");
string hexValue1;
string hexValue2;
if ( Val1< 16)
{
hexValue1 = "0" + Val1.ToString("x");
}
else
hexValue1 = Val1.ToString("x");
if (Val2< 16)
{
hexValue2 = "0" + Val2.ToString("x");
}
else
hexValue2 = Val2.ToString("x");
textread.Text = hexValue1 + hexValue2 + a;
}
}
private void button1_Click(object sender, EventArgs e)
{
string hexValue1;
string hexValue2;
if (Val1 < 16)
{
hexValue1 = "0" + Val1.ToString("x");
}
else
hexValue1 = Val1.ToString("x");
if (Val2 < 16)
{
hexValue2 = "0" + Val2.ToString("x");
}
else
hexValue2 = Val2.ToString("x");
int checksum = (byte)~(Val1 + Val2) + 1;
string a = checksum.ToString("x");
hexValue1 = HexAsciiConvert(hexValue1);
hexValue2 = HexAsciiConvert(hexValue2);
a = HexAsciiConvert(a);
textsend.Text = hexValue1 + hexValue2 + a;
sp.Open();
sp.Write(textsend.Text);
sp.Close();
}
emily