Converting ASCII String to Bytes

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
Miller
Posts: 3
Joined: Tue Apr 09, 2019 8:25 am

Converting ASCII String to Bytes

Post 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!

User avatar
Russ C.
Posts: 213
Joined: Thu Nov 16, 2017 3:32 pm
Contact:

Re: Converting ASCII String to Bytes

Post 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);
Best regards,

Russ
(801) 708-6690
Technical Support
Contact Us

Beijer Electronics AB
http://www.beijerelectronics.us

Miller
Posts: 3
Joined: Tue Apr 09, 2019 8:25 am

Re: Converting ASCII String to Bytes

Post 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.

AMitchneck
Posts: 137
Joined: Mon Jun 11, 2012 2:10 pm

Re: Converting ASCII String to Bytes

Post 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];
  }
}
Adam M.
Controls Engineer
FlexEnergy

Miller
Posts: 3
Joined: Tue Apr 09, 2019 8:25 am

Re: Converting ASCII String to Bytes

Post 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?
Attachments
Script_Errors.PNG
Script_Errors.PNG (111.2 KiB) Viewed 6007 times

AMitchneck
Posts: 137
Joined: Mon Jun 11, 2012 2:10 pm

Re: Converting ASCII String to Bytes

Post 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];
  }
}
Adam M.
Controls Engineer
FlexEnergy

Post Reply