新手上路
- 积分
- 48
- 金钱
- 48
- 注册时间
- 2019-7-13
- 在线时间
- 12 小时
|

楼主 |
发表于 2019-7-13 23:23:40
|
显示全部楼层
示例:UDP发送器
- --构造一个65507的数据
- big_data = string.rep("1", 65507)
- --测试端口名
- test = "test"
- --移除同名端口
- portRemove(test)
- --创建UDP端口并打开
- portCreate(test, "Udp", {port_num = 4000})
- portConnect(test)
- --压力发送数据
- function sendTask(port)
- --记录上次时间和数据量
- local len = 0
- local lastTime = gettime()
- while true do
- --发送数据
- local sbytes, err = portSend(port, "127.0.0.1:2000", big_data)
- --失败处理
- if err ~= 0 then
- print("发送失败!")
- return
- end
- --发送数据量统计
- len = len + sbytes
- --超过300M做一次速率统计
- if len > 300 * 1024 * 1024 then
- --获取当前时间
- local cur = gettime()
- --计算间隔
- local spec = cur - lastTime
- lastTime = cur
- --打印速率
- printf(port..": 当前速率为:%.2f KB/S\n", len * 1000 / 1024 / spec)
- len = 0
- end
- end
- end
- --创建发送任务
- taskStart(sendTask, test)
复制代码
UDP接收器:
- test = "test"
- --创建名为test的UDP端口,为确保创建成功,先尝试移除同名端口
- portRemove(test)
- portCreate(test, "Udp", {port_num = 2000})
- --打开这个端口
- portConnect(test)
- --接收任务
- function recvTask(port)
- --记录时间和数据量
- local len = 0
- local lastTime = gettime()
- while true do
- --接收端口发来的数据
- local data, err, ip = portRecv(port)
- --错误处理
- if err ~= 0 then
- print("接收错误:"..err)
- return
- end
- --统计数据量
- len = len + #data
- --超过300M打印一下速率
- if len > 300 * 1024 * 1024 then
- --获取当前时间,精确到ms
- local cur = gettime()
- --计算间隔
- local spec = cur - lastTime
- lastTime = cur
- --打印速率
- printf(port..": 当前速率为:%.2f KB/S\n", len * 1000 / 1024 / spec)
- len = 0
- end
- end
- end
- --启动接收任务
- taskStart(recvTask, "test")
复制代码
串口自动监听:
当串口插入电脑后,创建任务自动监听其数据并存入文件,串口拔出后,进入等待,串口再次插入后自动连接,并继续接收数据
- --串口配置
- config = {
- baudrate = 115200, --115200波特率
- dataBits = 8, --8位数据位
- stopBits = 1, --1位停止位,当使用1.5时请使用字符串"1.5"
- parity = "N", --无校验
- flowctl = 0 --无流控
- }
- --获取已连接的端口列表
- local connList = portConnectedListGet()
- --遍历当前已连接的端口,断开其中的串口端口
- for k, v in pairs(connList) do
- --断开类型为串口的端口
- if portInfo(k).type == "Serial" then
- portRemove(k)
- end
- end
- --串口数据接收任务
- function recvTask(port)
- --记录数据到文件中
- local dataRecordFile = io.open(port.."_RECORD", "wb")
- if dataRecordFile == nil then
- --文件打开失败
- printf("文件%s打开失败,无法记录当前串口的数据!\n", port.."RECORD")
- end
- --不停的接收来自串口的任务
- while true do
- local data, err = portRecv(port)
- --发生错误
- if err ~= 0 then
- while true do
- --等待端口连接
- local info = portInfo(port)
- --被销毁
- if info.state == "destroyed" then
- print(port..": 已销毁!")
- dataRecordFile:close()
- return
- --已连接上
- elseif info.state == "connected" then
- print(port..": 已连接!")
- break
- end
- --等待状态变化
- portEventWait(port)
- end
- else
- --记录数据
- dataRecordFile:write(data)
- --打印数据
- print(port..":"..data)
- end
- end
- end
- --为当前插上的串口创建接收任务
- while true do
- --获取串口列表
- local serialList = serialListGet()
- --创建名为端口号的端口
- for k, v in pairs(serialList) do
- --获取将要创建的端口的信息
- local info = portInfo(k)
- --这个端口没有创建
- if info.state == "destroyed" or info.type ~= "Serial" then
- --尝试移除这个端口
- portRemove(k)
- --创建这个串口
- config.port = k
- portCreate(k, "Serial", config)
- --连接这个端口
- portConnect(k)
- --创建接收任务
- taskStart(recvTask, k)
- --连接断开的端口
- elseif info.state == "disconnected" then
- portConnect(k)
- end
- end
- --等待串口列表改变
- serialListChangedWait()
- --WinXP需要延时一下才能操作
- sleep(200)
- end
复制代码
TCP回显服务器:
客户端连接上后,发送的所有数据将回显至客户端,支持任意多个客户端
- --服务器名称
- server = "test"
- --创建TCP服务器,并打开
- portRemove(server)
- portCreate(server, "TcpServer", {port_num = 4000})
- portConnect(server)
- --TCP客户端回显任务
- function client_echo_task(client)
- --print(client.."已连接!")
- while true do
- --等待客户端发送数据
- local data, err = portRecv(client)
- --回显
- if err == 0 then
- portSend(client, data)
- --print(client..":"..data)
- else
- --print(client.."读出错:"..err)
- break
- end
- end
- end
- while true do
- --等待客户端接入
- local client, err = portRecv(server)
- if err == 0 then
- --创建回显任务
- taskStart(client_echo_task, client)
- else
- print(server.."服务器错误:"..err)
- break
- end
- end
复制代码
TCP客户端:
- --测试客户端
- client = "test"
- --创建端口,并连接服务器
- portRemove(client)
- portCreate(client, "TcpClient", {host = "localhost", port_num = 4000})
- portConnect(client)
- --接收服务器数据任务
- function recvTask(port)
- while true do
- local data, err = portRecv(port)
- if err ~= 0 then
- print("客户端接收出错!")
- exit()
- return
- end
- print(data)
- end
- end
- --启动接收任务
- taskStart(recvTask, client)
- while true do
- --每隔1s发送一包数据
- portSend(client, "Hello..")
- sleep(1000)
- end
复制代码
每个1s自动向服务器发送 "Hello .."
更多示例代码请见分享 |
|