标签/分类 "上位机软件开发" 下的文章
QT 十六进制字符串和十六进制之间互转(ByteArray和HexString)
举例说明 ByteArray {0x01,0x02,0x03,0x04,0x05,0x06}; HexString "01 02 03 04 05 06" 代码 Convert.cpp #include "convert.h"#include <QString>#include <QMessageBox>#include <QDebug> Convert::Convert() { } QString Convert::ByteArrayToHexString(QByteArray data) { QString ret(data.toHex().toUpper()); int len = ret.length()/2; for(int i=1;i<=len;i++) { ret.insert(2*i+i-1,""); } return ret; } QByteArray Convert::HexStringToByteArray(QString HexString) { bool ok; QByteArray ret; HexString = HexString.trimmed(); HexString = HexString.simplified(); QStringList sl = HexString.split(""); foreach (QString s, sl) { if(!s.isEmpty()) { char c = s.toInt(&ok,16)&0xFF; if(ok){ ret.append(c); }else{ ret.clear(); //ret = NULL; qDebug()<<"非法的16进制字符:"<&l
C# 多线程操作
1 、带参数多线程 Thread t = new Thread(new ParameterizedThreadStart(ProcessAtCommand)); t.Start(at); private void ProcessAtCommand(Object at1) { AtType at = (AtType)at1; } 2、不带参数多线程 Thread t = new Thread(new ThreadStart(Calculate)); t.Start();
QT-串口操作
修改.pro文件 添加如下所示代码 QT += serialport 编写.h文件 1、添加头文件 #include <QtSerialPort>2、添加串口定义 QSerialPort *serialPort; 源文件代码 1、 添加头文件 #include <QSerialPort>#include <QSerialPortInfo>2、搜索串口 void MainWindow::on_btn_find_seriaport_clicked() { const auto infos = QSerialPortInfo::availablePorts(); ui->cbx_com_name->clear(); for(const QSerialPortInfo &info : infos)
CSharp 一些代码小结--文件对话框
C# 一些代码小结–文件对话框 查看文件完整路径 try { Config cfg = new Config(); var file = ""; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { file = saveFileDialog1.FileName;//(获取整个文件名) cfg.WriteConfigFile(command, file); MessageBox.Show("file:" + file); } } catch (Exception) { OutputMessage("检查软件路径下面是否
CSharp 一些代码小结--UI操作
C# 一些代码小结–UI操作 使用控件名调用控件 object obj = this.GetType().GetField("控件名", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(this); ComboBox cb = (ComboBox)obj; 在控件触发事件时判断触发事件的控件 TextBox text = sender as TextBox; int id = 0; String str = null; switch (text.Name) 跨线程使用委托调用U
CSharp 一些代码小结--使用文件记录日志
C# 一些代码小结–使用文件记录日志 public class FaceLog { public static void AppendInfoLog(string errMsg) { try { string Folder = ".\\log\\"; string fileName = Folder + "Info_" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; if (!System.IO.Directory.Exists(Folder)) System.IO.Directory.CreateDirectory(Folder); if (!File.Exists(fileName)) { FileStream stream = System.IO.File.Create(fileName); stream.Close(); stream.Dispose(); } using (TextWriter fs = new StreamWriter(fileName, true)) { fs.Write(errMsg); fs.Close(); fs.Dispose(); } } catch { } } public static void AppendSerialLog(string errMsg) { try { string Folder = ".\\log\\"; string fileName = Folder + "Serial_" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; if (!System.IO.Directory.Exists(Folder)) System.IO.Directory.CreateDirectory(Folder); if (!File.Exists(fileName)) System.IO.File.Create(fileName); using (TextWriter fs = new StreamWriter(fileName, true)) { fs.Write(errMsg); fs.Close(); fs.Dispose(); } } catch
CSharp 一些代码小结--串口操作
串口解析显示中文 private String SerialPortReadStr() { try { String str = null; int n = serialPort1.BytesToRead;//数据接收缓存区的字接数 byte[] buffer = new byte[n];//创建临时字节数组 serialPort1.Read(buffer, 0, n);//从缓存区读取数据 str = Encoding.GetEncoding("GB2312").GetString(buffer); if(!IsStopReadComData) { UpDateLogTextBox(str); } if (保存串口日志ToolStripM
CSharp 一些代码小结--datGirdView保存到csv文件
if (dataGridView1.Rows.Count == 0) { MessageBox.Show("No data available!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "CSV files (*.csv)|*.csv"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.FileName = null; saveFileDialog.Title = "Save path of the file to be exported"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { Stream myStream = saveFileDialog.OpenFile(); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0)); string strLine = ""; try { //Write in the headers of the columns. for (int i = 0; i < dataGridView1.ColumnCount; i++) { if (i > 0) strLine += ","; strLine += dataGridView1.Columns[i].HeaderText; } strLine.Remove(strLine.Length - 1); sw.WriteLine(strLine); strLine = ""; //Write in the content of the columns. for (int j = 0; j < dataGridView1.Rows.Count; j++) {
CSharp读取json配置文件内容
步骤 读取配置文件转换成字符串,代码如下 string contents = System.IO.File.ReadAllText("config.json"); 注意:该语句会抛出文件不存在异常。 使用Newtonsoft.Json将json字符串转换成类的对象,完整代码如下所示 public class Init { public static InitInfo init = new InitInfo(); public Init(){ try { string contents = System.IO.File.ReadAllText("config.json"); //Shell.WriteLine("ini 配置文件\r\n" + JsonConvert.SerializeObject(init)); init = JsonConvert.DeserializeObject<InitInfo>(contents); S