lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Thu, Oct 17, 2019 at 10:34 PM Vinicius Jarina <viniciusjarina@gmail.com> wrote:
Hello Russel

NLua will use the current Encoding to convert the Lua string to C# string.

There was a minor issue on NLua where wasn't possible to pass a Lua string to a byte [] parameter on C#.

I've fixed the issue. Here is a example how to pass binary data from Lua side to C# side.

https://github.com/NLua/NLua/commit/d8ae863aeae4cff6cdb6de48ddddb36a2940bf14#diff-8aa37412f4b48b161b9e772ef2a6e850R2608

Thank you

That's fantastic. It works seamlessly in my test jig. This is going to be perfect for my serial console!

using System;
using System.Linq;
using NLua;

namespace NLuaConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting Lua");

            Lua lua = new Lua();
            lua["WriteBinary"] = (Action<byte[]>)WriteBinary;
            lua.DoString(@"
print('testing')
local t = {0x5aa5, 0x0600}
str = string.pack('<h<h',t[1], t[2])
print(str)
WriteBinary(str)
");

            Console.ReadLine();
        }

        private static void WriteBinary(byte[] buffer)
        {
            foreach(var b in buffer)
            {
                Console.WriteLine("{0:X}", b);
            }
        }
    }
}

Starting Lua
testing
ÑZ
A5
5A
0
6

Russ  

On Thu, Oct 17, 2019 at 8:01 PM Russell Haley <russ.haley@gmail.com> wrote:


On Thu, Oct 17, 2019 at 1:41 PM Sean Conner <sean@conman.org> wrote:
It was thus said that the Great Russell Haley once stated:
> Hi,
>
> My project is a serial port console written in C# that uses an embedded Lua
> interpreter via NLua. I have a lua script to send a binary messages out the
> serial port. I am using string.pack to create the message. My lua script:
>
> local bytes = {0x5AA5}

  [ snip ]

> My C# WriteRemote is simple:
>         public void WriteRemote(string data, bool appendLineEnding = true)
>         {
>             if(_logging)
>             {
>                 _scriptLog.WriteLine(data);
>             }
>             data = "" ? data + _lineEnding : data;
>             byte[] bytes = Encoding.UTF8.GetBytes(data);
                                      ^^^^

The string you are generating, 0xA5,0x5A, is not a valid UTF-8 string.
Could that perhaps be the issue?

  -spc

It very well could be. I'll look into this tomorrow. Thanks Sean!

Russ