Home > Mobile >  In my C# app. I am reading the CNC program of a remote machine into a byte buffer. How can I read th
In my C# app. I am reading the CNC program of a remote machine into a byte buffer. How can I read th

Time:08-07

I can read the complete CNC program of a remote machine to a byte buffer with a third party dll. After that I have to read the byte buffer line by line and transfer it to a string array.(I don't want to write the program to a file, I want to show the program lines line by line in my Blazor page)

I have written following code, where I read the complete buffer into one string. But I cannot split it to lines. How can I do that? I should normally read the CNC program for example like:

G0 G603

AAA:

Z=IC(-50)

Z=IC(50)

GOTOB AAA

M30

But my code is reading the CNC program as one line.

G0 G603 AAA: Z=IC(-50) Z=IC(50) GOTOB AAA M30

public Int32 doNCK_CopyFromNCAlloc(Int32 connnr, Int32 timeout, string path, out string  CNC_File_Out)
    {
        Int32 result = 0;
        Byte[] buff = null;
        Int32 bufflen = 0;
        String filename = path;
        CNC_File_Out = "";
        result = AGL4.NCK_CopyFromNCAlloc(connnr, filename, out buff, out bufflen, timeout);

     if (result == 0)
        {                
            b = buff;             
            parameter_value_string = BitConverter.ToString(b, 0);
            parameter_value_string_UTF8 = System.Text.Encoding.UTF8.GetString(b);
            CNC_File_Out= parameter_value_string_UTF8.Replace("\0", string.Empty);
        }            
        Return_Read_CNC_File = result;
        return result;
    }

Update-1 (The byte output is like)

47-30-20-47-36-30-33-0D-0A-41-41-41-3A-0D-0A-5A-3D-49-43-28-2D-35-30-29-0D-0A-5A-3D-49-43-28-35-30-29-0D-0A-47-4F-54-4F-42-20-41-41-41-0D-0A-4D-33-30-0D-0A-0D-0A

CodePudding user response:

Simply reading the bytes will give you the desired string including Windows system newline:

CNC_File_Out = Encoding.UTF8.GetString(buff, 0, buff.length);

No need for BitConverter.ToString() and replacing a null-terminator.

  • Related