Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. problem with sending message to serial port with timer [modified]

problem with sending message to serial port with timer [modified]

Scheduled Pinned Locked Moved C#
databaseregexhelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Honeyboy_20
    wrote on last edited by
    #1

    Hi guys , I have problem with sending message to serial port I have arraylist called MessageTimeAssociation and in it's first index of every message I have the time which determine when I will send Message into serial Port. ex. if the first index value =5 I should send message after 5 seconds so I am variable xCounter and I increment it with timer but My problem if time of message not equal to the xCounter the condition not success so the message skipped. So I want this condition wait execution while Xcounter match the value of time. How i can do that ?

    ArrayList MessageTimeAssociation = new ArrayList();
    int xCounter = 0;
    private void timer1_Tick(object sender, EventArgs e)
    {
    foreach (string[] msg in MessageTimeAssociation)
    {
    // I want this condition wait execution while Xcounter match the value of time
    if ((msg[1] == (setListBoxIndex.ToString()) &$ (int.Parse(msg[0])) == xCounter))
    {
    ConnectToSerialPort();
    int q = 0;
    numberOfMessagesInSection = msg.Length;
    for (q = 2; q < msg.Length; q++)
    {
    byte[] myTempRcv ={ byte.Parse(msg[q]) };
    serialPort.Write(myTempRcv, 0, 1);
    }
    }
    }
    xCounter++; ;
    }

    L 1 Reply Last reply
    0
    • H Honeyboy_20

      Hi guys , I have problem with sending message to serial port I have arraylist called MessageTimeAssociation and in it's first index of every message I have the time which determine when I will send Message into serial Port. ex. if the first index value =5 I should send message after 5 seconds so I am variable xCounter and I increment it with timer but My problem if time of message not equal to the xCounter the condition not success so the message skipped. So I want this condition wait execution while Xcounter match the value of time. How i can do that ?

      ArrayList MessageTimeAssociation = new ArrayList();
      int xCounter = 0;
      private void timer1_Tick(object sender, EventArgs e)
      {
      foreach (string[] msg in MessageTimeAssociation)
      {
      // I want this condition wait execution while Xcounter match the value of time
      if ((msg[1] == (setListBoxIndex.ToString()) &$ (int.Parse(msg[0])) == xCounter))
      {
      ConnectToSerialPort();
      int q = 0;
      numberOfMessagesInSection = msg.Length;
      for (q = 2; q < msg.Length; q++)
      {
      byte[] myTempRcv ={ byte.Parse(msg[q]) };
      serialPort.Write(myTempRcv, 0, 1);
      }
      }
      }
      xCounter++; ;
      }

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, that is pretty confusing. Here are some ideas: 1. please show your code inside PRE tags, not CODE tags. That will preserve formatting and indentation, making things much more readable. 2. also show the declarations of the variables you are using. 3. it looks as if each item in the array list is a string array, with some specific values at specific locations. That is not so good. You should define your own type (class or struct) and store instances thereof inside your collection; the collection could better be a generic list, ArrayList is archaic and has no advantage over a generic list. 4. I am surprised you seem to have a "ConnectToSerialPort" inside a loop. I doubt that is correct. 5. This is what I would do: - keep the messages to be sent (each as an instance of some Message class) inside a generic list, which is ordered by increasing due datetime. - have a timer ticking at an appropriate interval (e.g. 1 second), then checking the current time with the time of the first entry in the list; if current time greater equal due time, remove message from list and send it. This means the tick method would never wait, and that is how it should be: event handlers should never wait, since they would immobilize the GUI while they wait. :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      H 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, that is pretty confusing. Here are some ideas: 1. please show your code inside PRE tags, not CODE tags. That will preserve formatting and indentation, making things much more readable. 2. also show the declarations of the variables you are using. 3. it looks as if each item in the array list is a string array, with some specific values at specific locations. That is not so good. You should define your own type (class or struct) and store instances thereof inside your collection; the collection could better be a generic list, ArrayList is archaic and has no advantage over a generic list. 4. I am surprised you seem to have a "ConnectToSerialPort" inside a loop. I doubt that is correct. 5. This is what I would do: - keep the messages to be sent (each as an instance of some Message class) inside a generic list, which is ordered by increasing due datetime. - have a timer ticking at an appropriate interval (e.g. 1 second), then checking the current time with the time of the first entry in the list; if current time greater equal due time, remove message from list and send it. This means the tick method would never wait, and that is how it should be: event handlers should never wait, since they would immobilize the GUI while they wait. :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        H Offline
        H Offline
        Honeyboy_20
        wrote on last edited by
        #3

        I am Update the problem.

        L 1 Reply Last reply
        0
        • H Honeyboy_20

          I am Update the problem.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          OK, you have taken care of my earlier #1 and #2. That still leaves #3, #4, and #5. I also noticed something else, so: 6.

          for (q = 2; q < msg.Length; q++) {
          byte[] myTempRcv ={ byte.Parse(msg[q]) };
          serialPort.Write(myTempRcv, 0, 1);
          }

          does not look good. Why would you send one byte at a time? here is something much more efficient:

          int length=msg.Length;
          byte[] myTempRcv =new byte[length];
          for (q = 2; q < length; q++) {
          byte[q]=byte.Parse(msg[q]);
          }
          serialPort.Write(myTempRcv, 2, length-2);

          BTW: I am not sure the indexes are all correct, [ADDED] in fact I'm not sure your original code is correct as I don't know what the intention is, what the original content of msg is, and what exactly should be written to the serial port. [/ADDED] If you need more help about this, please provide an example with actual numbers (in decimal, or in hexadecimal, whatever suits you). :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          modified on Sunday, March 27, 2011 7:29 PM

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups