Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to realize Serial data acquisition and TCP one-way transparent Transmission by LUA

Shulou Source: shulou.com Published: 2022-06-01 16:58:19 09月30日 Update

This article mainly explains "LUA how to achieve serial data acquisition and TCP one-way transparent transmission", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "LUA how to achieve serial data acquisition and TCP one-way transparent transmission" bar!

Foreword:

LUA is widely used in the game industry, especially in writing the coordination between extension modules and the C language. Its efficiency is higher than that of many other scripting languages. I am engaged in software development in the direction of industrial control. I want this scripting language to adapt to my working environment.

Why is it forwarded instead of two-way transmission between serial port and network?

When testing, you can only read data by opening the serial port "rb". In theory, you can read and write serial port in both directions with "wb". If you know how to solve this problem, please let me know.

How to set lua serial port parameters? Such as the baud rate?

I set the parameters with minicom and then run the lua script

Network module support

The luasocket library (luasocket-2.0.2) is used

Lua 5.2.2 Copyright (C) 1994-2013 Lua.org, PUC-Rio

-init socksocket = require ("socket") if nil = = socket then print ("socket environment load fail.") Os.exit (); end--init server socketnet_host = "192.168.17.7" net_port = 8888server = socket.bind (net_host,net_port) if nil = = server then print ("server socket bind fail.") Os.exit (); end::io_ret::--wair connectprint ("wait tcp client connect...") client = server:accept (1) if nil = client then print ("tcp client connect fail") os.exit () else print ("tcp client connect succ") endclient:settimeout (0)-init serial portserial_port = "/ dev/ttyS2" serial = io.open (serial_port, "rb") if nil = serial then print ("serial port open fail.") Os.exit () Else print ("serial port open succ") end--serial to tcp proc while true do local ser_byte = nil local net_byte = nil-- network data proc net_str Net_state = client:receive (1)-- check socket if net_state ~ = "closed" then-- check serial port ser_str = serial:read (1) if ser_str then-- serial data valid ser_byte = string.byte (ser_str)-- serial to tcp client:send (string.char (ser_byte))-- debug output io.write (string.format ("\ nSER: [X]\ n") Ser_byte)) end-[if net_str then-- network data valid net_byte = string.byte (net_str)-- tcp to serial Serial:write (100)-- string.char (net_byte)-- debug output io.write (string.format ("\ nNET: [X]\ n" Net_byte)) end]]-- else print ("tcp client closed") client = nil serial = nil goto io_ret endendserial:close () client:close () so far I believe that the "LUA how to achieve serial data acquisition and TCP one-way transparent transmission" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Serial port data one-way data collection script language content parameters bidirectional module network study work practical deeper between interest preface only practicality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Linux vpn Redmi Shulou Technology