dcsimg
Results 1 to 12 of 12

Thread: How to generate text file from AutoID Network Navigator-Reader SR-710

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    How to generate text file from AutoID Network Navigator-Reader SR-710

    Hi,

    I am making a program in C# windows application where I used a device called SR-710 to auto scan the serial number of a panel and auto-generate a text file.

    Now I used a textbox to received the data from SR-710.

    My problem is after my device scan the serial number and shown in the textbox, it is not calling my text change event and run my code to generate a text file but when I type serial number manually it is fine.

    I want if scan 1 serial number then generate a text file.

    here is my code to communicate with my device.

    Code:
       public delegate void AddDataDelegate(String myString);
            public AddDataDelegate myDelegate;
            SerialPort mySerialPort = new SerialPort();
    
            private void SPI_MONITOR_FAIL_Load(object sender, EventArgs e)
            {
                try
                {
                    var ports = SerialPort.GetPortNames();
                    //cmbSerialPorts.DataSource = ports;
                    //mySerialPort.PortName = cmbSerialPorts.Text;
    
                    mySerialPort.PortName = "COM1";
                    mySerialPort.BaudRate = 115200;
                    mySerialPort.Parity = Parity.Even;
                    mySerialPort.StopBits = StopBits.One;
                    mySerialPort.DataBits = 8;
                    mySerialPort.Handshake = Handshake.None;
                    mySerialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);
                    mySerialPort.Open();
                   // MessageBox.Show("open successfully v1");
                    this.myDelegate = new AddDataDelegate(AddDataMethod);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
              
            }
    
                public void AddDataMethod(String myString)
            {
                serialNum_txt.AppendText(myString);  
            }
    
            private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
                {
                    SerialPort sp = (SerialPort)sender;
                    string s = sp.ReadExisting();
                    serialNum_txt.Invoke(this.myDelegate, new Object[] { s });
            }
    Here is my code in my text change event in my text box to generate a text file.
    I set the length of the serial number to call my generate code.

    Code:
     
     private void serialNum_txt_TextChanged(object sender, EventArgs e)
            {
    
                try
                {
    
    
                    int sn_digit;
                    sn_digit = Convert.ToInt32(textBox2.Text);
    
                    if (serialNum_txt.Text.Length == sn_digit)
                    {
    
                        string p1 = @"C:\LOGFILE";
                        string p2 = serialNum_txt.Text + "_RF";
                        string Combine = Path.Combine(p1, p2);
                        using (System.IO.StreamWriter sw = new System.IO.StreamWriter(Combine + ".txt"))
                        {
                            sw.Write("|" + serialNum_txt.Text);
                            sw.Write("|" + DateTime.Now.ToString("yyyyMMddhhmmss"));
                            sw.Write("|" + line_cmb.Text);
                            sw.Write("|" + txtResult.Text);
                            sw.Write("|" + station_cmb.Text);
                            sw.Write("|" + model_cmb.Text);
                            sw.Write("|");
                        }
                        serialNum_txt.Clear();
                    }
                  
                }
    
                catch (Exception EX)
                {
                    MessageBox.Show(EX.Message);
                }
    
            }
    Thank you in advance.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,733

    Re: How to generate text file from AutoID Network Navigator-Reader SR-710

    In C#, the first thing to check is always that you have actually registered that method as a handler for the event, because you can't see it in code like you can in VB.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How to generate text file from AutoID Network Navigator-Reader SR-710

    Please tell me where must I call my event? because text change event is not calling when I received serial number from my device.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,733

    Re: How to generate text file from AutoID Network Navigator-Reader SR-710

    You don't call an event. You ought to spend a bit of time to learn how events work, because their fairly critical in an event-driven environment like WinForms. Events are raised and handled. In order to handle an event, you need to write an appropriate method and then register it as a handler for the desired event. That method will then be invoked when the event is raised. In your case, the TextBox raises the event and invokes the registered method, which is a member of the form.

    You can do it all manually if you want but, more often than not, you get the IDE to generate an empty method and do the registration for you. If you copy a method off the web and paste it into your code then there's no registration, so the method won't be invoked when the event is raised. Generally speaking, rather than copying an entire method, it's better to generate the event handler in the IDE as you usually do, then copy and paste the body of the method only.

    I'm just guessing that that is what happened, based on the behaviour you describe. You should be able to confirm or deny that that is the case. How did you create that method? Have you used the debugger to test it, i.e. place a breakpoint on the method and then simply type into the TextBox to see whether your method is executed? You don;t have to sit on your hands and wait for a turnkey solution to be handled to you. You can do a lot for yourself, including providing us with more relevant information about what you did and did not do as well as doing some reading about how events work to learn for yourself. That way, you'll be more equipped to solve similar problems in the future.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,642

    Re: How to generate text file from AutoID Network Navigator-Reader SR-710

    Why are you relying on the textChanged event anyways? You've got the data? As soon as you put it into the text box, just write it out to the file? Or just put it in the file, skip the textbox all together. Unless you want to see the data.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How to generate text file from AutoID Network Navigator-Reader SR-710

    I understand how the event works, I already tried typing in my textbox and if the length is = 10 it automatic run my code to generate a text file in drive c, the problem is if I connect it to my camera/scanner and get input to it the text change event doesn't execute.
    I was expecting to be called also just like when I type manually.
    Please help thanks.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How to generate text file from AutoID Network Navigator-Reader SR-710

    I understand how the event works, I already tried typing in my textbox and if the length is = 10 it automatic run my code to generate a text file in drive c, the problem is if I connect it to my camera/scanner and get input to it the text change event doesn't execute.
    I was expecting to be called also just like when I type manually.
    Please help thanks.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How to generate text file from AutoID Network Navigator-Reader SR-710

    because firstly my scanner will throw a serial number in my textbox then I need to generate a log file for that serial number, my idea is if the serial length number is == 10 then generate a log file, then wait for the next serial number and do the same.

    how can I write my log file as soon as scanner input data to the textbox?
    please help thanks.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,733

    Re: How to generate text file from AutoID Network Navigator-Reader SR-710

    Quote Originally Posted by BONITO View Post
    I understand how the event works, I already tried typing in my textbox and if the length is = 10 it automatic run my code to generate a text file in drive c
    And how are we to know that if you don't tell us? If you tell us that a method is not executed when you expect an event to be raised, the obvious reason is that you haven't registered it as an event handler. Why would we ignore that obvious cause and go looking for something more exotic when that would be a big waste of time if the obvious cause was the actual cause? We only know what you tell us so you should consider telling us ALL the relevant information. That way we can all avoid wasting our time covering things that you already know or have already tested.

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,733

    Re: How to generate text file from AutoID Network Navigator-Reader SR-710

    Have you actually debugged the code? Debugging is not just seeing if the effect is as expected but actually examining the code as it runs. Have you set a breakpoint on the event handler to see whether it is actually executed? Maybe the Text is just never the Length that you expect, so it never enters the if block. I was working on the assumption that you actually had used the debugger and the issue was that the event handler wasn't being executed but my previous post reminded me that I shouldn't assume that someone has behaved in a way that seems obvious to me. I should know by now that the majority of people who post on this site either don't know how to debug or haven't bothered to do it anyway.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How to generate text file from AutoID Network Navigator-Reader SR-710

    Quote Originally Posted by jmcilhinney View Post
    Have you actually debugged the code? Debugging is not just seeing if the effect is as expected but actually examining the code as it runs. Have you set a breakpoint on the event handler to see whether it is actually executed? Maybe the Text is just never the Length that you expect, so it never enters the if block. I was working on the assumption that you actually had used the debugger and the issue was that the event handler wasn't being executed but my previous post reminded me that I shouldn't assume that someone has behaved in a way that seems obvious to me. I should know by now that the majority of people who post on this site either don't know how to debug or haven't bothered to do it anyway.
    Yes, I used a breakpoint to find where my code have a problem.

    I placed my [I]if[/]block statement inside my method as below code:
    so after scan barcode to my textbox if the textbox is not empty it will call my if statement to generate log file but unfortunately, it throws an error " Illegal Character in the path"


    Code:
     public void AddDataMethod(String myString)
            {
              
               try
               {
                   serialNum_txt.AppendText(myString);
    
                   if (serialNum_txt.Text !="")
                   {
                       string p1 = @"C:\LOGFILE";
                       string p2 = myString + "_RF";
                       string Combine = Path.Combine(p1, p2);
                       using (System.IO.StreamWriter sw = new System.IO.StreamWriter(Combine + ".Txt"))
                       {
                           sw.Write("|" + serialNum_txt.Text);
                           sw.Write("|" + DateTime.Now.ToString("yyyyMMddhhmmss"));
                           sw.Write("|" + line_cmb.Text);
                           sw.Write("|" + txtResult.Text);
                           sw.Write("|" + station_cmb.Text);
                           sw.Write("|" + model_cmb.Text);
                           sw.Write("|");
                       }
                       serialNum_txt.Clear();
    
                   }
                   else
                   {
                       MessageBox.Show("Invalid sn");
                      serialNum_txt.Clear();
                   }
    
               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }
            }

    still, my load form is the same:

    Code:
     public delegate void AddDataDelegate(String myString);
           public AddDataDelegate myDelegate;
           
    
          //  serialPort1 mySerialPort = new serialPort1();
           SerialPort mySerialPort = new SerialPort();
    
            private void SPI_MONITOR_FAIL_Load(object sender, EventArgs e)
            {
                try
                {
    
                   mySerialPort.PortName = "COM6";
                    mySerialPort.BaudRate = 115200;
                    mySerialPort.Parity = Parity.Even;
                    mySerialPort.StopBits = StopBits.One;
                    mySerialPort.DataBits = 8;
                    mySerialPort.Handshake = Handshake.None;
                    mySerialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);            
                    mySerialPort.Open();
    
                    this.myDelegate = new AddDataDelegate(AddDataMethod);
              
              
    
               
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
              
            }

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,733

    Re: How to generate text file from AutoID Network Navigator-Reader SR-710

    Quote Originally Posted by BONITO View Post
    it throws an error " Illegal Character in the path"
    So the obvious next step is to look at the path that contains illegal characters? Not the code that constructs the path but the actual path. Have you done that, because you certainly haven't shown us that path?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width