V
V
Vlad Zaitsev2015-04-20 15:38:18
UART
Vlad Zaitsev, 2015-04-20 15:38:18

How to work in Lua with bytes in port?

There are two certain controllers. These controllers have ports (RS485, but not the essence) that are interconnected. Several simple devices on the MK are also connected there. One controller can throw bytes, the second controller can receive them. On Linux controllers (closed a little more than completely) and LUA. There is nothing more.
Attention, the question is: How can I send and receive in Lua not a character, not a string, but just a byte or several bytes in hexadecimal representation? Those. I send 0x15, and on the other controller I also receive 0x15.
So far, I've been tormented for an hour, and I get anything but 0x15. I did not understand whether he converts it into something when sending it, or when receiving it.
Shipping:

require('serial')
port = serial.open('/dev/RS485-2', {
  baudrate = 115200,
  databits = 8,
  stopbits = 1,
  parity = 'none',
  duplex = 'half'
})
port:write(0x15)
port:close()

Reception:
require('serial')
port2 = serial.open('/dev/RS485-3', {
  baudrate = 115200,
  databits = 8,
  stopbits = 1,
  parity = 'none',
  duplex = 'half'
})
data = port2:read(1)
byte=string.byte(data)

log(string.format("%o, %x, %d", byte,byte,byte))
port2:close()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aelfimov, 2015-07-09
@vvzvlad

Dealt with the same problem.
data = port2:read(1) should be like this data = port2:read(1, 1) the second unit is the time to wait for data. and be sure to reset the port after or before the port:flush() session

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question