Hi,
I am having problems with a device bulit by a colleague that needs to be working for Tuesday. If you talk to this device in HyperTerminal it errors all the time as it get the data from the UART too quickly it , he has found a thing call Advanced Serial Port Monitor which lets the user type a complete string before sending it. My code replicates this but I am having trouble seeing the replies
coming back while Advanced Serial Port Monitor says this is being sent I am not picking them up. My first though was a timer how ever this does not appear to work, I think now I have found a way around it using Events rather than the timers is this even possible???

private void port_DataReceivedATE(object sender, SerialDataReceivedEventArgs e) 
{ 
byte[] dataATE = new byte[myComPort.BytesToRead]; 
myComPort.Read(dataATE, 0, dataATE.Length); 
if ((dataATE.Length > 1) && (dataATE[dataATE.Length - 1] == 13)) 
{ 
Array.Resize(ref dataATE, dataATE.Length); 
} 
bufferATE.AddRange(dataATE); 
ProcessBufferATE(); 

} 
private void ProcessBufferATE() 
{ 
byte[] byteATE = new byte[buffer.Count]; 
buffer.CopyTo(0, byteATE, 0, buffer.Count); 
buffer.RemoveRange(0, buffer.Count); 

string dataATE; 
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); 
dataATE = enc.GetString(byteATE); 
this.BeginInvoke(new MethodInvoker(delegate() { rtbIncoming.Text += dataATE; })); 
} 

private void myComPort_DateRecived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 
{ 
}

This does even less than it did before. I'm guessing I need some code in the myComPort_DateRecived() the unit I am talking to has all commands framed by #, ie #L# or #W512#. what I'm thinking is "

if(myComPort.BytesToRead > 0) 
{ 
dataInATE = myComPort.Read() //not really sure which Read func to use???? 
if(dataInATE = (char)35) 
{ 
StartOfByte = true; 
} 
         if(StartOFByte == true) 
         {
         while(dataInATE != (char)35)
                 {
                      dataInATE += dataInATE;
                  }
          }

} 

//copy data out of dataInATE to a rich text box 
dataInATE =rtbIncoming.Text;

Umm will this get around the problem I'm having or is it going to get worse!

Glenn

Please use code tags when posting code on daniweb:

[code]

...code here...

[/code]

It sounds like the problem you're having is specific to the communication with the device and not necessarily the .NET framework. I'm not sure I understand what your question is either. You can use an event-driven architecture for reading data from the serial port instead of using a timer. Upload your project so we can take a look.

Hi,

Sorry will try to use code tags (I tried last time!) it is just [code] at the start [icode] at the end.
It is a little large shall I just post the bits that are causing problems?
Glenn

No you can click "Go Advanced" then "Manage Attachments" and upload a zip file with your project inside of it.

Hi,

Sorry will try to use code tags (I tried last time!) it is just (code) at the start (icode) at the end.
It is a little large shall I just post the bits that are causing problems?
Glenn

Code tags should be (CODE) and (./CODE) (without the '.'). (ICODE) is for inline code sections.

Rather than paste all of the code here, you can attach a copy of your project by clicking the "Go Advanced" button then "Manage Attachments"

Hi,

I am sorry I can't find the "Go Advanced" button.
I don't really want to dump the code on the web I belong to a customer (or will do soon ((quake, shudder)))

Glenn

Ho well,
I now feel a complete doughnut....
Code a comin' well a bit.....

private void btnOpen_Click(object sender, EventArgs e)
        {
            BOX_Port_timerTimer.Enabled = true;
            cboParity.Visible = false;
            cboHandShaking.Visible = false;
            cboDataBits.Visible = false;
            cboBaudRate.Visible = false;
            cboPort.Visible = false;
            cboStopBits.Visible = false;
        
            if (btnOpen.Text == "&Open")
            {
                OpenInstruments();
                
                myComPort.PortName = Convert.ToString(cboPort.Text);
                myComPort.BaudRate = Convert.ToInt32(cboBaudRate.Text);
                myComPort.Handshake = (Handshake)(Enum.Parse(typeof(Handshake), cboHandShaking.Text));
                myComPort.DataBits = Convert.ToInt16(cboDataBits.Text);
                myComPort.Parity = (Parity)Enum.Parse(typeof(Parity), cboParity.Text);
                myComPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cboStopBits.Text);

                if (!(myComPort.IsOpen))
                {
                    tmrBoxError.Enabled = true;
                    myComPort.Open();
                    btnOpen.Text = "C&lose";
                    btnClose.Enabled = false;
                    rtbOutgoing.Focus();
                    tmrPollForRecievedData.Enabled = true;
                    tmrTF930.Enabled = true;
                    Write_Out_Data();
                   // myComPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceivedATE);
                    Check_Instruments();
                    rbProgramBoardYes.Visible = true;
                    rbProgramNo.Visible = true;
                    Thread.Sleep(100);
                    tmrErrorCheck.Enabled = true;
                    
                    rtbOutgoing.Text = "#D1#";
                    Write_Out_Data();
                    Thread.Sleep(1000);
                    rtbOutgoing.Text = "#V1#";
                    Write_Out_Data();
                    Thread.Sleep(1000);
                    rtbOutgoing.Text = "#V0#";
                    Write_Out_Data();
                    Thread.Sleep(1000);
                    rtbOutgoing.Text = "#G1#";
                    Write_Out_Data();
                    Thread.Sleep(1000);
                    rtbOutgoing.Text = "#G0#";
                    Write_Out_Data();
                    Thread.Sleep(1000);
                    btnDUTcomms_Click(null, EventArgs.Empty);
                    Thread.Sleep(1000);
                    rtbOutgoing.Text = "#L#";
                    Write_Out_Data();
                    Thread.Sleep(1000);
                    //prgTests.Value++;
                 }
              }else if (btnOpen.Text == "C&lose")
            {
                tmrPollForRecievedData.Enabled = false;
                tmrPollForRecievedDUTData.Enabled = false;
                if (myComPort.IsOpen)
                    myComPort.Close();
                if(DUTComPort.IsOpen)
                    DUTComPort.Close();
                if (TF930FreqCount.IsOpen)
                    TF930FreqCount.Close(); 
                btnOpen.Text = "&Open";
                tmrErrorCheck.Enabled = false;
                tmrPollForRecievedData.Enabled = false;
                btnClose.Enabled = true;
                btnClose.Focus();
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        public void btnRead_Click(object sender, EventArgs e)
        {
            rtbIncoming.Text = myComPort.ReadExisting();
        }


        private void btnWrite_Click(object sender, EventArgs e)
        {

            Write_Out_Data();
            Thread.Sleep(200);

            tmrPollForRecievedData.Enabled = true;
            rtbOutgoing.Text = "";
            rtbOutgoing.Focus();
        }

        private void tmrPollForRecievedData_Tick(object sender, EventArgs e)
        {
            
            string charToRead;
        
            while (myComPort.BytesToRead > 0)
            {
                charToRead = (myComPort.ReadExisting());

                rtbIncoming.Text = rtbIncoming.Text + (charToRead);//+ System.Convert.ToString(charToRead);
            }

           
        }

        private void port_DataReceivedATE(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] dataATE = new byte[myComPort.BytesToRead];
            myComPort.Read(dataATE, 0, dataATE.Length);
            if ((dataATE.Length > 1) && (dataATE[dataATE.Length - 1] == 13))
            {
                Array.Resize(ref dataATE, dataATE.Length);
            }
            bufferATE.AddRange(dataATE);
            ProcessBufferATE();

        }
        private void ProcessBufferATE()
        {
            byte[] byteATE = new byte[buffer.Count];
            buffer.CopyTo(0, byteATE, 0, buffer.Count);
            buffer.RemoveRange(0, buffer.Count);

            string dataATE;
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            dataATE = enc.GetString(byteATE);
            this.BeginInvoke(new MethodInvoker(delegate() { rtbIncoming.Text += dataATE; }));
        }
        private void myComPort_DateRecived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.