In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.