Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    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>
Posted
Updated 30-Mar-22 23:19pm

1 solution

You need to subscribe to the DataReceived event[^] to trigger the update:
C#
private readonly SerialPort sp1;

public MainWindow()
{
    InitializeComponent();
    sp1 = new SerialPort();
    sp1.DataReceived += DataReceived;
}

private void DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    var sp = (SerialPort)sender;
    string data = sp.ReadExisting();
    Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action<string>)Update, data);
}

private void Update(string data)
{
    txtReceive.Text += data;
}
 
Share this answer
 
Comments
TheRealSteveJudge 31-Mar-22 5:30am    
That's right! 5*
Benoy Sharry 31-Mar-22 5:59am    
Thank you Richard for the help and others too. it works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900