Serial Port reading and writing class of honeywell 1500/1300 code gun, honeywell1300
This type is used for reading and writing the serial port of honeywell 1500/1300 code gun, which can scan the control bar code gun and obtain the scanned data from the bar code gun. The serial port mode of the bar code gun has the following advantages:
(1) Avoid input failure because the text box loses focus.
(2) works with automation devices.
/* ---------------------------------------------------------------- // File name: barcode=. cs // file Function Description: barcode scanner class. Used to control the bar code gun for scanning. /// Create an ID: leo 2012.10.16 /// modify the ID: // modify the description: // modify the ID: // modify the description: // ------------------------------------------------------------------ */using System; using System. collections. generic; using System. text; using System. IO. ports; using System. windows. forms; using System. threading; using System. IO; namespace QMS. infrastructure {public class barcode= {//******************************** * ********************** **************************************** ********************************* // <Summary> /// serial object. /// </Summary> SerialPort comm = new SerialPort (); // <summary> // form object. /// </Summary> Form _ frm; //************************************** ********************** **************************************** * ************ // <summary> // return the bar code value. The barcode value should be retrieved when the barcode scan is completed. /// </Summary> public string Barcode {set; get ;} //************************************** *********************** **************************************** * ************ /// <summary> // The event is scanned. /// </Summary> public event EventHandler ScanFinished; //************************************** ********************** **************************************** * ***** // <summary> // open the device. /// </Summary> /// <param name = "portName"> serial port number </param> /// <param name = "frm"> form </param> public void Open (string portName, form frm) {try {// initialize the Form object _ frm = frm; _ frm. formClosing + = new FormClosingEventHandler (_ frm_FormClosing); // initialize the SerialPort object comm. portName = portName; comm. baudRate = 19200; // The value of honeywell is comm. open (); comm. dataReceived + = comm_DataReceived; // Add event registration} catch (Exception e) {MessageBox. Show (e. Message) ;}/// <summary> /// bar code gun for scanning. /// </Summary> public void Scan () {// Scan timeout monitoring // tmr. enabled = true; // trigger scan byte [] B = {22, 84, 13}; // The hexadecimal number is 16 54 0d, the three numbers in parentheses are in decimal Format string s = System. text. encoding. ASCII. getString (B); if (comm. isOpen = true) {comm. write (s);} else {MessageBox. show ("Scan failed! Check the connection or restart the program ") ;}/// <summary> /// close the device. /// </Summary> public void Close () {if (comm. isOpen = true) {comm. close (); comm. dispose ();}} //************************************** ********************** **************************************** * ***** // <summary> // serial data receiving function. When data is received, the ScanFinished event is triggered. /// </Summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> void comm_DataReceived (object sender, serialDataReceivedEventArgs e) {// scan timeout monitoring. If the data is returned, the monitoring is disabled // tmr. enabled = false; // read the serial port returned value this. barcode = comm. readLine (). trim (); // triggers the event if (this. scanFinished! = Null) {_ frm. invoke (new MethodInvoker (delegate {this. scanFinished (this, new EventArgs () ;}}/// <summary> /// close the serial port connection when the form is closed. /// </Summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> void _ frm_FormClosing (object sender, formClosingEventArgs e) {this. close ();}}}