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. serial port and data sequencing problem...?

serial port and data sequencing problem...?

Scheduled Pinned Locked Moved C#
csharpquestionhelp
33 Posts 4 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.
  • R rahvyn6

    Hmm, how are you opening the port? I can run the line where you are getting the error. Here is how I open mine: try { if (comport.IsOpen) comport.Close(); else { // Set the port's settings comport.BaudRate = int.Parse(Settings.Default.BaudRate.ToString()); comport.DataBits = int.Parse(Settings.Default.DataBits.ToString()); comport.StopBits = (StopBits)Settings.Default.StopBits; comport.Parity = (Parity)Settings.Default.Parity; comport.PortName = Settings.Default.PortName.ToString(); // Open the port try { comport.Open(); } catch { } } } catch (Exception ex) { //no com port may be present log.Write("ERROR: " + ex.Message, "InitializePort"); }

    M Offline
    M Offline
    Mir_As
    wrote on last edited by
    #24

    my friend my computer will die soon:)second time my computer closed itself. i just want to sequence my 6 datas.

    R M L 5 Replies Last reply
    0
    • M Mir_As

      my friend my computer will die soon:)second time my computer closed itself. i just want to sequence my 6 datas.

      R Offline
      R Offline
      rahvyn6
      wrote on last edited by
      #25

      Im not sure what else to tell you without seeing the code as a whole. What I have posted should work. Com ports are pretty easy to work with, just make sure its opened correctly, then read the data and do what you need to do with it.

      1 Reply Last reply
      0
      • M Mir_As

        my friend my computer will die soon:)second time my computer closed itself. i just want to sequence my 6 datas.

        M Offline
        M Offline
        Mir_As
        wrote on last edited by
        #26

        i have done it many times.my problem is that: i have a simulation project in wpf.so my serial datas will will simulate my robot arm s axises.for example:i send from microcontrolor to computer this: for(;;) { delay_ms(1); putc(value1); putc(':'); putc(value2); putc(':'); putc(value3); putc(':') putc(value4); putc(':'); putc(value5); putc(':'); putc(value6); } so i must get datas like this: "value1" for "axis 1","value2" for "axis2" .... now could i explain my problem?

        1 Reply Last reply
        0
        • M Mir_As

          my friend my computer will die soon:)second time my computer closed itself. i just want to sequence my 6 datas.

          R Offline
          R Offline
          rahvyn6
          wrote on last edited by
          #27

          Ok, try concatinating all values into one string, then sending that string to the port all at once. putc(value1 & ":" & value2 & ":" & value3 & ":" & value4 & ":" & value5 & ":" & value6); Not sure what language you are using to send the information, so may need a different concatenation value. Then, when you recieve the data, you will get the whole string at once, and the Split() command will split all the values so data[0] will hold value1, data[1] will hold value2 and so on.

          1 Reply Last reply
          0
          • M Mir_As

            my friend my computer will die soon:)second time my computer closed itself. i just want to sequence my 6 datas.

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

            Sorry, but I doubt the entire concept of your project. 1) Reality check: you want to send 6 bytes of data at 9600 Baud ? with 1 start bit, 1 stop bit, no parity bit, a byte takes 10 bit times (a bit time is 1 second divided by the baudrate), that leads to a maximum of 960 bytes per second (lets call this 1000 bytes per second, or 1 msec/byte for easier calculations). Assuming you could send just 6 bytes (i.e. no overhead) you will need almost 6 msec to transfer the amount of data you want to transfer every msec !?!? 2) if 9600 Baud is the maximum your serial port (PC or target) can support, you are stuck. You need more bandwidth, i.e. a higher baudrate, or a different interface hardware all together; alternatively (and preferably, assuming data is not changing much) you should select some setup with buffering+compression at the producer side (target), and buffering+decompression at the consumer side (PC). 3) you dont need separation characters between the six bytes, they serve no purpose; you do need something that shows which byte is the first of a series of six. Lets assume you use 7 bytes: 1 special character indicating "start of data", followed by 6 data bytes. Your "start of data" value might also occur in one of the data bytes, and hence you could have a case of mistaken synchronization, but if you test for that and react properly, it will solve itself after a while (unless you are really unlucky; to understand assume 5 of the 6 values you need to send happen to also have that special value: how then can you tell to what channel belongs the one byte that differs ???) 4) It will not be obvious to get the PC listen to the serial data in real-time; PCs are not good in real-time stuff, they tend to do a whole lot of things, some in foreground, some in background, and typically fast enough to satisfy the human user (who tolerates delays of 10 or 100 msec, and occasionally a full second). If you need better, you must work with "real-time" priorities, which easily will ruin the behavior of all other processes unless you get everything correct, not a simple job. 5) The normal way to connect a device (such as a robot arm) to a computer (such as a PC) is by having a smarter target that executes intelligent commands, and reports higher-level results (things like: rotate 20 degrees per second for the next 3 seconds; and "rotation' done; new position=...". At that pace, you can easily have the PC follow the target. Compare this with a printer: you send one byte to instruct

            1 Reply Last reply
            0
            • M Mir_As

              i send 6 arranged datas from outside to c#.net with serialport.and i use serialport_datareceived(..) .i must get these datas in the c# arranged.but i cant get datas with sequence in the c#.how can i solve this problem?my code is this:i mean first data will be value[1]=first data coming from outside. value[2]=second data coming from outside .... i wish i could explain my problem:) private void myform_Loaded(object sender, EventArgs e) { seriport = new SerialPort(); seriport.DataReceived+=new SerialDataReceivedEventHandler(this.seriport_DataReceived); seriport.PortName = "COM4"; seriport.DataBits = 8; seriport.BaudRate = 9600; seriport.Parity =System.IO.Ports.Parity.None; seriport.StopBits = System.IO.Ports.StopBits.One; seriport.ReceivedBytesThreshold =6; seriport.ReadBufferSize =10000; seriport.WriteBufferSize =10000; if (seriport.IsOpen) seriport.Close(); seriport.Open(); } private void seriport_DataReceived(objectsender,SerialDataReceivedEventArgse) { byte[] values={0,0,0,0,0,0,0,0}; seriport.Read(values, 0, 6); //icall my functioun here. }

              E Offline
              E Offline
              Ed Poore
              wrote on last edited by
              #29

              If your micro is sending indivdual characters then in most cases the DataReceived event will be fired the number of times the characters are sent, i.e. you're trying to send 6 characters but by using 6 individual putcs it will fire off 6 DataReceived events.  The best way would be to have a buffer to do something like this:

              List<byte> buffer = new List<byte>();
              private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
              {
                  buffer.Add(serialPort.ReadByte());
                  if (buffer.Length == 6)
                  {
                     // Call your function here
                     YourFunctionCall(buffer.ToArray());
                     // Clear the buffer
                     buffer.Clear();
                  }
              }

              The problem you mentioned in the other thread about Invoke is basically because Windows is using a different thread to receive data from the SerialPort, this is the one which is the owner of the DataReceived event.  If you want to update for example some textboxes with teh values you receive you will have to do something like this:

              private delegate void UpdateTextBoxes(byte[] values);
              private void YourFunctionCall(byte[] values)
              {
                  this.Invoke(new UpdateTextBoxes(this.UpdateTextBoxes));
              }
              privaet vodi UpdateTextBoxes(byte[] values)
              {
                  this.txt1.Text = values[0].ToString();
                  this.txt2.Text = values[1].ToString();
                  // ...
              }

              You might want to take a look at my article[^] which looks into this problem.


              My Blog

              M 1 Reply Last reply
              0
              • M Mir_As

                my friend my computer will die soon:)second time my computer closed itself. i just want to sequence my 6 datas.

                M Offline
                M Offline
                Mir_As
                wrote on last edited by
                #30

                i didnt do it:(i tried many codes but i didnt:(.i will try more i must do it.thanks All of:)

                1 Reply Last reply
                0
                • E Ed Poore

                  If your micro is sending indivdual characters then in most cases the DataReceived event will be fired the number of times the characters are sent, i.e. you're trying to send 6 characters but by using 6 individual putcs it will fire off 6 DataReceived events.  The best way would be to have a buffer to do something like this:

                  List<byte> buffer = new List<byte>();
                  private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
                  {
                      buffer.Add(serialPort.ReadByte());
                      if (buffer.Length == 6)
                      {
                         // Call your function here
                         YourFunctionCall(buffer.ToArray());
                         // Clear the buffer
                         buffer.Clear();
                      }
                  }

                  The problem you mentioned in the other thread about Invoke is basically because Windows is using a different thread to receive data from the SerialPort, this is the one which is the owner of the DataReceived event.  If you want to update for example some textboxes with teh values you receive you will have to do something like this:

                  private delegate void UpdateTextBoxes(byte[] values);
                  private void YourFunctionCall(byte[] values)
                  {
                      this.Invoke(new UpdateTextBoxes(this.UpdateTextBoxes));
                  }
                  privaet vodi UpdateTextBoxes(byte[] values)
                  {
                      this.txt1.Text = values[0].ToString();
                      this.txt2.Text = values[1].ToString();
                      // ...
                  }

                  You might want to take a look at my article[^] which looks into this problem.


                  My Blog

                  M Offline
                  M Offline
                  Mir_As
                  wrote on last edited by
                  #31

                  if i cant solve this problem i will write 1000000000 messages: ).it s so easy but it s not easy in real application:) i will try last helper 's codes:) i wish i can solve

                  E 1 Reply Last reply
                  0
                  • M Mir_As

                    if i cant solve this problem i will write 1000000000 messages: ).it s so easy but it s not easy in real application:) i will try last helper 's codes:) i wish i can solve

                    E Offline
                    E Offline
                    Ed Poore
                    wrote on last edited by
                    #32

                    I would try keeping an internal buffer as in my example because there is no guarantee of how Windows will receive the data, although the serial port has one of the highest IRQs of any hardware if Windows decides to go and do something then it decides to go and do it.  I had to do something similar when parsing NMEA data from a GPS receiver, lines would come in a bit at a time.


                    My Blog

                    M 1 Reply Last reply
                    0
                    • E Ed Poore

                      I would try keeping an internal buffer as in my example because there is no guarantee of how Windows will receive the data, although the serial port has one of the highest IRQs of any hardware if Windows decides to go and do something then it decides to go and do it.  I had to do something similar when parsing NMEA data from a GPS receiver, lines would come in a bit at a time.


                      My Blog

                      M Offline
                      M Offline
                      Mir_As
                      wrote on last edited by
                      #33

                      i slept yesterday 03:30 at night :(.i cant solve.i m not good at in visual c# so much.i have tried your codes but when i debug project so i see this: "Visual Studio can not start debugging because the debug target 'C:\my project location\windowsapplication1.exe"' is missing.Please buld the project ........" but i didnt anything wrong:(.i can go mad soon:(

                      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