vsergeev / lua-periphery

A Lua library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux.
MIT License
182 stars 38 forks source link

how to send hex data with serial? #25

Closed YoonesImani closed 3 years ago

YoonesImani commented 3 years ago

Hi, how can I send serial data in HEX format?

Thanks

vsergeev commented 3 years ago

This is a bit vague. Do you want to send binary data? or binary data encoded into an ASCII hex string?

YoonesImani commented 3 years ago

actually, I want to send binary data.

On Sat, Oct 3, 2020 at 10:15 PM Vanya Sergeev notifications@github.com wrote:

This is a bit vague. Do you want to send binary data? or binary data encoded into an ASCII hex string?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/vsergeev/lua-periphery/issues/25#issuecomment-703147767, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJHLXHOUMRWTDUPVR3OWOWLSI5WLTANCNFSM4SCU7QXA .

vsergeev commented 3 years ago

Lua strings can contain binary data, so you can write binary data with the write() method on the serial handle (https://github.com/vsergeev/lua-periphery/blob/master/docs/serial.md).

YoonesImani commented 3 years ago

The problem is for example when I want to send \r (carriage return) it sends two characters "\" and "r"

On Sat, Oct 10, 2020 at 11:37 PM Vanya Sergeev notifications@github.com wrote:

Closed #25 https://github.com/vsergeev/lua-periphery/issues/25.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/vsergeev/lua-periphery/issues/25#event-3863075235, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJHLXHP2TO4XZOE33COR23TSKC5HZANCNFSM4SCU7QXA .

vsergeev commented 3 years ago

I think your problem is somewhere in the byte string construction.

The Lua string "\r" is one byte:

Lua 5.4.0  Copyright (C) 1994-2020 Lua.org, PUC-Rio
> s = "\r"
> #s
1
> s == "\x0d"
true
> string.byte(s, 1) == 0x0d
true
>