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. Visual Basic
  4. Serial Communication, vb.net

Serial Communication, vb.net

Scheduled Pinned Locked Moved Visual Basic
questioncsharpcomhelp
7 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.
  • S Offline
    S Offline
    Subjugate
    wrote on last edited by
    #1

    I have a problem here with receiving data from the serial COM port. The i received is not in full, example if i suppose to received "password" but u only display pass. I know it is something wrong with my buffer but in my code i did not specified my buffer. have anyone encounter similar problem?? How can i set the buffer size of my data received?

    L 1 Reply Last reply
    0
    • S Subjugate

      I have a problem here with receiving data from the serial COM port. The i received is not in full, example if i suppose to received "password" but u only display pass. I know it is something wrong with my buffer but in my code i did not specified my buffer. have anyone encounter similar problem?? How can i set the buffer size of my data received?

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

      Hi, there are latencies (time delays) everywhere: in the target device, in the driver, in intermediate buffers. As a result you typically can't expect to get a DataReceived event and Read() an entire message; instead you may well read half a message, or even more than one message. There are two easy situations: 1. if messages are far apart (or you have to issue a command before a single message can come), then it suffices to incorporate a delay in between the DataReceived event and the Read() operation. The delay needs to exceed the normal transmission time for the given message length (This method creates a bandwidth limitation). 2. if data is text and each message/line ends on a fixed delimiter (see NewLine property), you can forgo the DataReceived event; instead use a separate thread that calls ReadLine() to get an entire line of text. In all other cases, you must organize your software yourself such that it knows how to collect bytes and extract messages from them. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


      S 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, there are latencies (time delays) everywhere: in the target device, in the driver, in intermediate buffers. As a result you typically can't expect to get a DataReceived event and Read() an entire message; instead you may well read half a message, or even more than one message. There are two easy situations: 1. if messages are far apart (or you have to issue a command before a single message can come), then it suffices to incorporate a delay in between the DataReceived event and the Read() operation. The delay needs to exceed the normal transmission time for the given message length (This method creates a bandwidth limitation). 2. if data is text and each message/line ends on a fixed delimiter (see NewLine property), you can forgo the DataReceived event; instead use a separate thread that calls ReadLine() to get an entire line of text. In all other cases, you must organize your software yourself such that it knows how to collect bytes and extract messages from them. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


        S Offline
        S Offline
        Subjugate
        wrote on last edited by
        #3

        Actually i not sure if the complete output is shown. As i can only see part of it and the other part of it is missing. My buffer is 15 letters and it should be more den enough to fill the output. So if between the data received and read i do a sleep, will it works?

        L 1 Reply Last reply
        0
        • S Subjugate

          Actually i not sure if the complete output is shown. As i can only see part of it and the other part of it is missing. My buffer is 15 letters and it should be more den enough to fill the output. So if between the data received and read i do a sleep, will it works?

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

          Subjugate wrote:

          will it works?

          with a larger buffer and more delay, it may or may not receive more data. If it is binary data, make sure to use a binary Read, not a text-oriented ReadLine which stops at the first CR, LF, NULL. If it is text data, you might try and receive the data with a terminal emulator (e.g. HyperTerminal) so you can see what gets received. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


          S 1 Reply Last reply
          0
          • L Luc Pattyn

            Subjugate wrote:

            will it works?

            with a larger buffer and more delay, it may or may not receive more data. If it is binary data, make sure to use a binary Read, not a text-oriented ReadLine which stops at the first CR, LF, NULL. If it is text data, you might try and receive the data with a terminal emulator (e.g. HyperTerminal) so you can see what gets received. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


            S Offline
            S Offline
            Subjugate
            wrote on last edited by
            #5

            i already tried using hyper terminal before using my application. i know what is the result i am expecting but somehow when i used my application, the result i got is not complete. I tried changing some of the code and i test it again and it works and shown the complete result but when i test it the second time rd it fails again. I will show u might data received code below and see if u can help to see whether there is anything wrong with it. If port.bytesToRead > 0 then checkforillegalcrossthreadcalls = false Dimbytetoread as integer bytetoread = port.bytestoread Dim array as byte() = new byte(bytetoread - 1)() Dim linedata as string port.read(bytearray, 0, bytetoread) linedata = converttostring(bytearray) txtcommand.text = linedata port.close()

            L 1 Reply Last reply
            0
            • S Subjugate

              i already tried using hyper terminal before using my application. i know what is the result i am expecting but somehow when i used my application, the result i got is not complete. I tried changing some of the code and i test it again and it works and shown the complete result but when i test it the second time rd it fails again. I will show u might data received code below and see if u can help to see whether there is anything wrong with it. If port.bytesToRead > 0 then checkforillegalcrossthreadcalls = false Dimbytetoread as integer bytetoread = port.bytestoread Dim array as byte() = new byte(bytetoread - 1)() Dim linedata as string port.read(bytearray, 0, bytetoread) linedata = converttostring(bytearray) txtcommand.text = linedata port.close()

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

              Hi, your code is no good; it probably would not compile, it certainly will not work

              If port.bytesToRead > 0 then
              checkforillegalcrossthreadcalls = false <<<<<<< never do this, it is BAD
              Dimbytetoread as integer

              bytetoread = port.bytestoread
              Dim array as byte() = new byte(bytetoread - 1)()
              Dim linedata as string
              port.read(bytearray, 0, bytetoread) <<<<<<< bytearray or array???
              linedata = converttostring(bytearray) <<<<<<< use Encoding.ASCII.GetString

              txtcommand.text = linedata <<<<<<< use txtcommand.InvokeRequired pattern
              port.close()

              :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


              S 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, your code is no good; it probably would not compile, it certainly will not work

                If port.bytesToRead > 0 then
                checkforillegalcrossthreadcalls = false <<<<<<< never do this, it is BAD
                Dimbytetoread as integer

                bytetoread = port.bytestoread
                Dim array as byte() = new byte(bytetoread - 1)()
                Dim linedata as string
                port.read(bytearray, 0, bytetoread) <<<<<<< bytearray or array???
                linedata = converttostring(bytearray) <<<<<<< use Encoding.ASCII.GetString

                txtcommand.text = linedata <<<<<<< use txtcommand.InvokeRequired pattern
                port.close()

                :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                S Offline
                S Offline
                Subjugate
                wrote on last edited by
                #7

                Thanks for all the comment. i know that it is bad to use checkforillegalcrossthreadcalls = false but this is really the last resort as i still cant understand multi-threading. Next, port.read(bytearray, 0, bytetoread), it is bytearray and a few line above should be Dim bytearray as byte.. Sorry for the typo error. Noted for the encoding.ASCII.getstring. thanks for the advise. For the 2nd last line i don't get u.. Care to explain??

                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