Page 1 of 1
Converting ASCII String to Bytes
Posted: Tue Apr 09, 2019 8:43 am
by Miller
Hi,
Hoping someone can help me out here and let me start off by saying I'm not very knowledgeable in C#. Due to limitations of our SCADA system I cannot pull back strings from our RTU so I would like to convert a 10 character ASCII string into 10 different bytes (Tags) in the HMI that I could then send to the RTU.
Thanks in advance!
Re: Converting ASCII String to Bytes
Posted: Fri Apr 12, 2019 10:01 am
by Russ C.
You should be able to convert it to a byte array using this code:
Code: Select all
byte[] myBytes = Encoding.ASCII.GetBytes(myString);
and then convert it back using this code:
Code: Select all
string myString= Encoding.ASCII.GetString(myBytes);
Re: Converting ASCII String to Bytes
Posted: Mon Apr 15, 2019 7:42 am
by Miller
So I have the following on a button right now, will change it to a second tag once I get it to work.
using System.Text;
public partial class Home
{
void Button_Click(System.Object sender, System.EventArgs e)
{
string input = Globals.Tags.String_ROC.Value;
byte[]array = Encoding.ASCII.GetBytes(input);
}
}
How do I assign Byte 1 of this array to a Globals.Tags.XX.Value that is a single UINT16 tag in the tag list? Then Byte 2, etc. I have the UINT16 tag already set up as a byte to my controller using the B:XX,XX,XX call out.
Re: Converting ASCII String to Bytes
Posted: Tue Apr 16, 2019 7:39 am
by AMitchneck
Assuming the string always has 10 characters you could use the following
Code: Select all
public partial class Home
{
void Button_Click(System.Object sender, System.EventArgs e)
{
string input = Globals.Tags.String_ROC.Value;
byte[] array = Encoding.ASCII.GetBytes(input);
Globals.Tags.Byte0.Value = array[0];
Globals.Tags.Byte1.Value = array[1];
Globals.Tags.Byte2.Value = array[2];
Globals.Tags.Byte3.Value = array[3];
Globals.Tags.Byte4.Value = array[4];
Globals.Tags.Byte5.Value = array[5];
Globals.Tags.Byte6.Value = array[6];
Globals.Tags.Byte7.Value = array[7];
Globals.Tags.Byte8.Value = array[8];
Globals.Tags.Byte9.Value = array[9];
}
}
Re: Converting ASCII String to Bytes
Posted: Fri May 17, 2019 6:48 am
by Miller
AMitchneck wrote:Assuming the string always has 10 characters you could use the following
Code: Select all
public partial class Home
{
void Button_Click(System.Object sender, System.EventArgs e)
{
string input = Globals.Tags.String_ROC.Value;
byte[] array = Encoding.ASCII.GetBytes(input);
Globals.Tags.Byte0.Value = array[0];
Globals.Tags.Byte1.Value = array[1];
Globals.Tags.Byte2.Value = array[2];
Globals.Tags.Byte3.Value = array[3];
Globals.Tags.Byte4.Value = array[4];
Globals.Tags.Byte5.Value = array[5];
Globals.Tags.Byte6.Value = array[6];
Globals.Tags.Byte7.Value = array[7];
Globals.Tags.Byte8.Value = array[8];
Globals.Tags.Byte9.Value = array[9];
}
}
Tried incorporating this piece of code but am getting the errors that are attached. I then tried to create a set of internal tags with default data types which I could link to the external tags with the correct ones and got the same errors. Any idea's on corrections?
Re: Converting ASCII String to Bytes
Posted: Fri May 17, 2019 7:11 am
by AMitchneck
It seems the tag type does not directly support byte values so it's trying to type cast it. The error indicates C# can't implicitly determine which type to cast it to. Try the following. This explicitly type casts the byte values to ushort.
Code: Select all
public partial class Home
{
void Button_Click(System.Object sender, System.EventArgs e)
{
string input = Globals.Tags.String_ROC.Value;
byte[] array = Encoding.ASCII.GetBytes(input);
Globals.Tags.Byte0.Value = (ushort)array[0];
Globals.Tags.Byte1.Value = (ushort)array[1];
Globals.Tags.Byte2.Value = (ushort)array[2];
Globals.Tags.Byte3.Value = (ushort)array[3];
Globals.Tags.Byte4.Value = (ushort)array[4];
Globals.Tags.Byte5.Value = (ushort)array[5];
Globals.Tags.Byte6.Value = (ushort)array[6];
Globals.Tags.Byte7.Value = (ushort)array[7];
Globals.Tags.Byte8.Value = (ushort)array[8];
Globals.Tags.Byte9.Value = (ushort)array[9];
}
}