0
votes

I am reading data from a police scanner using C# and serial ports (USB to Serial). When I view the data in a different program there's no problem. When I use the following code I get repeat (duplicate) lines for everything. What am I doing wrong?

SerialPort _sp = new SerialPort();
_sp.BaudRate = 115000;
_sp.DataBits = 8;
_sp.Parity = Parity.None;
_sp.StopBits = StopBits.One;

and the function to start serial port reading...

public void StartSerial()
{
    _sp.PortName = comboPorts.SelectedItem.ToString();
    _sp.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
    _sp.Open();
}

and the read function...

private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    try
    {
        String strLine = _sp.ReadLine();
        // Handle Data
    }
    catch { }
}

The manual states the following is required:

To view CCDump data, connect the radio to a personal computer using the USB PC/IF cable. Start a terminal program or your third party application software. Configure the communications parameters for “115200, N, 8, 1” as follows:

COM Port: The COM port number assigned to the USB cable by your computer. If you are running a Windows operating system, you can find this information under “Ports” in the Device Manager.

Parity: None

Baud Rate: 115,000 bits per second

Word: 8 bits

Stop Bits: 1

4
can you show complete (cutdown, compilable) code that exhibits this behaviour? - Mitch Wheat

4 Answers

1
votes

It seems to me that the serialPort_DataReceived event had been fired for multiple times even there is only one line of data,

Check the settings , there should be a threshold, which means if the data length is less then the threshold , then the event won't be fired.

In the mean time,make sure you only register the event once...

1
votes

You must read whatever data is available at once. Do not use ReadLine since it's not clear which character the device uses to send LF or CR or if it even has them sent to you. Instead use this:

            int totalBytes = _sp.BytesToRead;
            byte[] buffer = new byte[totalBytes];
            _sp.Read(buffer, 0, totalBytes);

or

  string line = _sp.ReadExisting();
1
votes

Ok, I found the answer. The data is being sent as duplicates so there's nothing I can do.

1
votes

I was writing a piece of application which logged Serial data through USB and ran into similar issues where I was getting incomplete (partial) data, duplicated data, and worst, combination of both. The problem was latter solved.

The reason is that DataReceive does not trigger consistently, and can potentially trigger anyway it prefers if no restriction is configured. What I did was setting an EOP Character so application will buffer until it read the EOP and logged the complete data string only once and await for the next EOP Char to log the next data. See this SO post for related discussion.

In my case, I attached {CR+LF} (i.e. a carrier return and a new line) to each end of my data string as my EOP Char. Each data string, e.g. "Hello world", sent will become "Hello World{CR}{LF}". In my code where I set my port, baud rate and other properties I also set a NewLine property to match the EOP Char I set for my device:

myPort.NewLine = "\r\n";