I have a wired usb QR Scanner. Its very small. Task: I am trying to scan the qr code ( example it scans the serial no = 1025678 ). And different qr code with different qr numbers. So it scans the qr number and then displays the output in the textbox. I am using C#-WPF to code the software.
I would like to know how i can display the output continously by clicking the button once?.
What I have tried:
I have tried to open the port and its successful and then i have tried to display the output in the text box, but I have to click the serial code button everytime to display the qr number and I am finding out a way to display the output by clicking the button once, but i am not able to. I would like to know if someone could help me.
below is the code of what i have tried.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using System.Threading;
using System.Windows.Threading;
namespace WpfApp3
{
public partial class MainWindow : Window
{
SerialPort sp1 = new SerialPort();
public MainWindow()
{
InitializeComponent();
}
private void ComboBox_SelectChanged(object sender, SelectionChangedEventArgs e)
{
var selectedcomboitem = sender as ComboBox;
string name = selectedcomboitem.SelectedItem as string;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
string portName = cboPort1.SelectedItem as string;
sp1.PortName = portName;
sp1.BaudRate = 9600;
sp1.Open();
statussq.Text = "Connected";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "invalid port!", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
private void btnData_Click_1(object sender, RoutedEventArgs e)
{
Update();
}
private void Update()
{
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
(ThreadStart)delegate ()
{
try
{
if (sp1.IsOpen)
{
txtReceive.Text += sp1.ReadExisting();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
);
}
}
}
#xaml file
<Window x:Class="WpfApp3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ports="clr-namespace:System.IO.Ports;assembly=System"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ObjectDataProvider ObjectType="{x:Type ports:SerialPort}" MethodName="GetPortNames" x:Key="portNames"/>
</Window.Resources>
<Grid>
<ComboBox x:Name="cboPort1" ItemsSource="{Binding Source={StaticResource portNames}}" SelectionChanged="ComboBox_SelectChanged" HorizontalAlignment="Left" Margin="94,165,0,0" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.394,1.71"/>
<TextBlock x:Name ="statussq" HorizontalAlignment="Left" Margin="94,0,0,0" TextWrapping="Wrap" VerticalAlignment="Center" Height="25" Width="120"><Run Language="de-de" Text="Status:"/></TextBlock>
<TextBox x:Name="txtReceive" HorizontalAlignment="Left" Margin="343,217,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="198" Height="154"/>
<Button x:Name ="btnScan" Content="Scan" HorizontalAlignment="Left" Margin="343,167,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Button x:Name="btnData" Content="Serial Code" HorizontalAlignment="Left" Margin="446,170,0,0" VerticalAlignment="Top" Click="btnData_Click_1"/>
</Grid>
</Window>