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.
  • 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. }

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

    Is the data coming into the port delimited? Have you tried using seriport.ReadLine(), then parsing out the line?

    M 1 Reply Last reply
    0
    • R rahvyn6

      Is the data coming into the port delimited? Have you tried using seriport.ReadLine(), then parsing out the line?

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

      i have 6 datas.and each of data between 0-255 (just one byte).if i use serialport.readline() so how can i separate my datas in visual c#? for example my datas: 10 245 100 12 250 8 when i use serialport_readline() so how can i seperate these_another question is that will i get datas with arranged?

      R 1 Reply Last reply
      0
      • M Mir_As

        i have 6 datas.and each of data between 0-255 (just one byte).if i use serialport.readline() so how can i separate my datas in visual c#? for example my datas: 10 245 100 12 250 8 when i use serialport_readline() so how can i seperate these_another question is that will i get datas with arranged?

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

        If you are not using a delmiting character, such as a comma or colon, then you would not be able to seperate the values. What does the string look like when it is sent to the port?

        M 1 Reply Last reply
        0
        • R rahvyn6

          If you are not using a delmiting character, such as a comma or colon, then you would not be able to seperate the values. What does the string look like when it is sent to the port?

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

          i use "putc(value)".i use microcontrolor.i use 6 times putc(value); putc(value1); putc(value2); . . . or i can use: printf("%03u %03u "%03u %03u "%03u %03u ",value1,value2,...); i must solve this problem.cause my greduation work must be finish:(

          R 1 Reply Last reply
          0
          • M Mir_As

            i use "putc(value)".i use microcontrolor.i use 6 times putc(value); putc(value1); putc(value2); . . . or i can use: printf("%03u %03u "%03u %03u "%03u %03u ",value1,value2,...); i must solve this problem.cause my greduation work must be finish:(

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

            Can you insert a delimiter between the values using the printf? If so, then you can use ReadLine() on the serialport, and you would know that they are in the order you sent them. When you get the value, you can use .Split() on it, and put the values into a string array. Somthing like this: String[] data; data = seriport.ReadLine().Split(System.Convert.ToChar(":"));

            M 1 Reply Last reply
            0
            • R rahvyn6

              Can you insert a delimiter between the values using the printf? If so, then you can use ReadLine() on the serialport, and you would know that they are in the order you sent them. When you get the value, you can use .Split() on it, and put the values into a string array. Somthing like this: String[] data; data = seriport.ReadLine().Split(System.Convert.ToChar(":"));

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

              will i send ":" at the last of 6 values or between each of variable will be ":" 1)value1:value2:value3:value4:value5:value6: like this? 2)value1value2value3value4value5value6: or like this?

              R 1 Reply Last reply
              0
              • M Mir_As

                will i send ":" at the last of 6 values or between each of variable will be ":" 1)value1:value2:value3:value4:value5:value6: like this? 2)value1value2value3value4value5value6: or like this?

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

                Like this: value1:value2:value3:value4:value5:value6 You dont need one after value6, but you'll need one in between each of the other values.

                M 1 Reply Last reply
                0
                • R rahvyn6

                  Like this: value1:value2:value3:value4:value5:value6 You dont need one after value6, but you'll need one in between each of the other values.

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

                  can i ask you different question? when i programme goes to serialport_datareceived(..) so how can i break this.and i want to call another function how can i do it?

                  R 1 Reply Last reply
                  0
                  • M Mir_As

                    can i ask you different question? when i programme goes to serialport_datareceived(..) so how can i break this.and i want to call another function how can i do it?

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

                    I'm not sure I understand what you are asking. You should be able to call a function from with the data_recieved() function. Like this: private void seriport_DataReceived(object sender, SerialDataReceivedEventArgs e) { // This method will be called when there is data waiting in the port's buffer string[] data; data = comport.ReadLine().Split(System.Convert.ToChar(":")); FunctionCall(); }

                    M 1 Reply Last reply
                    0
                    • R rahvyn6

                      I'm not sure I understand what you are asking. You should be able to call a function from with the data_recieved() function. Like this: private void seriport_DataReceived(object sender, SerialDataReceivedEventArgs e) { // This method will be called when there is data waiting in the port's buffer string[] data; data = comport.ReadLine().Split(System.Convert.ToChar(":")); FunctionCall(); }

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

                      i mean that must i break this thread?i tried to write serial data in serialport_datareceived();function but i didnt do it. must i use some "invoke" code and "break" like break codes?

                      R M 2 Replies Last reply
                      0
                      • M Mir_As

                        i mean that must i break this thread?i tried to write serial data in serialport_datareceived();function but i didnt do it. must i use some "invoke" code and "break" like break codes?

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

                        Can you post the portion of the code you are having problems with?

                        1 Reply Last reply
                        0
                        • M Mir_As

                          i mean that must i break this thread?i tried to write serial data in serialport_datareceived();function but i didnt do it. must i use some "invoke" code and "break" like break codes?

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

                          i ll try:) did u write anything in this function?i mean did u try to write this data do textboxes in serialport_datareceived(..)?i tried but i didnt do it:)

                          R 1 Reply Last reply
                          0
                          • M Mir_As

                            i ll try:) did u write anything in this function?i mean did u try to write this data do textboxes in serialport_datareceived(..)?i tried but i didnt do it:)

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

                            What errors are you getting? Are you simply trying to set the .Text value of a textbox?

                            M 1 Reply Last reply
                            0
                            • R rahvyn6

                              What errors are you getting? Are you simply trying to set the .Text value of a textbox?

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

                              yes.i cant remember now. now i try "readline" :).but my variables come very frequently.so must i clear "string[] data" ?my data will come every milisecond

                              R 1 Reply Last reply
                              0
                              • M Mir_As

                                yes.i cant remember now. now i try "readline" :).but my variables come very frequently.so must i clear "string[] data" ?my data will come every milisecond

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

                                Yes, once you parse the recieved data, you can clear the array that holds it. Another option would be to close the port until you need it opened again. To clear the array, I think this should work: Array.Clear(data, 0, data.Length);

                                M 1 Reply Last reply
                                0
                                • R rahvyn6

                                  Yes, once you parse the recieved data, you can clear the array that holds it. Another option would be to close the port until you need it opened again. To clear the array, I think this should work: Array.Clear(data, 0, data.Length);

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

                                  "cause of thread output or an application I/O prossesing was canceled" i get this warning:)i didnt get serial datas:) i think i must write invoke but how:)

                                  R 1 Reply Last reply
                                  0
                                  • M Mir_As

                                    "cause of thread output or an application I/O prossesing was canceled" i get this warning:)i didnt get serial datas:) i think i must write invoke but how:)

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

                                    Do you get any errors when you open the port? Also, can you post the code within the data_recieved event handler?

                                    M 1 Reply Last reply
                                    0
                                    • R rahvyn6

                                      Do you get any errors when you open the port? Also, can you post the code within the data_recieved event handler?

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

                                      string[] data; private void serialPort1_DataReceived(object sender,System.IO.Ports.SerialDataReceivedEventArgs e) { //problemhere//data=serialPort1.ReadLine().Split(System.Convert.ToChar(":")); serialPort1.Close(); write(); }

                                      R 1 Reply Last reply
                                      0
                                      • M Mir_As

                                        string[] data; private void serialPort1_DataReceived(object sender,System.IO.Ports.SerialDataReceivedEventArgs e) { //problemhere//data=serialPort1.ReadLine().Split(System.Convert.ToChar(":")); serialPort1.Close(); write(); }

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

                                        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 2 Replies Last reply
                                        0
                                        • 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
                                          #21

                                          soon before i see blue screen on DOS:) it must be dangerous error?.now i ll try your code.

                                          M R 2 Replies 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