谱仪数据处理逻辑

上传逻辑
This commit is contained in:
copper 2025-06-23 16:33:31 +08:00
parent 47c339f1b3
commit d21391fb0c
6 changed files with 958 additions and 759 deletions

486
Form1.Designer.cs generated
View File

@ -22,333 +22,273 @@ partial class Form1
#region Windows Form Designer generated code
private System.Windows.Forms.GroupBox groupBoxGPS;
private System.Windows.Forms.ComboBox comboBoxGPSPort;
private System.Windows.Forms.Label labelGPSPort;
private System.Windows.Forms.GroupBox groupBoxSpectrometer;
private System.Windows.Forms.NumericUpDown numericUpDownRefreshInterval;
private System.Windows.Forms.Label labelRefreshInterval;
private System.Windows.Forms.ComboBox comboBoxSpectrometerPort;
private System.Windows.Forms.Label labelSpectrometerPort;
private System.Windows.Forms.GroupBox groupBoxMQTT;
private System.Windows.Forms.TextBox textBoxDeviceSecret;
private System.Windows.Forms.Label labelDeviceSecret;
private System.Windows.Forms.TextBox textBoxDeviceName;
private System.Windows.Forms.Label labelDeviceName;
private System.Windows.Forms.TextBox textBoxTopicRoot;
private System.Windows.Forms.Label labelTopicRoot;
private System.Windows.Forms.TextBox textBoxProductKey;
private System.Windows.Forms.Label labelProductKey;
private System.Windows.Forms.Button buttonStartStop;
private System.Windows.Forms.TextBox textBoxLog;
private System.Windows.Forms.Label labelLog;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.cmbPort1 = new System.Windows.Forms.ComboBox();
this.cmbBaud1 = new System.Windows.Forms.ComboBox();
this.btnConnect1 = new System.Windows.Forms.Button();
this.lblPort1 = new System.Windows.Forms.Label();
this.lblBaud1 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.cmbPort2 = new System.Windows.Forms.ComboBox();
this.cmbBaud2 = new System.Windows.Forms.ComboBox();
this.btnConnect2 = new System.Windows.Forms.Button();
this.lblPort2 = new System.Windows.Forms.Label();
this.lblBaud2 = new System.Windows.Forms.Label();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.txtUrl = new System.Windows.Forms.TextBox();
this.lblUrl = new System.Windows.Forms.Label();
this.btnStartUpload = new System.Windows.Forms.Button();
this.btnStopUpload = new System.Windows.Forms.Button();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.txtLog = new System.Windows.Forms.TextBox();
this.btnClearLog = new System.Windows.Forms.Button();
this.btnSaveLog = new System.Windows.Forms.Button();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.groupBox4.SuspendLayout();
this.statusStrip1.SuspendLayout();
this.groupBoxGPS = new System.Windows.Forms.GroupBox();
this.comboBoxGPSPort = new System.Windows.Forms.ComboBox();
this.labelGPSPort = new System.Windows.Forms.Label();
this.groupBoxSpectrometer = new System.Windows.Forms.GroupBox();
this.numericUpDownRefreshInterval = new System.Windows.Forms.NumericUpDown();
this.labelRefreshInterval = new System.Windows.Forms.Label();
this.comboBoxSpectrometerPort = new System.Windows.Forms.ComboBox();
this.labelSpectrometerPort = new System.Windows.Forms.Label();
this.groupBoxMQTT = new System.Windows.Forms.GroupBox();
this.textBoxDeviceSecret = new System.Windows.Forms.TextBox();
this.labelDeviceSecret = new System.Windows.Forms.Label();
this.textBoxDeviceName = new System.Windows.Forms.TextBox();
this.labelDeviceName = new System.Windows.Forms.Label();
this.textBoxTopicRoot = new System.Windows.Forms.TextBox();
this.labelTopicRoot = new System.Windows.Forms.Label();
this.textBoxProductKey = new System.Windows.Forms.TextBox();
this.labelProductKey = new System.Windows.Forms.Label();
this.buttonStartStop = new System.Windows.Forms.Button();
this.textBoxLog = new System.Windows.Forms.TextBox();
this.labelLog = new System.Windows.Forms.Label();
this.groupBoxGPS.SuspendLayout();
this.groupBoxSpectrometer.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownRefreshInterval)).BeginInit();
this.groupBoxMQTT.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
// groupBoxGPS
//
this.groupBox1.Controls.Add(this.lblBaud1);
this.groupBox1.Controls.Add(this.lblPort1);
this.groupBox1.Controls.Add(this.btnConnect1);
this.groupBox1.Controls.Add(this.cmbBaud1);
this.groupBox1.Controls.Add(this.cmbPort1);
this.groupBox1.Location = new System.Drawing.Point(12, 12);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(200, 120);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "串口1";
this.groupBoxGPS.Controls.Add(this.comboBoxGPSPort);
this.groupBoxGPS.Controls.Add(this.labelGPSPort);
this.groupBoxGPS.Location = new System.Drawing.Point(12, 12);
this.groupBoxGPS.Name = "groupBoxGPS";
this.groupBoxGPS.Size = new System.Drawing.Size(200, 80);
this.groupBoxGPS.TabIndex = 0;
this.groupBoxGPS.TabStop = false;
this.groupBoxGPS.Text = "GPS设置";
//
// cmbPort1
// comboBoxGPSPort
//
this.cmbPort1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbPort1.FormattingEnabled = true;
this.cmbPort1.Location = new System.Drawing.Point(60, 25);
this.cmbPort1.Name = "cmbPort1";
this.cmbPort1.Size = new System.Drawing.Size(121, 23);
this.cmbPort1.TabIndex = 0;
this.comboBoxGPSPort.FormattingEnabled = true;
this.comboBoxGPSPort.Location = new System.Drawing.Point(70, 30);
this.comboBoxGPSPort.Name = "comboBoxGPSPort";
this.comboBoxGPSPort.Size = new System.Drawing.Size(121, 28);
this.comboBoxGPSPort.TabIndex = 1;
//
// cmbBaud1
// labelGPSPort
//
this.cmbBaud1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbBaud1.FormattingEnabled = true;
this.cmbBaud1.Items.AddRange(new object[] {
"9600",
"19200",
"38400",
"57600",
"115200"});
this.cmbBaud1.Location = new System.Drawing.Point(60, 54);
this.cmbBaud1.Name = "cmbBaud1";
this.cmbBaud1.Size = new System.Drawing.Size(121, 23);
this.cmbBaud1.TabIndex = 1;
this.labelGPSPort.AutoSize = true;
this.labelGPSPort.Location = new System.Drawing.Point(6, 33);
this.labelGPSPort.Name = "labelGPSPort";
this.labelGPSPort.Size = new System.Drawing.Size(54, 20);
this.labelGPSPort.TabIndex = 0;
this.labelGPSPort.Text = "串口:";
//
// btnConnect1
// groupBoxSpectrometer
//
this.btnConnect1.Location = new System.Drawing.Point(60, 83);
this.btnConnect1.Name = "btnConnect1";
this.btnConnect1.Size = new System.Drawing.Size(75, 23);
this.btnConnect1.TabIndex = 2;
this.btnConnect1.Text = "连接";
this.btnConnect1.UseVisualStyleBackColor = true;
this.btnConnect1.Click += new System.EventHandler(this.btnConnect1_Click);
this.groupBoxSpectrometer.Controls.Add(this.numericUpDownRefreshInterval);
this.groupBoxSpectrometer.Controls.Add(this.labelRefreshInterval);
this.groupBoxSpectrometer.Controls.Add(this.comboBoxSpectrometerPort);
this.groupBoxSpectrometer.Controls.Add(this.labelSpectrometerPort);
this.groupBoxSpectrometer.Location = new System.Drawing.Point(230, 12);
this.groupBoxSpectrometer.Name = "groupBoxSpectrometer";
this.groupBoxSpectrometer.Size = new System.Drawing.Size(250, 80);
this.groupBoxSpectrometer.TabIndex = 1;
this.groupBoxSpectrometer.TabStop = false;
this.groupBoxSpectrometer.Text = "谱仪设置";
//
// lblPort1
// numericUpDownRefreshInterval
//
this.lblPort1.AutoSize = true;
this.lblPort1.Location = new System.Drawing.Point(15, 28);
this.lblPort1.Name = "lblPort1";
this.lblPort1.Size = new System.Drawing.Size(32, 15);
this.lblPort1.TabIndex = 3;
this.lblPort1.Text = "端口";
this.numericUpDownRefreshInterval.Location = new System.Drawing.Point(190, 50);
this.numericUpDownRefreshInterval.Maximum = new decimal(new int[] { 60, 0, 0, 0 });
this.numericUpDownRefreshInterval.Minimum = new decimal(new int[] { 1, 0, 0, 0 });
this.numericUpDownRefreshInterval.Name = "numericUpDownRefreshInterval";
this.numericUpDownRefreshInterval.Size = new System.Drawing.Size(50, 27);
this.numericUpDownRefreshInterval.TabIndex = 3;
this.numericUpDownRefreshInterval.Value = new decimal(new int[] { 1, 0, 0, 0 });
//
// lblBaud1
// labelRefreshInterval
//
this.lblBaud1.AutoSize = true;
this.lblBaud1.Location = new System.Drawing.Point(15, 57);
this.lblBaud1.Name = "lblBaud1";
this.lblBaud1.Size = new System.Drawing.Size(44, 15);
this.lblBaud1.TabIndex = 4;
this.lblBaud1.Text = "波特率";
this.labelRefreshInterval.AutoSize = true;
this.labelRefreshInterval.Location = new System.Drawing.Point(6, 52);
this.labelRefreshInterval.Name = "labelRefreshInterval";
this.labelRefreshInterval.Size = new System.Drawing.Size(178, 20);
this.labelRefreshInterval.TabIndex = 2;
this.labelRefreshInterval.Text = "刷新间隔(秒)";
//
// groupBox2
// comboBoxSpectrometerPort
//
this.groupBox2.Controls.Add(this.lblBaud2);
this.groupBox2.Controls.Add(this.lblPort2);
this.groupBox2.Controls.Add(this.btnConnect2);
this.groupBox2.Controls.Add(this.cmbBaud2);
this.groupBox2.Controls.Add(this.cmbPort2);
this.groupBox2.Location = new System.Drawing.Point(230, 12);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(200, 120);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "串口2";
this.comboBoxSpectrometerPort.FormattingEnabled = true;
this.comboBoxSpectrometerPort.Location = new System.Drawing.Point(70, 20);
this.comboBoxSpectrometerPort.Name = "comboBoxSpectrometerPort";
this.comboBoxSpectrometerPort.Size = new System.Drawing.Size(121, 28);
this.comboBoxSpectrometerPort.TabIndex = 1;
//
// cmbPort2
// labelSpectrometerPort
//
this.cmbPort2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbPort2.FormattingEnabled = true;
this.cmbPort2.Location = new System.Drawing.Point(60, 25);
this.cmbPort2.Name = "cmbPort2";
this.cmbPort2.Size = new System.Drawing.Size(121, 23);
this.cmbPort2.TabIndex = 0;
this.labelSpectrometerPort.AutoSize = true;
this.labelSpectrometerPort.Location = new System.Drawing.Point(6, 23);
this.labelSpectrometerPort.Name = "labelSpectrometerPort";
this.labelSpectrometerPort.Size = new System.Drawing.Size(54, 20);
this.labelSpectrometerPort.TabIndex = 0;
this.labelSpectrometerPort.Text = "串口:";
//
// cmbBaud2
// groupBoxMQTT
//
this.cmbBaud2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbBaud2.FormattingEnabled = true;
this.cmbBaud2.Items.AddRange(new object[] {
"9600",
"19200",
"38400",
"57600",
"115200"});
this.cmbBaud2.Location = new System.Drawing.Point(60, 54);
this.cmbBaud2.Name = "cmbBaud2";
this.cmbBaud2.Size = new System.Drawing.Size(121, 23);
this.cmbBaud2.TabIndex = 1;
this.groupBoxMQTT.Controls.Add(this.textBoxDeviceSecret);
this.groupBoxMQTT.Controls.Add(this.labelDeviceSecret);
this.groupBoxMQTT.Controls.Add(this.textBoxDeviceName);
this.groupBoxMQTT.Controls.Add(this.labelDeviceName);
this.groupBoxMQTT.Controls.Add(this.textBoxTopicRoot);
this.groupBoxMQTT.Controls.Add(this.labelTopicRoot);
this.groupBoxMQTT.Controls.Add(this.textBoxProductKey);
this.groupBoxMQTT.Controls.Add(this.labelProductKey);
this.groupBoxMQTT.Location = new System.Drawing.Point(500, 12);
this.groupBoxMQTT.Name = "groupBoxMQTT";
this.groupBoxMQTT.Size = new System.Drawing.Size(300, 150);
this.groupBoxMQTT.TabIndex = 2;
this.groupBoxMQTT.TabStop = false;
this.groupBoxMQTT.Text = "MQTT设置";
//
// btnConnect2
// textBoxDeviceSecret
//
this.btnConnect2.Location = new System.Drawing.Point(60, 83);
this.btnConnect2.Name = "btnConnect2";
this.btnConnect2.Size = new System.Drawing.Size(75, 23);
this.btnConnect2.TabIndex = 2;
this.btnConnect2.Text = "连接";
this.btnConnect2.UseVisualStyleBackColor = true;
this.btnConnect2.Click += new System.EventHandler(this.btnConnect2_Click);
this.textBoxDeviceSecret.Location = new System.Drawing.Point(100, 115);
this.textBoxDeviceSecret.Name = "textBoxDeviceSecret";
this.textBoxDeviceSecret.Size = new System.Drawing.Size(190, 27);
this.textBoxDeviceSecret.TabIndex = 7;
this.textBoxDeviceSecret.Text = "1031a49a4f61c29a086f79b41ed971c7";
//
// lblPort2
// labelDeviceSecret
//
this.lblPort2.AutoSize = true;
this.lblPort2.Location = new System.Drawing.Point(15, 28);
this.lblPort2.Name = "lblPort2";
this.lblPort2.Size = new System.Drawing.Size(32, 15);
this.lblPort2.TabIndex = 3;
this.lblPort2.Text = "端口";
this.labelDeviceSecret.AutoSize = true;
this.labelDeviceSecret.Location = new System.Drawing.Point(6, 118);
this.labelDeviceSecret.Name = "labelDeviceSecret";
this.labelDeviceSecret.Size = new System.Drawing.Size(88, 20);
this.labelDeviceSecret.TabIndex = 6;
this.labelDeviceSecret.Text = "设备密钥:";
//
// lblBaud2
// textBoxDeviceName
//
this.lblBaud2.AutoSize = true;
this.lblBaud2.Location = new System.Drawing.Point(15, 57);
this.lblBaud2.Name = "lblBaud2";
this.lblBaud2.Size = new System.Drawing.Size(44, 15);
this.lblBaud2.TabIndex = 4;
this.lblBaud2.Text = "波特率";
this.textBoxDeviceName.Location = new System.Drawing.Point(100, 85);
this.textBoxDeviceName.Name = "textBoxDeviceName";
this.textBoxDeviceName.Size = new System.Drawing.Size(190, 27);
this.textBoxDeviceName.TabIndex = 5;
this.textBoxDeviceName.Text = "PubDevice";
//
// groupBox3
// labelDeviceName
//
this.groupBox3.Controls.Add(this.btnStopUpload);
this.groupBox3.Controls.Add(this.btnStartUpload);
this.groupBox3.Controls.Add(this.lblUrl);
this.groupBox3.Controls.Add(this.txtUrl);
this.groupBox3.Location = new System.Drawing.Point(450, 12);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(320, 120);
this.groupBox3.TabIndex = 2;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "数据上传";
this.labelDeviceName.AutoSize = true;
this.labelDeviceName.Location = new System.Drawing.Point(6, 88);
this.labelDeviceName.Name = "labelDeviceName";
this.labelDeviceName.Size = new System.Drawing.Size(88, 20);
this.labelDeviceName.TabIndex = 4;
this.labelDeviceName.Text = "设备名称:";
//
// txtUrl
// textBoxTopicRoot
//
this.txtUrl.Location = new System.Drawing.Point(15, 40);
this.txtUrl.Name = "txtUrl";
this.txtUrl.Size = new System.Drawing.Size(290, 23);
this.txtUrl.TabIndex = 0;
this.txtUrl.Text = "http://localhost:8080/api/data";
this.textBoxTopicRoot.Location = new System.Drawing.Point(100, 55);
this.textBoxTopicRoot.Name = "textBoxTopicRoot";
this.textBoxTopicRoot.Size = new System.Drawing.Size(190, 27);
this.textBoxTopicRoot.TabIndex = 3;
this.textBoxTopicRoot.Text = "a10inDdCRS6";
//
// lblUrl
// labelTopicRoot
//
this.lblUrl.AutoSize = true;
this.lblUrl.Location = new System.Drawing.Point(15, 22);
this.lblUrl.Name = "lblUrl";
this.lblUrl.Size = new System.Drawing.Size(56, 15);
this.lblUrl.TabIndex = 1;
this.lblUrl.Text = "上传地址";
this.labelTopicRoot.AutoSize = true;
this.labelTopicRoot.Location = new System.Drawing.Point(6, 58);
this.labelTopicRoot.Name = "labelTopicRoot";
this.labelTopicRoot.Size = new System.Drawing.Size(88, 20);
this.labelTopicRoot.TabIndex = 2;
this.labelTopicRoot.Text = "主题根路径:";
//
// btnStartUpload
// textBoxProductKey
//
this.btnStartUpload.Location = new System.Drawing.Point(15, 80);
this.btnStartUpload.Name = "btnStartUpload";
this.btnStartUpload.Size = new System.Drawing.Size(75, 23);
this.btnStartUpload.TabIndex = 2;
this.btnStartUpload.Text = "开始上传";
this.btnStartUpload.UseVisualStyleBackColor = true;
this.btnStartUpload.Click += new System.EventHandler(this.btnStartUpload_Click);
this.textBoxProductKey.Location = new System.Drawing.Point(100, 25);
this.textBoxProductKey.Name = "textBoxProductKey";
this.textBoxProductKey.Size = new System.Drawing.Size(190, 27);
this.textBoxProductKey.TabIndex = 1;
this.textBoxProductKey.Text = "gfcq950RDqt";
//
// btnStopUpload
// labelProductKey
//
this.btnStopUpload.Location = new System.Drawing.Point(110, 80);
this.btnStopUpload.Name = "btnStopUpload";
this.btnStopUpload.Size = new System.Drawing.Size(75, 23);
this.btnStopUpload.TabIndex = 3;
this.btnStopUpload.Text = "停止上传";
this.btnStopUpload.UseVisualStyleBackColor = true;
this.btnStopUpload.Click += new System.EventHandler(this.btnStopUpload_Click);
this.labelProductKey.AutoSize = true;
this.labelProductKey.Location = new System.Drawing.Point(6, 28);
this.labelProductKey.Name = "labelProductKey";
this.labelProductKey.Size = new System.Drawing.Size(88, 20);
this.labelProductKey.TabIndex = 0;
this.labelProductKey.Text = "产品密钥:";
//
// groupBox4
// buttonStartStop
//
this.groupBox4.Controls.Add(this.btnSaveLog);
this.groupBox4.Controls.Add(this.btnClearLog);
this.groupBox4.Controls.Add(this.txtLog);
this.groupBox4.Location = new System.Drawing.Point(12, 150);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(758, 270);
this.groupBox4.TabIndex = 3;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "数据日志";
this.buttonStartStop.Font = new System.Drawing.Font("Microsoft YaHei UI", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point);
this.buttonStartStop.Location = new System.Drawing.Point(12, 110);
this.buttonStartStop.Name = "buttonStartStop";
this.buttonStartStop.Size = new System.Drawing.Size(120, 50);
this.buttonStartStop.TabIndex = 3;
this.buttonStartStop.Text = "开始";
this.buttonStartStop.UseVisualStyleBackColor = true;
this.buttonStartStop.Click += new System.EventHandler(this.buttonStartStop_Click);
//
// txtLog
// textBoxLog
//
this.txtLog.Location = new System.Drawing.Point(15, 22);
this.txtLog.Multiline = true;
this.txtLog.Name = "txtLog";
this.txtLog.ReadOnly = true;
this.txtLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtLog.Size = new System.Drawing.Size(728, 210);
this.txtLog.TabIndex = 0;
this.textBoxLog.Location = new System.Drawing.Point(12, 200);
this.textBoxLog.Multiline = true;
this.textBoxLog.Name = "textBoxLog";
this.textBoxLog.ReadOnly = true;
this.textBoxLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBoxLog.Size = new System.Drawing.Size(788, 250);
this.textBoxLog.TabIndex = 4;
//
// btnClearLog
// labelLog
//
this.btnClearLog.Location = new System.Drawing.Point(15, 238);
this.btnClearLog.Name = "btnClearLog";
this.btnClearLog.Size = new System.Drawing.Size(75, 23);
this.btnClearLog.TabIndex = 1;
this.btnClearLog.Text = "清空日志";
this.btnClearLog.UseVisualStyleBackColor = true;
this.btnClearLog.Click += new System.EventHandler(this.btnClearLog_Click);
//
// btnSaveLog
//
this.btnSaveLog.Location = new System.Drawing.Point(110, 238);
this.btnSaveLog.Name = "btnSaveLog";
this.btnSaveLog.Size = new System.Drawing.Size(75, 23);
this.btnSaveLog.TabIndex = 2;
this.btnSaveLog.Text = "保存日志";
this.btnSaveLog.UseVisualStyleBackColor = true;
this.btnSaveLog.Click += new System.EventHandler(this.btnSaveLog_Click);
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabel1});
this.statusStrip1.Location = new System.Drawing.Point(0, 428);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(784, 22);
this.statusStrip1.TabIndex = 4;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabel1
//
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel1.Size = new System.Drawing.Size(32, 17);
this.toolStripStatusLabel1.Text = "就绪";
this.labelLog.AutoSize = true;
this.labelLog.Location = new System.Drawing.Point(12, 177);
this.labelLog.Name = "labelLog";
this.labelLog.Size = new System.Drawing.Size(69, 20);
this.labelLog.TabIndex = 5;
this.labelLog.Text = "数据日志";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(784, 450);
this.Controls.Add(this.statusStrip1);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "串口数据采集与上传系统";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.groupBox4.ResumeLayout(false);
this.groupBox4.PerformLayout();
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.ClientSize = new System.Drawing.Size(820, 470);
this.Controls.Add(this.labelLog);
this.Controls.Add(this.textBoxLog);
this.Controls.Add(this.buttonStartStop);
this.Controls.Add(this.groupBoxMQTT);
this.Controls.Add(this.groupBoxSpectrometer);
this.Controls.Add(this.groupBoxGPS);
this.Text = "串口数据采集与MQTT发送";
this.groupBoxGPS.ResumeLayout(false);
this.groupBoxGPS.PerformLayout();
this.groupBoxSpectrometer.ResumeLayout(false);
this.groupBoxSpectrometer.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownRefreshInterval)).EndInit();
this.groupBoxMQTT.ResumeLayout(false);
this.groupBoxMQTT.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.ComboBox cmbPort1;
private System.Windows.Forms.ComboBox cmbBaud1;
private System.Windows.Forms.Button btnConnect1;
private System.Windows.Forms.Label lblPort1;
private System.Windows.Forms.Label lblBaud1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.ComboBox cmbPort2;
private System.Windows.Forms.ComboBox cmbBaud2;
private System.Windows.Forms.Button btnConnect2;
private System.Windows.Forms.Label lblPort2;
private System.Windows.Forms.Label lblBaud2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.TextBox txtUrl;
private System.Windows.Forms.Label lblUrl;
private System.Windows.Forms.Button btnStartUpload;
private System.Windows.Forms.Button btnStopUpload;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.TextBox txtLog;
private System.Windows.Forms.Button btnClearLog;
private System.Windows.Forms.Button btnSaveLog;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
}

1053
Form1.cs

File diff suppressed because it is too large Load Diff

56
MqttSign.cs Normal file
View File

@ -0,0 +1,56 @@
using System;
using System.Security.Cryptography;
namespace aiot_paho_csharp
{
class CryptoUtil
{
public static String hmacSha256(String plainText, String key)
{
var encoding = new System.Text.UTF8Encoding();
byte[] plainTextBytes = encoding.GetBytes(plainText);
byte[] keyBytes = encoding.GetBytes(key);
HMACSHA256 hmac = new HMACSHA256(keyBytes);
byte[] sign = hmac.ComputeHash(plainTextBytes);
return BitConverter.ToString(sign).Replace("-", string.Empty);
}
}
public class MqttSign
{
private String username = "";
private String password = "";
private String clientid = "";
public String getUsername() { return this.username; }
public String getPassword() { return this.password; }
public String getClientid() { return this.clientid; }
public bool calculate(String productKey, String deviceName, String deviceSecret)
{
if (productKey == null || deviceName == null || deviceSecret == null)
{
return false;
}
//MQTT用户名
this.username = deviceName + "&" + productKey;
//MQTT密码
String timestamp = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds).ToString();
String plainPasswd = "clientId" + productKey + "." + deviceName + "deviceName" +
deviceName + "productKey" + productKey + "timestamp" + timestamp;
this.password = CryptoUtil.hmacSha256(plainPasswd, deviceSecret);
//MQTT ClientId
this.clientid = productKey + "." + deviceName + "|" + "timestamp=" + timestamp +
",_v=paho-c#-1.0.0,securemode=2,signmethod=hmacsha256|";
return true;
}
}
}

94
readme.txt Normal file
View File

@ -0,0 +1,94 @@
我希望用C#写一个GUI应用能够打开2个串口然后读取数据然后能够整理数据把数据发送到配置好的网址上并且能够在界面上看到数据记录然后也能把数据写到一个log文件中。
一个串口是GPS的数据格式是这样的。要提取WGS84后面的经纬度信息。这个串口打开发送bestposa com1 1 \r\n后就有数据传输进来
#BESTPOSA,COM1,14394,98.0,UNKNOWN,1,1699.000,1700908,2,18;INSUFFICIENT_OBS,NONE,0.00000000000,0.00000000000,0.0000,0.0000,WGS84,0.0000,0.0000,0.0000,"",0.000,0.000,0,0,0,0,0,00,00,00*fcf220b4
另一个串口是谱仪数据,文档如下:
航空γ谱仪通信协议
通信方式
采用RS232串口通信115200n81。
操作指令
开始测量指令
指令格式:$start[回车][换行]
反馈格式ok[回车][换行]
谱数据刷新指令(刷新的回复要忽略)
指令格式:$refresh[回车][换行]
反馈格式0 0 0 0 0 0 0 0 0 0 0 0 0 (很长的一个序列)[回车][换行]
清空测量指令
指令格式:$clear[回车][换行]
反馈格式ok[回车][换行]
谱分析指令
指令格式:$getSperesult[回车][换行]
反馈格式xxxx.xx.xx xx:xx:xx aaaaaaa.aa bbbbbbb.bb 1111111.11 2222222.12 3333333.33 4444444.44 5555555.55 6666666.66 7777777.77 8888888.88 9999999.99 0000000.00 JJJJJJ.JJ cccccccccc.cc [回车][换行]
“aaaaaaa.aa bbbbbbb.bb”表示实时间和死时间单位s
“1111111.11 … …0000000.00”表示十种核素计数率单位cps
“JJJJJJ.JJ”表示环境总辐射剂量率值单位nSv/h
“cccccccccc.cc”表示γ谱的全谱计数率cps
“xxxx.xx.xx xx:xx:xx”表示返回该帧时间
1111111.11 (核素40K单位cps)
2222222.22 (核素208Tl单位cps)
3333333.33 (核素214Bi单位cps)
4444444.44 (核素214Pb单位cps)
5555555.55 (核素226Ra单位cps)
6666666.66 (核素232Th单位cps)
7777777.77 (核素60Co单位cps)
8888888.88 (核素131I单位cps)
9999999.99 (核素137Cs单位cps)
0000000.00 (核素192Ir单位cps)
这个需要打开串口的时候先开始,然后就进入一个循环,刷新一次,解析一次,得到数据,这样反复。我希望能选择刷新的频率。
两个串口的数据分别放到两个队列里每次要从谱仪队列里读取数据然后从GPS队列里读取时间最近的数据两个合并起来组成这样的格式{"OptTime":"2020-11-30 13:16:24.178","PointLat":"23.10606","PointLng":"113.213425","Value":1.9334545697802241E-07}。
然后通过mqtt发送出去。
这是大致的发布代码:
String productKey = "gfcq950RDqt";
String topicRoot = "a10inDdCRS6";
String deviceName = "PubDevice";
String deviceSecret = "1031a49a4f61c29a086f79b41ed971c7";
//计算Mqtt建联参数
MqttSign sign = new MqttSign();
sign.calculate(productKey, deviceName, deviceSecret);
Console.WriteLine("username: " + sign.getUsername());
Console.WriteLine("password: " + sign.getPassword());
Console.WriteLine("clientid: " + sign.getClientid());
//使用Paho链接阿里云物联网平台
int port = 443;
String broker = productKey + ".iot-as-mqtt.cn-shanghai.aliyuncs.com";
MqttClient mqttClient = new MqttClient(broker, port, true, MqttSslProtocols.TLSv1_2, null, null);
mqttClient.Connect(sign.getClientid(), sign.getUsername(), sign.getPassword());
Console.WriteLine("broker: " + broker + " Connected");
// //Paho Mqtt 消息订阅
// String topicReply = "/" + topicRoot + "/" + deviceName + "/user/get";
// mqttClient.MqttMsgPublishReceived += MqttPostProperty_MqttMsgPublishReceived;
// mqttClient.Subscribe(new string[] { topicReply }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });
// Console.WriteLine("subscribe: " + topicReply);
//Paho Mqtt 消息发布
String topic = "/" + topicRoot + "/" + deviceName + "/user/update";
String message = "{\"id\":\"1\",\"version\":\"1.0\",\"params\":{\"LightSwitch\":0}}";
mqttClient.Publish(topic, Encoding.UTF8.GetBytes(message));
Console.WriteLine("publish: " + message);
while(true) {
Thread.Sleep(2000);
}
//Paho Mqtt 断开连接
mqttClient.Disconnect();
另外我希望是2个串口接收数据分别分配一个线程上传分配一个线程。数据读取后放到一个队列里上传线程去读取队列数据。
还要有一个日志线程,把数据写入到日志里。
软件要有个简单的文本框来展示串口收到的数据
日志要自动创建文件,每次打开的时候,日志的名字应该是当前的时间。
要有个按钮可以一键开始和结束。
另外,接收数据的时候很可能每次收到的是不完整的数据。
我已经创建好了模板程序。

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
@ -10,7 +10,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="M2Mqtt" Version="4.3.0" />
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

View File

@ -1,24 +0,0 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "sscom-sender", "sscom-sender.csproj", "{F03C6C53-8989-99E6-D747-EC03449AD20F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F03C6C53-8989-99E6-D747-EC03449AD20F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F03C6C53-8989-99E6-D747-EC03449AD20F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F03C6C53-8989-99E6-D747-EC03449AD20F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F03C6C53-8989-99E6-D747-EC03449AD20F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {707867E8-3EF5-4E83-A851-9053BE677857}
EndGlobalSection
EndGlobal