Author Topic: Strange Serial Port:  (Read 14519 times)

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Strange Serial Port:
« on: December 13, 2012, 08:16:22 am »
Hi All,

Same board I was fiddling with all week being that I have to get some thing done and out to a customer Today.  I have to talk to a board the same as HyperTerminal.  My first step was to look over the comms spec 2400, 8, None, 1 and set a program to send out a command (in C#) using an application I have written the sending routine is
  private void rtbOutgoing_KeyPress(object sender, KeyPressEventArgs e)
        {   
              if (e.KeyChar == (char)13) // enter key 
              {        myComPort.Write("\r\n");     
                  Thread.Sleep(200);   
                  rtbOutgoing.Text = "";
              }
              else if (e.KeyChar < 32 || e.KeyChar > 126) 
              {   
                    e.Handled = true; // ignores anything else outside printable ASCII range 
              }
              else
             {   
                 myComPort.Write(e.KeyChar.ToString());
             } 
This routine works for sending out what evers in the output text box (rtbOutgoing keypress routine).
This appears not to work so I tried to use a routine that was developed to ensure all data from a reply was got.

  private string Write_Board(string Data_To_Board)
        {

            Pause.Start();
           
             myComPort.Write(Data_To_Board + (char)10 + (char)13);
            {

                while (Reply_Status == (int)REPLY.NO_REPLY)
                {
                    NoDataAtPort.Enabled = true;
                }
                NoDataAtPort.Enabled = false;

                if (Reply_Status == (int)REPLY.TIMEOUT_REPLY)
                {
                    Data_From_Board = "TIMEOUT\n";
                    Pause.Stop();
                }
                else if (Reply_Status == (int)REPLY.YES_REPLY)
                {
                    Pause.Stop();
                    //add try catch
                    try
                    {
                        InputData = myComPort.ReadExisting();
                        if (InputData != String.Empty)
                        {
                            this.BeginInvoke(new SetTextCallback(SetText), new object[] { InputData });
                            ProcessInput();
                        }
                        else if (InputData == String.Empty)
                        {
                            return (null);
                        }

                    }
                    catch (UnauthorizedAccessException)
                    {
                         MessageBox.Show("Error Caught!!" );
                    }

                }
                else
                {
                   
                    Cursor.Current = Cursors.No;
                    Reply_Status = (int)REPLY.NO_REPLY;
                }
            }
        //    ProcessInput();
       //     Data_From_Board = Data_From_Board + "|||";
         
      //      return (Data_From_Board);
    return("");
        }


This was called by the line of code Write_Board(command)  example follows
             Reply_Status = (int)REPLY.NO_REPLY;
            Write_Board("/");

 Reply Status is an Enumerated list as follows
    enum REPLY : int { NO_REPLY, TIMEOUT_REPLY, YES_REPLY }

The Data Recieved Event has the following
    private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Reply_Status = (int)REPLY.YES_REPLY;
         }

I was a little supprised when this worked and returned data.  As this method had originally created for ensuring the complete command was sent.  As I mentioned earlier (previous question) this board uses a one wire interface for communications so I get back an echo of what I sent (not a problem I can filter out these replies) I am playing at Debugging/Trying ideas while I am typing this,
I have a dodgy solution at the moment in that if I send the command twice with a gap from a Thread Sleep, while this is not ideal (far from it) it does appear to work, however I will need a proper solution quickly.  As far as I can see I am doing everything properly.  This board is being troublesome.  Can anyone give me a pointer (or website/book recomendation) to doing single wire comms, as Sending the commands twice to get a reply is awkward, on the a spy HyperTerminal window I have set up a second USB serial port to spy on the Comms with the board I can see it is replying twice but my program will only display the second. WHY???
Glenn

 

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: Strange Serial Port:
« Reply #2 on: December 13, 2012, 02:38:07 pm »
Hmm...Looks like it might, I also believe that the NEC part is in 1/2 Duplex mode that is possible why I need to send each command twice to get a responce. I will look at that more throughly tomorrow as it has been a long day!
Glenn!