Question

Okay so I am stuck for almost 20 days now in the same problem of a serial communication device. I have a hardware sensor which read tags and returns the tag code number on every read through serial com port 1 or 3.Any of these I use doesn't matter. I am using a program I wrote in c# to play with the incoming data. Now problem is that if forexample: my sensor reads tag with code "e2 0 10 1 83 10 1 23 7 0 d0 c0 1 be" It will not read this tag again unless I switch of the sensor and turn it on again (Power reset) . So I can't figure out how to make my sensor forget all the data it read till I closed the port. ANY ONE CAN HELP PLEASE I AM DESPERATE NOW Some one told me that we need to write to device with some commands but he didn't know more than that. Here is the current code:

 void IntializeSensor()
    {
        try
        {
            if (mySerialPort==null)
            {
                mySerialPort = new SerialPort("COM3");

                mySerialPort.BaudRate = 9600;
                mySerialPort.Parity = Parity.None;

                mySerialPort.StopBits = StopBits.One;
                mySerialPort.DataBits = 8;
                mySerialPort.ReadTimeout = 2000;
                mySerialPort.Handshake = Handshake.None;
                LoglistBox.Items.Add("--Port Intilalized at COM1--");
                mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            }

        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }
    }
  void OpenPort()
    {
        try
        {
            str = "";

            if (mySerialPort.IsOpen)
            {
                ClosePort();
                Thread.Sleep(6000);

            }
            mySerialPort.Open();

            LoglistBox.Items.Add("--Port Opened at COM1 Success--");
        }
        catch (Exception ex)
        {


            LoglistBox.Items.Add("--Port Opened Failed--Error: "+ex.Message);
        }
    }

void ClosePort() {

        try
        {
            mySerialPort.Write("ABCABCABCABCABC");
            mySerialPort.DiscardInBuffer();
            mySerialPort.DiscardOutBuffer();
            mySerialPort.Dispose();
            mySerialPort.Close();

            LoglistBox.Items.Add("--Port Closed at COM1 Success--");
        }
        catch (Exception ex)
        {

            LoglistBox.Items.Add("--Port Closed Failed--Error: " + ex.Message);
            MessageBox.Show(ex.Message);
        }

    }

private void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e) {

        try
        {

            if (e.EventType != SerialData.Chars) return; 
            SerialPort COMPort = (SerialPort)sender;

            int bytes = COMPort.BytesToRead;

            //create a byte array to hold the awaiting data

            byte[] comBuffer = new byte[bytes];

            //read the data and store it

            COMPort.Read(comBuffer, 0, bytes);



            // System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

            str = ByteArrayToHexString(comBuffer);

            holdfirstvalue += str;
            //str = str +" "+ str;
            //MessageBox.Show("after concat "+str);


            if (str.Contains("FF") || str.Contains("F F") || str.Contains("F"))
            {


             SetText(holdfirstvalue.ToString());// ONE TAG CODE SENT TO BE SET IN LIST
                str = "";
                holdfirstvalue = "";
            }

        }

        catch (Exception ex)
        {
           // LoglistBox.Items.Add("--Port Opened Failed--Error: " + ex.InnerException);
            MessageBox.Show(ex.Message+" "+ex.InnerException);
        } 

    }
Was it helpful?

Solution 2

Ok so I finally found the code in hex which actually resets the sensor. So to help anyone out there who bought Middle Range UHF RFID Reader or any type of this model.

What we did is we hacked into the wires of serial port by soldering copper wire on data pins. Then we attached the other end of those copper wires to my laptop and used the terminal reader "Putty" to see what is actually being sent by the PC to the Reader on RESET HEAD button click.

This way we found the hex code , converted it to byte array and here is the C# code to write it to reader device by serial port and resets the device:

{ 
mySerialPort.Open();
        byte[] bytesToSend = StringToByteArray("A00265F9");// correct command to reset READER
        mySerialPort.Write(bytesToSend, 0, 4);
}
public static byte[] StringToByteArray(string hex)
        {
            return Enumerable.Range(0, hex.Length)
                             .Where(x => x % 2 == 0)
                             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                             .ToArray();
        }

OTHER TIPS

As I understood from your comments, even the program that is shipped with it does not read the same bar-code twice in a row. Am I right?

To me it seems that "the tag reader manufacturer" may have put that mechanism intentionally to prevent user mistakenly scan an item twice at check-out. because it happens a lot that a same stays on the scanner or be crossed against the scanner couple of times when moving things around.

Unless you have access to the scanner Firmware and are able to make changes yourself, I'd say contact the manufacturer. I would contact the manufacturer and ask about this directly. There should be a command that tells the scanner to "Get out of lock mode and restart scanning again" for the special case of scanning a same item several times (e.g. buying multiple similar things.)

They should provide you with a manual with the list of all the commands you can send to your device and you use this commands to build up your system.

One more thing to try! can you scope out your serial port using "Real Term" or any other terminal monitoring application to see if the scanner sends the code to the PC after you scan the same item again or not? This helps you to isolate the problem to make sure if it is the Scanner Firmware or the desktop software. (it seems to me that it is the scanner Firmware ignoring the item because you say it works fine when you reset it)

edit: also I see you are reading your serial port based on DataREadyEvent, again, if it was me, I would add another thread with a very short delay say 20ms or 50ms to keep reading the serial port constantly. there are plenty of examples on how to implement this on the net, one simple and quick way of this is described here:

Read com ports using threading in c#

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top