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 communication between computer and another hardware device via RS232 in C# for windows Application

serial port communication between computer and another hardware device via RS232 in C# for windows Application

Scheduled Pinned Locked Moved C#
helpcsharphardwaretutorial
10 Posts 6 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.
  • A Offline
    A Offline
    Arpita Patel
    wrote on last edited by
    #1

    I am developing an application in C#.NET , for transferring data between two PC's using RS232 protocol. For communication, i m using "SerialPort" class of "System.IO.Ports" namespace. but now i have a problems in recieveing the data and sending the data. even one major problem is whenever i'll recieve at that time i have to identify the begining of message and ending of message as well as if two messagees are recieveing at thae same time then how to differentiate that messagees. please help me i nthat. i really stuck in that. thank you in advance

    Arpita Patel

    M J L T 4 Replies Last reply
    0
    • A Arpita Patel

      I am developing an application in C#.NET , for transferring data between two PC's using RS232 protocol. For communication, i m using "SerialPort" class of "System.IO.Ports" namespace. but now i have a problems in recieveing the data and sending the data. even one major problem is whenever i'll recieve at that time i have to identify the begining of message and ending of message as well as if two messagees are recieveing at thae same time then how to differentiate that messagees. please help me i nthat. i really stuck in that. thank you in advance

      Arpita Patel

      M Offline
      M Offline
      monstale
      wrote on last edited by
      #2

      Hi, did you mention to use a start and a stop character, e.g. (char)1 start and (char)2 to stop. ;-) Then use

      // Delete parts of other message
      recvStr = recvStr.Remove(0, recvStr.IndexOf((char)1)-1);
      // Cut message out of the recived string
      msg = recvStr.Substring(recvStr.IndexOf((char)1), recvStr.IndexOf((char)2, recvStr.IndexOf((char)1));

      Kind regards

      A 1 Reply Last reply
      0
      • A Arpita Patel

        I am developing an application in C#.NET , for transferring data between two PC's using RS232 protocol. For communication, i m using "SerialPort" class of "System.IO.Ports" namespace. but now i have a problems in recieveing the data and sending the data. even one major problem is whenever i'll recieve at that time i have to identify the begining of message and ending of message as well as if two messagees are recieveing at thae same time then how to differentiate that messagees. please help me i nthat. i really stuck in that. thank you in advance

        Arpita Patel

        J Offline
        J Offline
        Jimmanuel
        wrote on last edited by
        #3

        To parse individual messages out of a stream of them there needs to be a start message flag that signals the beginning of a message. Just pick a unique character or pattern of characters and put that at the beginning of each message. Pick a different flag and put it at the end to identify where each message stops.

        1 Reply Last reply
        0
        • A Arpita Patel

          I am developing an application in C#.NET , for transferring data between two PC's using RS232 protocol. For communication, i m using "SerialPort" class of "System.IO.Ports" namespace. but now i have a problems in recieveing the data and sending the data. even one major problem is whenever i'll recieve at that time i have to identify the begining of message and ending of message as well as if two messagees are recieveing at thae same time then how to differentiate that messagees. please help me i nthat. i really stuck in that. thank you in advance

          Arpita Patel

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

          Hi, if you can choose the protocol freely, then this would probably be the easiest set-up with reasonable reliability: - send everything as text using ASCII, i.e. don't use binary data; - end each message on a reserved character, \n is a good choice; you can achieve this by setting SerialPort.NewLine to '\n' and using WriteLine() - create a receiver thread, which basically is an eternal loop performing a ReadLine(); this one will block until a SerialPort.NewLine was received, then return all the data received so far, which would be exactly one message. WARNING: the receiver thread cannot directly access any GUI Controls, it needs Control.Invoke for that. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


          1 Reply Last reply
          0
          • M monstale

            Hi, did you mention to use a start and a stop character, e.g. (char)1 start and (char)2 to stop. ;-) Then use

            // Delete parts of other message
            recvStr = recvStr.Remove(0, recvStr.IndexOf((char)1)-1);
            // Cut message out of the recived string
            msg = recvStr.Substring(recvStr.IndexOf((char)1), recvStr.IndexOf((char)2, recvStr.IndexOf((char)1));

            Kind regards

            A Offline
            A Offline
            Arpita Patel
            wrote on last edited by
            #5

            I used this code for sending the data private void SendData() { if (CurrentDataMode == DataMode.Text) { // Send the user's text straight out the port comport.Write(txtSendData.Text); // Show in the terminal window the user's text Log(LogMsgType.Outgoing, txtSendData.Text + "\n"); } else { try { // Convert the user's string of hex digits (ex: B4 CA E2) to a byte array byte[] data = HexStringToByteArray(txtSendData.Text); // Send the binary data out the port comport.Write(data, 0, data.Length); // Show the hex digits on in the terminal window Log(LogMsgType.Outgoing, ByteArrayToHexString(data) + "\n"); } catch (FormatException) { // Inform the user if the hex string was not properly formatted Log(LogMsgType.Error, "Not properly formatted hex string: " + txtSendData.Text + "\n"); } } txtSendData.SelectAll(); } For the Recieving purpose I used private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { // This method will be called when there is data waiting in the port's buffer // Determain which mode (string or binary) the user is in if (CurrentDataMode == DataMode.Text) { // Read all the data waiting in the buffer string data = comport.ReadExisting(); // Display the text to the user in the terminal Log(LogMsgType.Incoming, data); } else { // Obtain the number of bytes waiting in the port's buffer int bytes = comport.BytesToRead; // Create a byte array buffer to hold the incoming data byte[] buffer = new byte[bytes]; // Read the data from the port and store it in our buffer comport.Read(buffer, 0, bytes); // Show the user the incoming data in hex format Log(LogMsgType.Incoming, ByteArrayToHexString(buffer)); } } now give me appropriate answer for my question. thanks a lot fro replying me.

            Arpita Patel

            1 Reply Last reply
            0
            • A Arpita Patel

              I am developing an application in C#.NET , for transferring data between two PC's using RS232 protocol. For communication, i m using "SerialPort" class of "System.IO.Ports" namespace. but now i have a problems in recieveing the data and sending the data. even one major problem is whenever i'll recieve at that time i have to identify the begining of message and ending of message as well as if two messagees are recieveing at thae same time then how to differentiate that messagees. please help me i nthat. i really stuck in that. thank you in advance

              Arpita Patel

              T Offline
              T Offline
              Tr v
              wrote on last edited by
              #6

              I do a lot of RS232 programming for my job. All of our messages usually start with a special character, such as the hex 0x02 character, followed by a message identifier that we use to let the program know what to do with the message. Then the message is ASCII encoded text followed by a termination character, such as hex 0x03. When a message is received, the starting character, message identifier, and ending character are stripped from the message which is then passed to whatever part of the application it is intended for. If you are having trouble with receiving and sending data, make sure that you are using a crossover cable which has the send and receive wires switched at one end of the cable. If you use a straight through cable then the receiving computer will not be getting the data on the receive pin but the transmit pin, which it won't process. By definition, serial communication means that you cannot get two messages at the same time as you are transmitting the bytes in a 'series'. So as long as you are looking for the stop character to terminate a message then the next message should not get jumbled or lost as long as you don't overrun the buffer.

              U 2 Replies Last reply
              0
              • T Tr v

                I do a lot of RS232 programming for my job. All of our messages usually start with a special character, such as the hex 0x02 character, followed by a message identifier that we use to let the program know what to do with the message. Then the message is ASCII encoded text followed by a termination character, such as hex 0x03. When a message is received, the starting character, message identifier, and ending character are stripped from the message which is then passed to whatever part of the application it is intended for. If you are having trouble with receiving and sending data, make sure that you are using a crossover cable which has the send and receive wires switched at one end of the cable. If you use a straight through cable then the receiving computer will not be getting the data on the receive pin but the transmit pin, which it won't process. By definition, serial communication means that you cannot get two messages at the same time as you are transmitting the bytes in a 'series'. So as long as you are looking for the stop character to terminate a message then the next message should not get jumbled or lost as long as you don't overrun the buffer.

                U Offline
                U Offline
                User 6440437
                wrote on last edited by
                #7

                thanks for replying me hey you said you do lot of RS232 programming. can you teach me that because I am new in RS232 programming with multithreading and i have to do it in time so I dont have enough time but I will manage if you are ready to teach me. I really appriciate for that. I am waiting for your reply. thanks.

                1 Reply Last reply
                0
                • T Tr v

                  I do a lot of RS232 programming for my job. All of our messages usually start with a special character, such as the hex 0x02 character, followed by a message identifier that we use to let the program know what to do with the message. Then the message is ASCII encoded text followed by a termination character, such as hex 0x03. When a message is received, the starting character, message identifier, and ending character are stripped from the message which is then passed to whatever part of the application it is intended for. If you are having trouble with receiving and sending data, make sure that you are using a crossover cable which has the send and receive wires switched at one end of the cable. If you use a straight through cable then the receiving computer will not be getting the data on the receive pin but the transmit pin, which it won't process. By definition, serial communication means that you cannot get two messages at the same time as you are transmitting the bytes in a 'series'. So as long as you are looking for the stop character to terminate a message then the next message should not get jumbled or lost as long as you don't overrun the buffer.

                  U Offline
                  U Offline
                  User 6440437
                  wrote on last edited by
                  #8

                  hey need help for serial port. please give me your email addres or something so i can send info about it. please. thanks

                  T 1 Reply Last reply
                  0
                  • U User 6440437

                    hey need help for serial port. please give me your email addres or something so i can send info about it. please. thanks

                    T Offline
                    T Offline
                    Tr v
                    wrote on last edited by
                    #9

                    I don't mind offering assistance on your project if you have specific questions. I understand deadlines and your issue with getting this done in a certain amount of time but for me to 'teach' you about threading and serial port communication is a considerable task. What do you know already? If you are just starting out then I suggest you read the documentation at MSDN on the Threading class and the SerialPort class or check out one of the fine articles here at CP on the subject. That's how I learned how to do a lot of the stuff you are wanting to do. If you have a specific question feel free to contact me (you can click the email link at the bottom of this post) and I will answer the best I can.

                    U 1 Reply Last reply
                    0
                    • T Tr v

                      I don't mind offering assistance on your project if you have specific questions. I understand deadlines and your issue with getting this done in a certain amount of time but for me to 'teach' you about threading and serial port communication is a considerable task. What do you know already? If you are just starting out then I suggest you read the documentation at MSDN on the Threading class and the SerialPort class or check out one of the fine articles here at CP on the subject. That's how I learned how to do a lot of the stuff you are wanting to do. If you have a specific question feel free to contact me (you can click the email link at the bottom of this post) and I will answer the best I can.

                      U Offline
                      U Offline
                      User 6440437
                      wrote on last edited by
                      #10

                      thanks for help. and I know basic info about threading and serial port. but the main problem is in my project computer has to recieve signals from device in continous manner. Its like embedded tech. so new for that. thats why confused. cant find your email link so have to put on general blog. if you dont mind give me your email id so i can send you my problem. but thanks a lot.

                      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