C # SerialPort's DataReceived event could not be triggered, using Honeywell's scanning gun and the corresponding USB to serial port driver.

Source: Internet
Author: User

Yesterday I wanted to try Honeywell's sweep gun, the scanner has two modes, keyboard mode and serial port mode,

1, keyboard mode directly inserted on the line, just like a keyboard does not need any driver, swept out of the data directly to the input focus of the PC. Just like a keyboard, you can only enter characters.

2, and the serial port mode, is installed after the driver, can be virtual into a COM port,

If we use a scanner to input data to the application is certainly not possible, because the program needs to be able to run in the background can also use the scanner as data input. This requires the use of serial port mode.

Today, when I write the program, the problem is how can not trigger, datareceived events, finally found the problem, a few points

1, the serial port must be new instance, must and can correctly obtain the hardware serial port name.

2, Sp. Receivedbytesthreshold = 1; This is the default value of 1, which is to trigger the event with one byte of data.

3, Sp. rtsenable = True;sp. Dtrenable = true; These two properties must be set correctly, ha

That's why I haven't been able to trigger events correctly for a 3rd reason. Remember to add the event binding code AH.

public partial class Form1:form
{
SerialPort sp;
BOOL bisreading = false;
Dynamic d = new dynamic ();

Public Form1 ()
{
InitializeComponent ();
}

private void Button1_Click (object sender, EventArgs e)
{
BOOL B = false;
Try
{
SP = new SerialPort ();
Sp. BaudRate = 115200;
Sp. DataBits = 8;
Sp. Parity = Parity.none;
Sp. StopBits = Stopbits.one;
Sp. PortName = System.IO.Ports.SerialPort.GetPortNames () [0];

Sp. Rtsenable = true;
Sp. Dtrenable = true;
Sp. ReadTimeout = 3000;
Sp. Receivedbytesthreshold = 1;
Sp. DataReceived + = new System.IO.Ports.SerialDataReceivedEventHandler (this.sp_datareceived_1);
Sp. DataReceived + = sp_datareceived;
Sp. DataReceived-= sp_datareceived;
Sp. Open ();


}
catch (Exception Xe)
{
B = true;
MessageBox.Show ("Exception:" + XE. Message);

}
finally {
if (sp! = null && b = = true)
{
if (sp. IsOpen)
{
Sp. Close ();

} else
{
SP = null;
}

}

b = false;
}

}

private void Form1_formclosing (object sender, FormClosingEventArgs e)
{

Sp. Discardinbuffer ();
if (sp! = null)
{
Sp. Close ();
Sp. Dispose ();
}
}

private void Sp_datareceived_1 (object sender, Serialdatareceivedeventargs e)
{


if (bisreading = = True)
Return
Bisreading = true;
Try
{
StringBuilder currentline = new StringBuilder ();
Loop receive data
while (sp. Bytestoread > 0)
{
char ch = (char) sp. ReadByte ();
CurrentLine. Append (CH);
}
To handle the data received here.
MessageBox.Show ("Data is:" + currentline. ToString ());

GlobalPublic.BasePublic.ShowMessage ("Data is:" + currentline. ToString (), (basefrm as Baseform.frmbasedoc). Mtc_oglobal);
Invokedelegate del = new Invokedelegate (SetItem);
This. BeginInvoke (Del, CurrentLine. ToString ());
SetItem (CurrentLine. ToString ());
CurrentLine = new StringBuilder ();
}
catch (Exception ex)
{
MessageBox.Show ("Exception:" + ex.) Message);
}
Finally
{
Bisreading = false;
}
}
}
}

C # SerialPort's DataReceived event could not be triggered, using Honeywell's scanning gun and the corresponding USB to serial port driver.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.