NMET
NMET

Reputation: 1

C# I have a code that reads from a USB code scanner (COM4) that only works with a form i need it to run in background with just a NotifyIcon

i found the code/library and all i needed on BasselItech https://www.youtube.com/watch?v=zzs2xnyCczo

i tried modifying the code with using .Hide() and visible set to false but there is always the Form that appears and i would just like to have it run in the background with a notifyicon

Thank you all

here is my actual code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using USB_Barcode_Scanner;
using System.Threading;
using System.Timers;
using System.Windows.Threading;
using System.Diagnostics;

namespace USB_Barcode_Scanner_Tutorial___C_Sharp
{
    public partial class Form1 : Form
    {

        private static SerialPort currentPort;
        private delegate void updateDelegate(string txt);

        public Form1()
        {
            InitializeComponent();
            //BarcodeScanner barcodeScanner = new BarcodeScanner(textBox1);
            //barcodeScanner.BarcodeScanned += BarcodeScanner_BarcodeScanned;

            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            
            string com_PortName = "COM4";
            int com_BaudRate = 9600;

            currentPort = new SerialPort(com_PortName, com_BaudRate, Parity.None, 8, StopBits.One);
            currentPort.Handshake = Handshake.None;
            currentPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            currentPort.ReadTimeout = 1000;
            currentPort.WriteTimeout = 500;

           
            currentPort.Open();

        }


        private void BarcodeScanner_BarcodeScanned(object sender, BarcodeScannerEventArgs e)
        {
            textBox1.Text = e.Barcode;
        }

        private void CodeFunktionenFuerQRCode_ausfuehren(string inhalt)
        {
            if (inhalt.StartsWith("http")) /
            {
                Process.Start("explorer.exe", inhalt);

            }
            else if (inhalt.StartsWith("helios")) erkannt
            {
                Process.Start("explorer.exe", inhalt);
            }
            else 
            {

            }

            #region Inhalt des Links in die TEXTBOX-schreiben
            if (textBox2.Text.Length > 0)
            {
                textBox2.Text = "";
            }

            textBox2.Text = inhalt;
            #endregion
        }


        private void port_DataReceived(object sender,SerialDataReceivedEventArgs e)
        {
            string strFromPort = "";

            try
            {
                strFromPort = currentPort.ReadExisting();
            }
            catch { }


            if (!currentPort.IsOpen)
            {
                currentPort.Open(); 
                System.Threading.Thread.Sleep(100); 
                currentPort.DiscardInBuffer();      
            }
            else
            {
                currentPort.DiscardInBuffer();     
            }

            BeginInvoke(new updateDelegate(CodeFunktionenFuerQRCode_ausfuehren), strFromPort);

        }


    }


}

Upvotes: 0

Views: 31

Answers (0)

Related Questions