using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace YD_AD_Client
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
InitComPort();
//com.Output = "Serial Terminal Initialized";
}
private void InitComPort()
{
// Set the com port to be 1
com.CommPort = 1;
// This port is already open, close it to reset it.
if (com.PortOpen) com.PortOpen = false;
// Trigger the OnComm event whenever data is received
com.RThreshold = 1;
// Set the port to 9600 baud, no parity bit, 8 data bits, 1 stop bit (all standard)
com.Settings = "4800,n,8,1";
// Force the DTR line high, used sometimes to hang up modems
com.DTREnable = true;
// No handshaking is used
com.Handshaking = MSCommLib.HandshakeConstants.comNone;
// Don't mess with byte arrays, only works with simple data (characters A-Z and numbers)
com.InputMode = MSCommLib.InputModeConstants.comInputModeText;
// Use this line instead for byte array input, best for most communications
//com.InputMode = MSCommLib.InputModeConstants.comInputModeText;
// Read the entire waiting data when com.Input is used
com.InputLen = 0;
// Don't discard nulls, 0x00 is a useful byte
com.NullDiscard = false;
// Attach the event handler
com.OnComm += new System.EventHandler(this.OnComm);
// Open the com port
com.PortOpen = true;
}
private void OnComm(object sender, EventArgs e) // MSCommLib OnComm Event Handler
{
// If data is waiting in the buffer, process it.
// Note: This is using the string method for simple data, be sure
// to use byte arrays (described below) for more generic data.
if (com.InBufferCount > 0) ProcessComData((string)com.Input);
}
private void ProcessComData(string input)
{
// Send incoming data to a Rich Text Box
rtfTerminal.AppendText(input + "\n");
}