java发送串口命令,QT-实现串口命令发送列表(三)

本文档介绍了一个使用C++实现的QT配置文件读取功能,通过自定义的`my_config_file`类实现了文件的读写操作。内容包括初始化配置文件、动态生成命令列表以及在QT界面中展示这些命令。程序首先将配置文件写入AT命令,然后读取文件内容,创建并显示对应的命令列表。该方法简化了命令列表的管理,适用于需要根据配置文件动态更新界面的应用场景。
摘要由CSDN通过智能技术生成

2- 读取配置文件生成命令列表

实现思路:

第一步先实现QT文件读取功能,设计参数列表读取文件

使用c语言的文件读写,使用格式输入输出,方便存取数据

第二步读取配置文件生成命令列表

读取配置文件内容动态生成命令列表

第一步先实现QT文件读取功能,设计参数列表读取文件

因为逛了一圈Qfile, 感觉不熟悉,不太好实现格式输入输出,文件保存使用c语言实现。封装用c++.

My_config_file.h

#include "stdio.h"

#define FILENAME "config.txt"

#define FILETYPE_A "a+"

#define FILETYPE_W "w+"

#define FILETYPE_R "r+"

//按格式写

#define cprintf(fmt, ...) fprintf(config_file.get_cfp(), fmt, ##__VA_ARGS__)

//按格式读

#define cscanf(fmt, ...)  fscanf(config_file.get_cfp(), fmt, ##__VA_ARGS__)

class my_config_file

{

public:

my_config_file();

FILE *fp=NULL;

//初始化

int cmd_config_init(char *filename,char *type);

//关闭

void cmd_config_close();

//是否到文件尾巴

int cfeof();

FILE *get_cfp();

};

extern my_config_file config_file;

#endif // MY_CONFIG_FILE_H

My_config_file.cpp

#include "my_config_file.h"

my_config_file config_file;

my_config_file::my_config_file()

{

}

//初始化

int my_config_file::cmd_config_init(char *filename,char *type)

{

if( (fp=fopen(filename, type)) == NULL )

{

printf("Fail to open file!\n");

return 0;

}

return 1;

}

//关闭

void my_config_file::cmd_config_close()

{

if(fp!=NULL)

fclose(fp);

}

FILE *my_config_file::get_cfp()

{

return fp;

}

//是否到文件尾巴

int my_config_file::cfeof()

{

if(fp==NULL)

return 1;

return feof(fp);

}

Mainwindow.cpp

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include

#include

#include "QDebug"

#include

#include

#include "my_config_file.h"

//定义配置项

typedef struct

{

char name[100];

char comd[100];

}AT_CMD_S;

MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow)

{

ui->setupUi(this);

printf("save...\n");

AT_CMD_S at_cmd1={0};

AT_CMD_S at_cmd2={0};

int n=10;

config_file.cmd_config_init(FILENAME,FILETYPE_W);

while(n--)

{

cprintf("%d %s %s\n",n,"AT","ST+CSQ?");

}

config_file.cmd_config_close();

printf("read...\n");

config_file.cmd_config_init(FILENAME,FILETYPE_R);

while( !config_file.cfeof() )

{

cscanf("%d %s %s\n",&n,at_cmd2.name,at_cmd2.comd);

printf("read n:%d :%s - %s\n",n,at_cmd2.name,at_cmd2.comd);

}

config_file.cmd_config_close();

}

MainWindow::~MainWindow()

{

delete ui;

}

效果:

51a9386c32b357b9f9254f7114261ef8.png

4244e1dd3c9d14bac261d38a0659c01f.png

第二步读取配置文件生成命令列表

有了上面的文件读取接口,实现动态生成命令列表就变得很简单了

我们重新封装一下第一讲实现列表的功能,传入两个字符给 按键 和 编辑框

void MainWindow::add_widget(QString name,QString comd)

{

//创建widget

my_widget *mwidget =new my_widget(this);

mwidget->Button->setText(name);

mwidget->ineEdit->setText(comd);

//把item添加到listWidget

ui->listWidget->addItem(mwidget->item);

//将item与widget联系

ui->listWidget->setItemWidget(mwidget->item, mwidget);

global::Mwidget_List.append(mwidget);

}

Mainwindow中直接读取文件并添加列表

MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow)

{

ui->setupUi(this);

AT_CMD_S at_cmd2={0};

printf("read...\n");

config_file.cmd_config_init(FILENAME,FILETYPE_R);

//读取文件

while( !config_file.cfeof() )

{

int n=0;

cscanf("%d %s %s\n",&n,at_cmd2.name,at_cmd2.comd);

//设置文本和按钮的文本

QString btn_str(at_cmd2.name);

QString edit_str(at_cmd2.comd);

//添加命令

add_widget(btn_str,edit_str);

}

config_file.cmd_config_close();

}

我们在配置文件中手动添加几条指令

affa9a7b68219d5e3eb1d0215062849c.png

程序运行效果,全部读出生成命令列表。完美。

59c3a865733b9b306a229b631fbf2b73.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
短信内容的存储类 /*** * CommonSms 短信用于全局变量 */ public class CommonSms{ /** id */ private int id; /**短信内容*/ private String smstext; /**短信发送方*/ private String sender;//短信发送方 /**短信接收发*/ private String recver;//短信接收发 /**时间*/ private Date date; public String getSmstext() { return smstext; } public void setSmstext(String smstext) { this.smstext = smstext; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public int getId() { return id; } public void setId(Integer id) { this.id = id; } public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } public String getRecver() { return recver; } public void setRecver(String recver) { this.recver = recver; } } 串口操纵实现类 /*** * 串口操纵实现类 */ public class Port { private CommPortIdentifier portId; private SerialPort serialPort; private OutputStreamWriter out; private InputStreamReader in; private String COMname; private static char symbol1 = 13; public String getCOMname() { return COMname; } public void setCOMname(String mname) { COMname = mname; } public CommPortIdentifier getPortId() { return portId; } public void setPortId(CommPortIdentifier portId) { this.portId = portId; } public SerialPort getSerialPort() { return serialPort; } public void setSerialPort(SerialPort serialPort) { this.serialPort = serialPort; } public OutputStreamWriter getOut() { return out; } public void setOut(OutputStreamWriter out) { this.out = out; } public InputStreamReader getIn() { return in; } public void setIn(InputStreamReader in) { this.in = in; } public boolean isused =true; public boolean isIsused() { return isused; } public void setIsused(boolean isused) { this.isused = isused; } /** * 打开com口 * @param portName * @return */ public Port(String portName) { try { portId = CommPortIdentifier.getPortIdentifier(portName); if (portId == null) { System.out.println("port is null"); } try { serialPort = (SerialPort) portId.open(portName,100000); } catch (PortInUseException e) { System.gc(); e.printStackTrace(); } // 下面是得到用于和COM口通讯的输进、输出流。 try { in = new InputStreamReader(serialPort.getInputStream()); out = new OutputStreamWriter(serialPort.getOutputStream()); } catch (IOException e) { System.gc(); System.out.println("IOException"); } // 下面是初始化COM口的传输参数,如传输速率:9600等。 try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); setCOMname(portId.getName()); setIsused(true); } catch (UnsupportedCommOperationException e) { e.printStackTrace(); System.gc(); } } catch (NoSuchPortException e) { e.printStackTrace(); System.gc(); } } /** * 检查SIM是否存在 * @return */ public boolean chakanPort() { try { String atCommand = "AT+ccid"; String strReturn = sendAT(atCommand); if (strReturn.indexOf("OK", 0) != -1) { return true; } return false; } catch (Exception ex) { System.gc(); ex.printStackTrace(); return false; } } /** * 封闭COM口 * @return boolean */ public void close() { try { in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } serialPort.close(); System.gc(); setIsused(false); } /** * 向串口中写进字符串命令 * @param s 字符串命令 * @throws Exception 异常 */ public void writeln(String s) throws Exception { out.write(s); out.write('\r'); out.flush(); } /** * 读取COM命令的返回字符串 * @return 结果字符串 * @throws Exception */ public String read() throws Exception { int n, i; char c; String answer = ""; for (i = 0; i < 100; i++) { while (in.ready()) { n = in.read(); if (n != -1) { c = (char) n; answer = answer + c; Thread.sleep(1); } else break; } if (answer.indexOf("OK") != -1) { break; } Thread.sleep(100); } return answer; } /** * 向串口发送AT指令 * @param atcommand 指令内容 * @return 指令返回结果 * @throws java.rmi.RemoteException */ public String sendAT(String atcommand) throws java.rmi.RemoteException { String s = ""; try { Thread.sleep(100); writeln(atcommand); Thread.sleep(80); s = read(); Thread.sleep(100); } catch (Exception e) { System.gc(); System.out.println("ERROR: send AT command failed; " + "Command: " + atcommand + "; Answer: " + s + " " + e); } return s; } } 短信操纵类 /*** * 短信操纵类 */ public class Sms{ private CommonSms commonsms; private static char symbol1 = 13; private static String strReturn = "", atCommand = ""; public boolean SendSms(Port myport) { if(!myport.isIsused()) { System.out.println("COM通讯端口未正常打开!"); return false; } setMessageMode(myport,1); // 空格 char symbol2 = 34; // ctrl~z 发送指令 char symbol3 = 26; try { atCommand = "AT+CSMP=17,169,0,08" + String.valueOf(symbol1); strReturn = myport.sendAT(atCommand); System.out.println(strReturn); if (strReturn.indexOf("OK", 0) != -1) { atCommand = "AT+CMGS=" + commonsms.getRecver() + String.valueOf(symbol1); strReturn = myport.sendAT(atCommand); atCommand = StringUtil.encodeHex(commonsms.getSmstext().trim()) + String.valueOf(symbol3) + String.valueOf(symbol1); strReturn = myport.sendAT(atCommand); if (strReturn.indexOf("OK") != -1 && strReturn.indexOf("+CMGS") != -1) { System.out.println("短信发送成功..."); return true; } } } catch (Exception ex) { ex.printStackTrace(); System.out.println("短信发送失败..."); return false; } System.out.println("短信发送失败..."); return false; } /** * 设置消息模式 * @param op * 0-pdu 1-text(默认1 文本方式 ) * @return */ public boolean setMessageMode(Port myport,int op) { try { String atCommand = "AT+CMGF=" + String.valueOf(op) + String.valueOf(symbol1); String strReturn = myport.sendAT(atCommand); if (strReturn.indexOf("OK", 0) != -1) { System.out.println("*************文本方式设置成功************"); return true; } return false; } catch (Exception ex) { ex.printStackTrace(); return false; } } /** * 读取所有短信 * @return CommonSms集合 */ public List RecvSmsList(Port myport) { if(!myport.isIsused()) { System.out.println("System Message: COM通讯端口未正常打开!"); return null; } List listMes = new ArrayList(); try { atCommand = "AT+CMGL=\"ALL\""; strReturn = myport.sendAT(atCommand); listMes = StringUtil.analyseArraySMS(strReturn); } catch (Exception ex) { ex.printStackTrace(); } return listMes; } /** * 删除短信 * @param index 短信存储的位置 * @return boolean */ public boolean DeleteSMS(int index,Port myport) { if(!myport.isIsused()){ System.out.println("System Message: COM通讯端口未正常打开!"); return false; } try { atCommand = "AT+CMGD=" + index; strReturn = myport.sendAT(atCommand); if (strReturn.indexOf("OK") != -1) { System.out.println("System Message: 成功删除存储位置为" + index + "的短信......"); } } catch (Exception ex) { ex.printStackTrace(); } return true; } /** * 删除短信中所有短信 * @return boolean */ public boolean DeleteAllSMS(Port myport) { List list=RecvSmsList(myport); boolean ret=true; if(list!=null&&!list.equals("")&&list;.size()>0) { for(int i=0;i<list.size();i++) { CommonSms tempcomsms=(CommonSms)list.get(i); if(!DeleteSMS(tempcomsms.getId(),myport)) { ret=false; } } } return ret; } public CommonSms getCommonsms() { return commonsms; } public void setCommonsms(CommonSms commonsms) { this.commonsms = commonsms; } /** * 号码,内容,发送短信息 * @param phone * @param countstring * @throws Exception */ public static void sendmsn(String phone,String countstring){ Sms s = new Sms(); // 发送测试 CommonSms cs=new CommonSms(); cs.setRecver(phone); cs.setSmstext(countstring); s.setCommonsms(cs); Port myort=new Port("COM7"); s.SendSms(myort); myort.close(); } public static void main(String[] args) throws Exception { sendmsn("13265551149","我有一筐的愿看,却等不到一颗流星,闭上眼睛,我看到了我的前途"); } 指令字符串操纵类 /*** * 指令字符串操纵类 */ public class StringUtil { /** * 使用Sms 的RecvSms(int index)的方法时,使用该方法解析MODEM返回的字符串 * 根据MODEM返回的字符串,解析成一个CommonSms对象 * @param str 串口返回的读取短信结果字符串 * @param index 短信索引 * @return */ public static CommonSms analyseSMS(String str, int index) { CommonSms commonSms = new CommonSms(); String mesContent; String[] s = str.split("\""); int len = s.length; commonSms.setId(index); mesContent = s[len - 1]; if (mesContent.indexOf("OK") != -1) { mesContent = mesContent.substring(0, mesContent.indexOf("OK")); } mesContent = mesContent.trim(); commonSms.setSmstext(analyseStr(mesContent)); // 短信有中文时使用 // mes.setMessage(Unicode2GBK(analyseStr(mesContent))); SimpleDateFormat df = new SimpleDateFormat("yy/MM/dd hh:mm:ss"); String datestring = s[len - 2].substring(0, s[len - 2].length() - 3) .replace(',', ' ');// 短信时间格式09/09/09 20:18:01+32 Date date = null; try { date = df.parse(datestring); System.out.println(date.toLocaleString()); } catch (Exception ex) { System.out.println(ex.getMessage()); } commonSms.setDate(date); if (s[1].equals("REC READ")) { commonSms.setState("已读"); } else { commonSms.setState("未读"); } commonSms.setSender(s[3]); return commonSms; } /** * 使用Sms 的RecvSmsList()方法时,通过该方法解析MODEM返回来的字符串 * 根据MODEM返回的字符串,解析成一个CommonSms的集合对象 * @param str MODEM返回的字符串 * @return */ public static List analyseArraySMS(String str) { List mesList = new ArrayList(); CommonSms cs; String[] messages; String temp; String[] t; if (str.indexOf("CMGL: ") == -1) return null; str = str.substring(0, str.indexOf("OK")).trim(); messages = str.split("\n"); if (messages.length < 2) return null; for (int i = 1; i 5) { cs.setId(Integer.parseInt(t[0].trim())); temp = t[1].substring(t[1].indexOf('"') + 1, t[1].lastIndexOf('"')).trim(); if (temp.equals("REC READ")) { cs.setState("已读"); } else { cs.setState("未读"); } cs.setSender((t[2].substring(t[2].indexOf('"') + 1, t[2] .lastIndexOf('"')).trim())); SimpleDateFormat df = new SimpleDateFormat("yy/MM/dd hh:mm:ss"); String datestring = t[4].substring(t[4].indexOf('"') + 1) + " " + t[5].substring(0, t[5].indexOf('"'));// 短信时间格式09/09/09 // 20:18:01+32 Date date = null; try { date = df.parse(datestring); } catch (Exception ex) { System.out.println(ex.getMessage()); } cs.setDate(date); i++; cs.setSmstext(analyseStr(messages[i].trim())); mesList.add(cs); } } return mesList; } /** * 将PDU编码的十六进制字符串 如“4F60597DFF01” 转换成unicode "\u4F60\u597D\uFF01" * @param str 要转化的字符串 * @return 转换后的十六进制字符串 */ public static String analyseStr(String str) { StringBuffer sb = new StringBuffer(); if (!(str.length() % 4 == 0)) return str; for (int i = 0; i < str.length(); i++) { if (i == 0 || i % 4 == 0) { sb.append("\\u"); } sb.append(str.charAt(i)); } return Unicode2GBK(sb.toString()); } /** * 将unicode编码 "\u4F60\u597D\uFF01" 转换成中文 "你好!" * @param dataStr 要转化的字符串 * @return 转换后的中文字符串 */ public static String Unicode2GBK(String dataStr) { int index = 0; StringBuffer buffer = new StringBuffer(); while (index < dataStr.length()) { if (!"\\u".equals(dataStr.substring(index, index + 2))) { buffer.append(dataStr.charAt(index)); index++; continue; } String charStr = ""; charStr = dataStr.substring(index + 2, index + 6); char letter = 0; try{letter = (char) Integer.parseInt(charStr, 16);}catch (Exception e) {} buffer.append(letter); index += 6; } return buffer.toString(); } /** * 将中文字符串转换成Unicode * @param str 要转换的中文字符串 * @return 转换后的Unicode */ public static String GBK2Unicode(String str) { StringBuffer result = new StringBuffer(); for (int i = 0; i < str.length(); i++) { char chr1 = (char) str.charAt(i); if (!isNeedConvert(chr1)) { result.append(chr1); continue; } try{result.append("\\u" + Integer.toHexString((int) chr1));}catch (Exception e) {} } return result.toString(); } /** * 在中文字符串转换成Unicode方法中判定是否需要转换 * @param para 要转化的字符 * @return boolean */ public static boolean isNeedConvert(char para) { return ((para & (0x00FF)) != para); } /** * 使用Sms 的 SendSms()方法发送短信时,调用此方法将其短信内容转换成十六进制 * @param msg 短信内容 * @return 转换后的十六进制短信 */ public static final String encodeHex(String msg) { byte[] bytes = null; try { bytes = msg.getBytes("GBK"); } catch (java.io.UnsupportedEncodingException e) { e.printStackTrace(); } StringBuffer buff = new StringBuffer(bytes.length * 4); String b = ""; char a; int n = 0; int m = 0; for (int i = 0; i 0) { buff.append("00"); buff.append(b); n = n + 1; } else { a = msg.charAt((i - n) / 2 + n); m = a; try{b = Integer.toHexString(m);}catch (Exception e) {} buff.append(b.substring(0, 4)); i = i + 1; } } return buff.toString(); } }
QT 是一个跨平台的 C++ 应用程序开发框架,而 Modbus 是一种串行通信协议,它通常用于工业自动化领域中的设备之间进行通信。因此,在 QT 中使用 Modbus 串口通信需要使用相应的库来实现QT 中可用的 Modbus 库有很多,其中比较常用的有: 1. QModbus: 这是 QT 官方提供的 Modbus 库,它支持 Modbus TCP 和 RTU 协议,并且提供了一些易于使用的 API 接口。 2. libmodbus: 这是一个开源的 Modbus 库,它支持 Modbus TCP、RTU、ASCII 等协议,并且提供了 C、C++、Python、Java 等多种语言的 API 接口。 在使用这些库之前,需要先了解 Modbus 的通信原理和协议格式,以便正确地配置串口参数和解析数据帧。通常情况下,需要指定串口名称、波特率、数据位、校验位和停止位等参数,并且需要定义 Modbus 的寄存器地址、数据类型和读写方式等信息。 下面是一个简单的 QT Modbus 串口通信示例代码: ```cpp #include <QCoreApplication> #include <QModbusRtuSerialMaster> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QModbusRtuSerialMaster modbus; modbus.setConnectionParameter(QModbusDevice::SerialPortNameParameter, "/dev/ttyUSB0"); modbus.setConnectionParameter(QModbusDevice::SerialParityParameter, QSerialPort::NoParity); modbus.setConnectionParameter(QModbusDevice::SerialBaudRateParameter, 9600); modbus.setConnectionParameter(QModbusDevice::SerialDataBitsParameter, QSerialPort::Data8); modbus.setConnectionParameter(QModbusDevice::SerialStopBitsParameter, QSerialPort::OneStop); if (!modbus.connectDevice()) { qDebug() << "Failed to connect to Modbus device!"; return -1; } QModbusDataUnit request(QModbusDataUnit::HoldingRegisters, 0, 1); auto reply = modbus.sendReadRequest(request, 1); if (reply.error() == QModbusDevice::NoError) { auto value = reply.result().value(0); qDebug() << "Modbus value:" << value; } else { qDebug() << "Modbus error:" << reply.errorString(); } modbus.disconnectDevice(); return a.exec(); } ``` 这段代码使用 QModbusRtuSerialMaster 类来实现 Modbus RTU 协议的串口通信,首先设置串口参数,然后连接设备并发送读取请求,最后解析响应数据并断开连接。需要注意的是,这只是一个简单的示例代码,实际应用中还需要处理异常情况和错误码等信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值