当前位置: 首页 > wzjs >正文

西宁网站建设有限公司湖北城乡建设部网站首页

西宁网站建设有限公司,湖北城乡建设部网站首页,广州微商城开发公司,免费360地图手机版1、配置 线路连接:pc通过网线连接USR-TCP232-410S,USR-TCP232-410S另一端使用RS232串口连接麦创可编程直流电源。 电源设置:波特率9600,SCPI协议,命令模式:CRLF USR-TCP232-410S设置 2、 程序 2.1、pom…

1、配置

线路连接:pc通过网线连接USR-TCP232-410S,USR-TCP232-410S另一端使用RS232串口连接麦创可编程直流电源。

电源设置:波特率9600,SCPI协议,命令模式:CR+LF

USR-TCP232-410S设置

2、 程序

2.1、pom.xml

        <!--  netty      --><dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.86.Final</version></dependency>

2.2 、配置文件

tcp:client:host: 192.168.0.7port: 502timeout: 5000pool:maxTotal: 10maxIdle: 5minIdle: 2

2.3、配置类

package com.base.tcp;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;@Data
@Configuration
@ConfigurationProperties(prefix = "tcp.client")
public class TcpClientConfig {private String host;private int port;private Pool pool;private int timeout;// Getters and Setters@Datapublic static class Pool {private int maxTotal;private int maxIdle;private int minIdle;// Getters and Setters}
}

2.4、nettyclient

package com.base.tcp;import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.Promise;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;@Slf4j
@Component
public class NettyClient {private final TcpClientConfig config;private Channel channel;private EventLoopGroup workerGroup;private final ResponseHandler responseHandler = new ResponseHandler();public NettyClient(TcpClientConfig config) {this.config = config;}@PostConstructpublic void start() throws InterruptedException {workerGroup = new NioEventLoopGroup();try {Bootstrap bootstrap = new Bootstrap();bootstrap.group(workerGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) {ChannelPipeline pipeline = ch.pipeline();// 使用换行符作为消息分隔符ByteBuf delimiter = Unpooled.copiedBuffer("\n".getBytes());pipeline.addLast(new DelimiterBasedFrameDecoder(8192, delimiter));//pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));pipeline.addLast(new IdleStateHandler(0, 10, 0, TimeUnit.SECONDS));pipeline.addLast(responseHandler);}});ChannelFuture future = bootstrap.connect(config.getHost(), config.getPort()).sync();channel = future.channel();channel.closeFuture().addListener(f -> workerGroup.shutdownGracefully());} catch (InterruptedException e) {workerGroup.shutdownGracefully();throw e;}}public String sendSync(String command)throws TimeoutException, ExecutionException, InterruptedException {// 添加终止符String fullCommand = command.endsWith("\r\n") ? command : command +"\r\n";Promise<String> promise = workerGroup.next().newPromise();log.info("send command==> {}", fullCommand);responseHandler.setCurrentPromise(promise);channel.writeAndFlush(fullCommand).addListener(f -> {if (!f.isSuccess()) {promise.tryFailure(f.cause());}});return promise.get(5, TimeUnit.SECONDS);}public void sendSyncNoResponse(String command){// 添加终止符String fullCommand = command.endsWith("\r\n") ? command : command +"\r\n";log.info("send command==> {}", fullCommand);channel.writeAndFlush(fullCommand);}@ChannelHandler.Sharableprivate static class ResponseHandler extends ChannelInboundHandlerAdapter {private Promise<String> currentPromise;public void setCurrentPromise(Promise<String> promise) {this.currentPromise = promise;}@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) {log.info("receive msg ==> {}", msg);if (currentPromise != null) {String response = (String) msg;currentPromise.trySuccess(response.trim());currentPromise = null;}}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {if (currentPromise != null) {currentPromise.tryFailure(cause);currentPromise = null;}ctx.close();}@Overridepublic void userEventTriggered(ChannelHandlerContext ctx, Object evt) {// 空闲时发送查询保持连接ctx.writeAndFlush("*IDN?\n");}}}

 2.5、service

package com.web.task.service;import java.util.List;public interface LowerVoltageSourceService {List<Double> getVoltageAll();List<Double> getCurrentAll();void setVoltage(int channel, double voltage);void setCurrent(int channel, double current);void setVoltages(List<Double> voltageList);void setCurrents(List<Double> currentList);void setAllChannelOut(boolean open);void setChannelOut(int channel, boolean open);}

2.6、ServiceImpl

package com.web.task.service.impl;import com.base.tcp.NettyClient;import com.base.utils.TimeUtil;
import com.web.task.service.LowerVoltageSourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;@Slf4j
@Service
public class LowerVoltageSourceServiceImpl implements LowerVoltageSourceService {@Autowiredprivate  NettyClient nettyClient;/*** 麦创SPI命令模式 CR+LF*/private List<Double> getFromDevice(String command){List<Double> resultList = new ArrayList<>();try{String response = nettyClient.sendSync(command);resultList = Arrays.stream(response.split(","))       // 分割字符串.map(String::trim)                   // 去除前后空格.map(Double::parseDouble)            // 转换为Double.collect(Collectors.toList());       // 收集为List}catch (Exception e){log.info("LV has error =>{}", Arrays.toString(e.getStackTrace()));log.info("LV error message =>{}",e.getMessage());}return resultList;}private void exeCommand(String command){try{nettyClient.sendSyncNoResponse(command);}catch (Exception e){log.info("LV has error =>{}", Arrays.toString(e.getStackTrace()));log.info("LV error message =>{}",e.getMessage());}}/***此命令同时查询电源三通道的实际输出电压值*/@Overridepublic List<Double> getVoltageAll(){String command = "MEAS:VOLT:ALL?";return getFromDevice(command);}/***此命令同时查询电源三通道的实际输出电流值*/@Overridepublic List<Double> getCurrentAll() {String command = "MEAS:CURR:ALL?";return getFromDevice(command);}@Overridepublic void setVoltage(int channel, double voltage) {String command1 = "INST "+ channel;String command2 = "VOLT "+ voltage;exeCommand(command1);TimeUtil.delayMillisecond(30);exeCommand(command2);}@Overridepublic void setCurrent(int channel, double current) {String command1 = "INST "+ channel;String command2 = "CURR "+ current;exeCommand(command1);UnisicTimeUtil.delayMillisecond(30);exeCommand(command2);}@Overridepublic void setVoltages(List<Double> voltageList) {String voltages = voltageList.stream().map(String::valueOf)  // 将Double转为String.collect(Collectors.joining(","));String command = "APP:VOLT "+ voltages;exeCommand(command);}@Overridepublic void setCurrents(List<Double> currentList) {String currents = currentList.stream().map(String::valueOf)  // 将Double转为String.collect(Collectors.joining(","));String command = "APP:CURR "+ currents;exeCommand(command);}@Overridepublic void setAllChannelOut(boolean open) {// 全关String command = "APP:OUT 0,0,0,0,0";if(open){// 全开command = "APP:OUT 1,1,1,1,1";}exeCommand(command);}@Overridepublic void setChannelOut(int channel, boolean open) {String command1 = "INST "+ channel;String command2 = "OUTP 0";if(open){command2 = "OUTP 1";}exeCommand(command1);TimeUtil.delayMillisecond(30);exeCommand(command2);}
}

2.7、测试

package com.web.controller;import com.base.response.ResponseMessage;
import com.web.task.service.LowerVoltageSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Arrays;
import java.util.List;@RestController
@RequestMapping("/netty")
public class TestNettyController {@Autowiredprivate LowerVoltageSourceService lowerVoltageSourceService;@GetMapping("/getVoltage")public ResponseMessage<?> getVoltage() {return ResponseMessage.ok(lowerVoltageSourceService.getVoltageAll());}@GetMapping("/getCurrent")public ResponseMessage<?> getCurrent() {return ResponseMessage.ok(lowerVoltageSourceService.getCurrentAll());}@GetMapping("/setVoltage/{channel}/{votagel}")public void setVoltage(@PathVariable int channel, @PathVariable double votagel) {lowerVoltageSourceService.setVoltage(channel, votagel);}@GetMapping("/setCurrent/{channel}/{current}")public void setCurrent(@PathVariable int channel, @PathVariable double current) {lowerVoltageSourceService.setCurrent(channel, current);}@GetMapping("/setVoltages")public void setVoltages() {List<Double> Voltages = Arrays.asList(11.1d, 22.2d, 33.3d, 44.4, 50.0);lowerVoltageSourceService.setVoltages(Voltages);}@GetMapping("/setCurrents")public void setCurrent() {List<Double> currents = Arrays.asList(1.1, 2.2, 3.3, 4.4, 5.0);lowerVoltageSourceService.setCurrents(currents);}
}

http://www.dtcms.com/wzjs/742456.html

相关文章:

  • 企业网站建设项目描述免费响应式网站模板
  • 网站建设报什么专业莱州人才网
  • 那个网站平台可以做兼职阿里云做网站号码
  • python做网站比php网站绝对路径301
  • 携程网站建设的优缺点网站建设属于什么专业
  • 宁波怎么做外贸公司网站如何在自己电脑上做网站服务器
  • 网站建设怎设计黑糖WordPress主题
  • 免费设计装修公司网站邯郸网站设计
  • 如何规划建设一个企业网站长春建设平台网站的公司
  • 郑州哪个网站建设最好微信小程序 创建网站
  • 手机网站建设的背景建立网站要钱吗?
  • 上海做原创网站cgi做网站
  • 莆田建设信息网站seo招聘的关键词
  • 江苏省交通建设厅门户网站建筑工程网首页
  • wordpress企业站模板网页设计基础教程上机实训
  • 淅川网站建设网站文章只被收录网站首页
  • 网站后台文章排版seo怎么给网站做外链
  • 佛山企业网站建设电话百度热词搜索指数
  • 天河微网站建设想要导航页推广(推广页)
  • 怎样选择高性价比的建站公司wordpress获取文章一个tag标签
  • 比较正规的招聘网站富利建设集团有限公司网站
  • 网站建设 语言网络公司logo
  • 2003总是说网站建设中百度做网站好吗
  • 网站开发的学习路线廊坊网站建设技术托管
  • 广西医科大学网站建设企业案例网站生成
  • 网站域名备案时间简介常用的网页制作工具
  • 服务器网站目录凡客诚品官方网站的代码
  • wordpress检测seo网站建站公司的主页
  • 网站底部图片代码推广网站的广告怎样做
  • 顺德定制网站设计关键词搜索量查询