Although I have received some advice and code snippets, I think that I should present a broader view of what I am trying to do and the trouble I am having. I have built a test fixture that has an internal microcontroller which directs traffic in and out of the T.F. In order to test a unit (P.C. or module) a file is created to hold data that is to be sent to the T.F. Another file is created to hold expected answers that will be returned from the T.F. What I want to do is load two arrays with these two files and later be able to extract the data, byte by byte during the testing process. The two files are to contain hex data, 00 through ff, (0 through 255 decimal equivalent). The micrcontroller will analyze the bit structure of the data bytes sent to it and direct the unit under test to return a byte representing the condition being tested. My problem is that I can't get the hex bytes into an array without them being distorted by something beyond my understanding of what is going on. Whenever a byte that is larger than 80 (hex), what is extracted from the array is a byte with a value equal to 253.
My form has three buttons which are labeled: "Load Data File", "Load Answer File", and "Start Test" I need to have the two files to be found on the hard drive and then loaded into the appropriate array. Then when the "Start Test" button is clicked, start retrieving the bytes for processing.
Once I can get over this hump, I believe I can handle the rest of the program. Something more than just a code snippet would be greatly appreciated.
Novice3

Recommended Answers

All 8 Replies

Is the data in your file actual binary data, or is it hex strings?

I've attached a small shell program that will let you browse for 2 files and convert the contents of the files to a byte array. It assumes the data in the file is binary format (actually, it just reads in the bytes of the supplied file). However, if your data files are text files of hex strings, then the load of the data will not occur properly.

If using hex strings, you will have to read in the data 2 characters at a time and convert each 2-char sequence to a byte value, like this:

string sData = "F2";
byte bData = null;
if (!byte.TryParse(sData, System.Globalization.NumberStyles.AllowHexSpecifier, System.Globalization.CultureInfo.InvariantCulture, out bData))
{
    // Error converting byte data
}

Hope this helps!

I appreciate the response, however, when I downloaded your .zip file and tried to open it, I got the message: "zip file is corrupted or invalid". Would you plase try to attach it again?
Thanks
NOVICE3

Try this one. I used 7-Zip to zip it, but normally you shouldn't have trouble with it.

I tried a second time to unzip the file. I have a dual boot machine, 7 on one side and XP on the other. I tried both and still get the message that the folder is invalid. What next?
NOVICE3

Here's the code. You will have to put a form up with the names for the textboxes, buttons, etc. to match the names in the code, as well as the events.

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace TestShell
{
    public partial class Form1 : Form
    {
        private byte[] baData;
        private byte[] baAnswer;

        public Form1()
        {
            InitializeComponent();
        }

        private void cmdDataBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (ofd.FileName.Length > 0)
                {
                    txtDataFile.Text = ofd.FileName;
                }
            }
        }

        private void cmdAnswerBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (ofd.FileName.Length > 0)
                {
                    txtAnswerFile.Text = ofd.FileName;
                }
            }
        }

        private void cmdTest_Click(object sender, EventArgs e)
        {
            if (txtAnswerFile.Text.Length == 0 || txtDataFile.Text.Length == 0)
                return;

            // Load up the byte arrays with the data from the files
            try
            {
                baData = File.ReadAllBytes(txtDataFile.Text);
                baAnswer = File.ReadAllBytes(txtAnswerFile.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR");
                return;
            }

            // Run the test with the 2 byte arrays
        }
    }
}

You'll need:

3 Buttons (named "cmdDataBrowse", "cmdAnswerBrowse" and "cmdTest")
2 Text Boxes (named "txtDataFile" and "txtAnswerFile")

Mike:
You have given me a great boost. The program works fine up to the point where you indicated that I could "run the test with the 2 byte arrays". I put in a line such as:
dataByte = baData[indexer];
where "indexer" was declared as a variable to run from 0 to the end of the file.
This was going to be part of a while loop. The second problem, was finding the length of the file as a limit for "indexer". I am continuing to experiment with pointer, etc, to find the right combination but I am getting error statements that indicate that the program doesn't know where the arrays are. Can you help further?
Alan, NOVICE3

Alan,

The byte array has its own length. This works:

for (int i = 0; i < baData.Length; i++)
{
    byte bByte = baData[i];
    // Do something with "bByte"
}

but so does this:

foreach (byte bByte in baData)
{
    // do something with bByte
}

It just depends if you need the location in the array; if so, then the first approach is better.

Use the "Length" property on the byte arrays to ensure you don't try to grab an element that's not there.

but I am getting error statements that indicate that the program doesn't know where the arrays are

Are you talking about using the "baData" and "baAnswer" arrays in another part of your program? If so, you need to make sure that from a scoping standpoint, you can "see" those variables. In other words, let's say you have another class, called "Analyze", that operates on the two arrays. If you just declare the class "Analyze", like this:

public class Analyze
{
    public void doAnalysis()
    {
        for (int i = 0; i < baData.Length; i++)
        {
            byte bByte = baData[i];
            // Do something with "bByte"
        }
    }
}

That won't work - the "Analyze" class doesn't "know" about the byte array in the "Form1" class. You would have to do something like this:

public class Analyze
{
    public void doAnalysis(byte[] testArray)
    {
        for (int i = 0; i < testArray.Length; i++)
        {
            byte bByte = testArray[i];
            // Do something with "bByte"
        }
    }
}

and you would call this from "Form1", like this:

Analyze myAnalyzer = new Analyze();
myAnalyzer.doAnalysis(baData);

Hopefully, this is the issue you were referring to. Let me know.

I wish to thanks you for all of the help. It has gotten me over the big hump. The program is not finished yet, but I think I can handle the rest myself. If I bump into more problems, I will post a new thread.
THANKS, THANKS, THANKS
Alan NOVICE3

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.