read and write on serial port with a lua script on raspberry - lua

i'm currently working on a project aiming at controlling a RS232 device by a Raspberry pi4. I'm forced by other softwares to use a lua script and i never coded in lua, even if i have already made the code in Python... I've searched on google far and wide for an answer but have not found anything helping me.
I want my raspberry to open the COM port, and then read and send messages through that port.
I've tried the lua user wiki and This post on stackoverflow (read and write on windows) but both can't be applied to raspberry.
If anyone could help that'd be awesome !
here's my code in python if that can help you.
import serial
def convertisseur(chemin):
Tableau = []
f = open(chemin)
for row in f:
Tableau.append(row)
f.close
return (Tableau)
def statut(ser):
ser.write(bytearray([0X53,0X07,0X01,0X01,0X41,0X60,0X00,0X73,0X45]));
lecture=ser.readline()
print(lecture);
return()
def main():
ser = serial.Serial('COM3', 115200, timeout=1)
ser.close()
ser.open()
Code=convertisseur('C:/Users/Ello/Desktop/CodePosition.txt')
statut(ser)
ser.write(bytearray([0X53,0X09,0X01,0X02,0X40,0X60,0X00,0X06,0X00,0X2C,0X45]));
ser.readline()
ser.write(bytearray([0X53,0X09,0X01,0X02,0X40,0X60,0X00,0X0F,0X00,0X25,0X45,0X53,0X08,0X01,0X02,0X60,0X60,0X00,0X01,0XF5,0X45]));
ser.readline()
statut(ser)
print('Engine move in cm ? - for trigo, STOP to stop')
Speed = input()
while Speed != "STOP":
speed = int(Speed)
if speed > 38:
speed = 38
if speed < -37:
speed = -37
speed = round(abs((speed-38)))
ser.write(bytearray(int(i, 16) for i in Code[speed-1].split(",")))
ser.write(bytearray([0X53,0X09,0X01,0X02,0X40,0X60,0X00,0X0F,0X00,0X25,0X45,0X53,0X09,0X01,0X02,0X40,0X60,0X00,0X7F,0X00,0X55,0X45]));
ser.readline()
statut(ser)
Speed = input()
print("Fin du programme, fermeture du port, passage du moteur sur OFF")
ser.write(bytearray(int(i, 16) for i in Code[60].split(",")))
statut(ser)
ser.close()
return 0

I want my raspberry to open the COM port, and then read and send messages through that port.
Hi, try vsergeev/lua-periphery: A Lua library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux
, especially Serial section.
Not sure the dev path for you.
In my case the periphery COM device connect to TTL-USB converter, then USB of Pi, so path is '/dev/ttyUSBx'.
As I know, GPIO has UART serial support too.

Related

Capture ANSI escape sequence response using Micropython ESP8266

I'm using an ESP8266 with Micropython to communicate with a serial terminal using Putty. Using escape sequences like referenced in these questions:
Read ANSI escape from terminal
How do I determine size of ANSI terminal?
List of escape sequences https://vt100.net/docs/vt100-ug/chapter3.html
I am trying to read back an escape sequence that is a reply to find the cursor position.
I can see the response in the terminal, but I need to capture it so I can parse it. I'm familiar with Python, but a complete noob with Micropython and the ESP8266 (ESP01). I'm using the webrepl to upload the code. I saw this and I'm not sure if it's related: https://forum.micropython.org/viewtopic.php?t=5359
I've tried running my code without webrepl connected, but it still didn't work.
What am I doing wrong, how can I capture the response?
Depending on the terminal size, the response should be something like below, the stuff before the ; will not be visible.
^[[45;157R
#import esp
import network
import machine
from machine import UART
uart = UART(0, 115200)
x=uart.write("\033[500;500H\033[6n")
if uart.any():
print(":> ", uart.read())
EDIT: Tried example from this link https://forum.micropython.org/viewtopic.php?t=5359#p30867
No luck with that either (yes, I know it's blocking). Here if I type the letter 'R', it drops out of the loop, but it's not catching the output from the escape sequence.
from machine import UART
import machine
import network
import uos
uos.dupterm(None, 1)
uart = UART(0, 115200)
charbuf = None
uart.write("\033[500;500H\033[6n")
while charbuf != b"R":
if uart.any():
charbuf = uart.read()
uart.write(charbuf)
uos.dupterm(UART(0, 115200), 1)
Solving this was a nightmare, the WebRepl console replication has been the bane of my existence and probably many others. I tried multiple read() methods and they all failed.
uart.read()
sys.stdin.read()
sys.stdin.buffer.read()
Those were in combination of enabling/disabling the console duplication for webrepl.
The incantation that finally worked for me is below. See inline comments for more details.
import machine
import network
#import sys
import uos
from machine import UART
uos.dupterm(None, 1) # Disable console duplication for Webrepl
uart = UART(0, 115200, timeout=100, timeout_char=100) #Instantiate uart, increase timeout for good measure
uart.write("\033[2J\033[1;1f") # Clear screen
uart.write("\033[500;500H\033[6n") # Move cursor to bottom right, send query.
# Wait for uart to collect response. This will block further execution until we get a reply.
while True:
if uart.any() != 0:
break
uos.dupterm(UART(0, 115200), 1) # Re-enable console redirection so print() will work
# Read the uart buffer with response, convert from binary to string, replace ESC character.
# First ^ gets eaten somewhere!
buf = str(uart.read(), 'UTF-8').replace('\x1b', '^^[')
if len(buf) > 0: # If response isn't empty, print buffer
print(buf)
This is the Output:
^[[43;157R

Trying to control fans on a raspberry pi 3 GPIO over Bluetooth Low Energey

I have 2 dual-h bridge controllers(used to supple power to fans) that are connected to my raspberry pi 3 b via GPIO and a battery for power I have created a script in python 3 that uses Bluetooth classic to ask the connected phone in a terminal how long the fan should be on for and how long they should be off for in a loop I have got this to work GREAT, but this works only on android as it is Bluetooth classic i now want to make this work for my iPhone as well i found out i would need to use BLE so my question is how can i convert my original script in Bluetooth classic(using RF COMM Sockets) to use Bluetooth Low Energy.
Here is my original script
# Importing the Bluetooth Socket library
import bluetooth
# Importing the GPIO library to use the GPIO pins of Raspberry pi
import RPi.GPIO as GPIO
import time
fan_pin = 16# Initializing pin 16 for fan
fan2_pin = 18
GPIO.setmode(GPIO.BOARD) # Using BCM numbering
GPIO.setup(fan_pin, GPIO.OUT) # Declaring the pin 16 as output pin
GPIO.setup(fan2_pin, GPIO.OUT) # Declaring the pin 16 as output pin
host = ""
port = 1 # Raspberry Pi uses port 1 for Bluetooth Communication
# Creaitng Socket Bluetooth RFCOMM communication
server = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
print('Bluetooth Socket Created')
try:
server.bind((host, port))
print("Bluetooth Binding Completed")
except:
print("Bluetooth Binding Failed")
server.listen(1) # One connection at a time
# Server accepts the clients request and assigns a mac address.
client, address = server.accept()
print("Connected To", address)
print("Client:", client)
while 1:
# Receivng the data.
client.send("How long do you want the fans to be on for?/n")
On = int(client.recv(1024)) # 1024 is the buffer size.
time.sleep(5)
client.send("Ok, Now how long do you want the fans to be off for?/n")
Off = int(client.recv(1024))
print(On)
print(Off)
while True:
GPIO.output(fan_pin, GPIO.HIGH)
GPIO.output(fan2_pin, GPIO.HIGH)
time.sleep(On)
GPIO.output(fan_pin, GPIO.LOW)
GPIO.output(fan2_pin, GPIO.LOW)
time.sleep(Off)
# Making all the output pins LOW
GPIO.cleanup()
# Closing the client and server connection
client.close()
server.close()
I would imagine this would be as simple as changing anything that was Bluetooth classic code to BLE code. Max like 20 lines to change?

SPI on Beagleboard Black

I've been trying to make the serial communication work on my BBB for days now and I am running out of ideas.
When I use just the BBB and connect MISO/MOSI I get the signal transfer on MOSI, SCLK and CS (MISO is mainly at high level). However, when I connect the lines to my slave part it does not work. I checked the signals on the oscilloscope and they seem fine and the part which I am using as the slave is working well when I set it in parallel mode, so I believe some programming or configuration must be wrong.
This is basically what I do:
config-pin P9.17 spi_cs
config-pin P9.18 spi
config-pin P9.21 spi
config-pin P9.22 spi_sclk
python
from Adafruit_BBIO.SPI import SPI
spi = SPI(1,0) #I would expect SPI(0,0) here, but I get the signal on the above configured ports
Then I set the configurations (already tried in many ways):
spi.mode = 0
spi.cshigh = False
spi.msh = 10500000
spi.bpw = 16
spi.lsbfirst = False
After that I open it and try to send data:
spi.open(1,0)
spi.xfer2([1,254])
If anyone is interested, I am trying to program the LMH6517 as slave and I already tried to ask about this at the TI forum here:
https://e2e.ti.com/support/amplifiers/f/14/t/751415
Oscilloscope images:
CS and SCLK
MOSI and SCLK
MISO and SCLK
Thank you,
JPL

Telnet server example

Today I installed NodeMCU on one ESP8266 module.
I made a simple telnet server (logging in to the AP is already done and connected)
srv=net.createServer(net.TCP,7200)
srv:listen(23,function(conn)
conn:on("receive", function(conn,telnetdata)
print(telnetdata)
conn:send("Got it\r\n")
end)
conn:on("sent",function(conn)
print("[Sent]");
--conn:close();
--collectgarbage();
end)
end)
I can telnet into the ESP8266 using Putty and I can send static data back to Putty (conn:send("Got it\r\n")).
What I want to do is to send data to Putty from the esp serial port (UART). I've googled for an example without success. BTW I'm using ESPlorer to program the ESP and to send data back to putty.
Instead of using conn:send("Got it\r\n") I want to dynamically type the response. I've tried uart:on callback but I haven't fully understood how it works.
NodeMCU has an "official" Telnet example in the repository. However, that one is also WiFi/socket-based.
If you're connected to the device through ESPlorer (i.e. serial/UART) you can't open another serial connection. See https://stackoverflow.com/a/36779799/131929 for details.
Marcel thank you for your answer.
The link posted is not exactly what I need but it offered good info.
I finally came up with this code that works as a simple bridge between putty and a micro controller (arduino mega 2560 in this case)
socket = net.createServer(net.TCP,7200)
socket:listen(23,function(c)
c:on("receive",function(c,l)
uart.write(0, l)
end)
c:on("disconnection",function(c)
-- not yet implemented
end)
uart.on("data", 0,
function(data)
c:send(data)
end, 0)
end)

UART data error when using uart.alt(1)

I am trying to acquire rs232 data from a device connected to the ESP8266 (data will then be sent our via http/wifi).
I am using max3232 IC to provide the necessary 3.3v TTL to the ESP8266.
I have have connected the max3232 (pin 12) to GPIO pin 13 (rx) on the ESP8266 (I am only receiving data not sending data, so only the rx pin is connected).
The code i am using:
--
--file: test2.lua
--
tst2 = require "tst2"
tst2.start()
--tst2.lua (testing script)
local module = {}
function module.start()
print("in tst2.start")
uart.alt(1) --use alt GPIO pin 13 (Rx)
uart.setup(0, 9600,8, uart.PARITY_NONE, uart.STOPBITS_1,0)
uart.on("data",10,
function(data)
file.open("data.tmp", "w+")
file.writeline("starting")
for i=1,10 do
file.writeline(string.byte(string.sub(data,i,i)) )
end
file.writeline("from uart: ", data)
file.writeline("finished")
file.close()
end, 0)
uart.alt(0) --switch back to standard Rx/Tx pins
end
return module
The rs232 device connected to the ESP8266 is putting out a single alphabetic character every 3 seconds, however the data written to file (data.tmp) is as follows
starting
10
13
10
13
10
13
10
13
10
13
from uart:
finished
file.close()
Problems:
1- The rs232 device is not issuing any newln or cr characters, but these are appearing in the data file.
2- the string "file.close()" is being written to the data file, and looks like it is the actual lua command that follows the final file.writeline command.
3- the alphabetic data is not appearing in the data file.
4- switching back to the standard uart pins via uart.alt(0) does not work (the ESP8266 must be rebooted - this is not a major issue as the standard uart pins are only used during debugging).
I am writing the rs232 data to a file instead of simply printing it out on the screen (I am using ESPlorer v0.2.0) because the uart.alt(1) command redirects the serial port to the alternative ESP8266 gpio pins.
I think I am doing something fundamentally wrong with the uart set up, but i can't tell what it is.
SOLVED:
It appears that you can't connect the ESP8266 to both the serial port for debugging (e.g. the serial port on a pc running ESPlorer) and also have the alternate serial pins (ESP8266 GPIO 13 and 15) connected (to an external serial device) at the same time.
The nodemcu uart.alt() function does not appear to "turn off" the standard serial i/o pins.
Disconnecting the pc from the standard serial i/o pins solved the problem (debugging becomes an issue, but there are work-arounds to resolve this).
(updated) one workaround is to use a simple telnet server to interact with the lua interpreter. you can either connect the ESP8266 to your wifi router or, even better, set it up as an access point (AP) so that all you have to do is to connect your computer to it and then simply telnet in (to the gateway's IP). so, in addition to the telnet code, you'll need set up the AP in your init.lua. full code for the telnet server and the AP setup is below. A nice benefit is that I can program and monitor the ESP8266 from my phone using an off-the-shelf telnet app!
jj = [[
sock = 22 -- just a placeholder, so it stays global. may not be needed.
-- use sock:send("hello") to insert your own custom output to the client.
telnet_srv = net.createServer(net.TCP, 180)
telnet_srv:listen(2323, function(socket)
local fifo = {}
local fifo_drained = true
local function sender(c)
if #fifo > 0 then
c:send(table.remove(fifo, 1))
else
fifo_drained = true
end
end
local function s_output(str)
table.insert(fifo, str)
if socket ~= nil and fifo_drained then
fifo_drained = false
sender(socket)
end
end
sock = socket -- make the socket globally available.
node.output(s_output, 0) -- re-direct output to function s_ouput.
socket:on("receive", function(c, l)
node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
end)
socket:on("disconnection", function(c)
node.output(nil) -- un-regist the redirect output function, output goes to serial
end)
socket:on("sent", sender)
print("Welcome to NodeMCU world.")
end)
]]
file.open("telnet.lua", "w")
file.write(jj)
file.close()
jj = [[
wifi.setmode(wifi.STATIONAP);
wifi.ap.config({ssid="ESPtest",pwd=""});
print("Server IP Address:",wifi.ap.getip())
dofile("telnet.lua")
]]
file.open("init.lua","w")
file.write(jj)
file.close()
node.restart()
output:
Server IP Address: 192.168.4.1 255.255.255.0 192.168.4.1
>

Resources