MSComm initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    #1

    MSComm initialization

    Using C# .NET

    I've implemented serial com with MSComm. My application works with a
    few errors. Most importantly, could someone tell me why the OnComm
    event fires twice. Let me explain:

    I have a data file that is sent across the serial port. The OnComm
    event fires indicating activity. With a file of let's say 4 records.
    Stepping through my app I noticed that in my com.Input variable, the
    first and only the first record is visible. That record is processed,
    but then the app continues to the same method (no while loop, just an
    if) only this time the variable contains the remaining records in my
    file.

    Threshold is set to 75 wanting the event to fire at that number,
    InputLen set to 0. I've included a snippet of code to help clarify.

    Help me see what I'm missing.

    FORM LOAD
    private void Form1_Load(obje ct sender, System.EventArg s e)
    {
    com.CommPort = 1;
    if (com.PortOpen) com.PortOpen = false;
    com.EOFEnable = true;
    com.RThreshold = 75;
    com.Settings = "9600,n,8,1 ";
    com.DTREnable = true;
    com.Handshaking = MSCommLib.Hands hakeConstants.c omNone;
    com.InputMode = MSCommLib.Input ModeConstants.c omInputModeText ;
    com.InputLen = 0;
    com.NullDiscard = false;
    com.OnComm += new System.EventHan dler(this.OnCom m);
    com.PortOpen = true;

    }

    ONCOMM EVENT

    private void OnComm(object sender, System.EventArg s e)
    {
    if (com.InBufferCo unt > 0)
    {
    ProcessComData( (string) com.Input);
    }

    }

    PROCESS DATA ****** OCCURS TWICE *******

    private void ProcessComData( string input)
    {
    lstData.Items.A dd(input);
    StreamWriter listwriter = new StreamWriter("C :\\Interrogator .txt",
    false);
    listwriter.Writ e(input);
    listwriter.Clos e();
    //Delay or breakpoint is necessary for accurate output.
    MessageBox.Show ("New information available", "TITLE",
    MessageBoxButto ns.OKCancel, MessageBoxIcon. Asterisk);
    }
Working...