Browse Source

串口发送数据后增加延时

mengshunguo 1 month ago
parent
commit
ce02fecc26

+ 2 - 2
SCADA_DAQ/Customer/UctFrmMain.xaml

@@ -12,7 +12,7 @@
     <Grid>
             <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="2*"/>
-                <ColumnDefinition Width="1*"/>
+                <ColumnDefinition Width="1.5*"/>
             </Grid.ColumnDefinitions>
         <DataGrid Name="DataGridDt" ItemsSource="{Binding}" AutoGenerateColumns="True" IsReadOnly="True" />
         <Grid Grid.Column="1">
@@ -38,7 +38,7 @@
             <GroupBox Grid.Row="2" Header="ASCII码信息">
                 <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                     <TextBlock x:Name="ASCIICode" Text="ASCII码" HorizontalAlignment="Center" VerticalAlignment="Center" 
-                     FontSize="40" Foreground="White" FontStretch="Normal" FontWeight="Black" Background="Black"/>
+                     FontSize="25" Foreground="White" FontStretch="Normal" FontWeight="Black" Background="Black"/>
                 </StackPanel>
             </GroupBox>
         </Grid>

+ 6 - 4
SCADA_DAQ/Customer/UctFrmMain.xaml.cs

@@ -89,7 +89,7 @@ namespace SCADA_DAQ.Customer
             string barCode = null;
             if (e.Barcode.Substring(0, 6) == "QRCode")
             {
-                barCode = e.Barcode.Substring(6, e.Barcode.Length - 6).Trim(); 
+                barCode = e.Barcode.Substring(6, e.Barcode.Length - 6).Trim();
                 Log.Info($"收到条码{barCode}");
             }
             else
@@ -100,11 +100,11 @@ namespace SCADA_DAQ.Customer
             if (barCode == null || barCode == "" || barCode == "NoRead" || barCode == this.Barcode
                 || BarcodeInfo.Count == 0 || !BarcodeInfo.ContainsKey(barCode))
             {
-                if(barCode == this.Barcode)
+                if (barCode == this.Barcode)
                 {
                     Log.Info($"重复读码{barCode}");
                 }
-                else if(!BarcodeInfo.ContainsKey(barCode))
+                else if (!BarcodeInfo.ContainsKey(barCode))
                 {
                     Log.Info($"Ecexl表中无此条码{barCode}信息");
                 }
@@ -136,7 +136,7 @@ namespace SCADA_DAQ.Customer
                 StopBits = CustomerEnv.CustomerConfig.SerialModel.StopBits,
                 Parity = CustomerEnv.CustomerConfig.SerialModel.Parity,
                 ReadTimeout = CustomerEnv.CustomerConfig.SerialModel.Timeout,
-                ReadBufferSize = 102400
+                //ReadBufferSize = 102400
             })
             {
                 try
@@ -144,6 +144,8 @@ namespace SCADA_DAQ.Customer
                     serialcomm.Open();
                     serialcomm.Write(_byte, 0, _byte.Count());
                     this.Barcode = barCode;
+                    System.Threading.Thread.Sleep(1000);
+                    serialcomm.Close();
                 }
                 catch
                 {

+ 26 - 1
SCADA_DAQ/Customer/UctFrmParameterSet.xaml.cs

@@ -2,6 +2,7 @@
 using SCADA_DAQ.Customer.Models;
 using System;
 using System.Collections.Generic;
+using System.IO.Ports;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -30,7 +31,8 @@ namespace SCADA_DAQ.Customer
         {
             InitializeComponent();
 
-            ToolBar.Visibility = Visibility.Collapsed;
+            //ToolBar.Visibility = Visibility.Collapsed;
+            ToolBar.Items.Clear();
 
             var view = new AutoView(
             CustomerEnv.CustomerConfig.SerialModel);
@@ -38,6 +40,11 @@ namespace SCADA_DAQ.Customer
                           (from t in new int[] { 5, 6, 7, 8 } select new KeyValuePair<int, int>(t, t)).ToList(),
                           "Key", "Value");
             Gp_Config.Content = view;
+
+
+            var comTest = new ToolBarButton() { ButtonKind = SCADA.CommonCtrl.ButtonKind.StartButton, Title = "串口测试" };
+            ToolBar.Items.Add(comTest);
+            comTest.Click += ComTest_Click;
             //Gp_Config.Content = new AutoView(
             // CustomerEnv.CustomerConfig,
             // nameof(CustomerConfig.BatchSize),
@@ -57,5 +64,23 @@ namespace SCADA_DAQ.Customer
             //    ContentBackground = Brushes.LightBlue,
             //};
         }
+
+        private void ComTest_Click(object sender, RoutedEventArgs e)
+        {
+            using (var comm = new SerialPort()
+            {
+                PortName = CustomerEnv.CustomerConfig.SerialModel.PortName,
+                BaudRate = CustomerEnv.CustomerConfig.SerialModel.BaudRate,
+                Parity = CustomerEnv.CustomerConfig.SerialModel.Parity,
+                DataBits = CustomerEnv.CustomerConfig.SerialModel.DataBits,
+                StopBits = CustomerEnv.CustomerConfig.SerialModel.StopBits
+            })
+            {
+                comm.Open();
+                comm.Write(new byte[] { 0x31, 0x31, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 }, 0, 10);
+                System.Threading.Thread.Sleep(10000);
+                comm.Close();
+            }
+        }
     }
 }