Skip to content

Serial Port Event

Rob

Asked Nov 13, 2006, 9:27 PM EST1 answer
Share
LinkedInX
Hi everyone, I'm having a lot of trouble with the events of my serial com port. This is the peace of code I wrote: using System; using System.Collections.Generic; using System.Text; using System.IO.Ports; using System.Windows.Forms; namespace Demo.Device.NetworkLayer.Stack { public class BTPortControl { SerialPort SPCOM1 = new SerialPort(); public BTPortControl() { SPCOM1.PortName = "COM1"; SPCOM1.BaudRate = 115200; SPCOM1.Parity = Parity.None; SPCOM1.DataBits = 8; SPCOM1.StopBits = StopBits.One; SPCOM1.Handshake = Handshake.RequestToSend; SPCOM1.DtrEnable = false; SPCOM1.ReadBufferSize = 4096; SPCOM1.WriteBufferSize = 2048; SPCOM1.ParityReplace = 63; //Enable Event handler SPCOM1.DataReceived += new serialDataReceivedEventHandler(COM1_DataReceived); } public void COM1Open() { SPCOM1.Open(); } public void COM1Close() { SPCOM1.Close(); } public void COM1_DataReceived(object sender, SerialDataReceivedEventArgs e) { Mainwindow.Instance.UpdateStatusBox(SPCOM1.ReadExisting()); } } } When I send data to the serialport, I wait for a response, the COM1_DataReceived event is raised once, so I only see one line of data in my Status Box. After that, I don't receive any more data from that event. Can someone tell me what i'm doing wrong? Thanks guys! :D

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    It took me 10 mins to format this code so I could even read it. My best guesses:
    1) the serial device quit responding.
    2) you have a flow contol problem see DtrEnable and RtsEnable, neither specfically set in this code
    3) RecieveBytesThreshold is not set to 1
    4) Some other task is eating too much time so it gets missed. This can also happen if you are still processing the last serialDataReceivedEventHandler and new data arrives.

    Anyway thats my $0.02
    0