Results 1 to 6 of 6

Thread: Using Serial Port to Read String

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    8

    Question Using Serial Port to Read String

    Dear All,

    I have an application in Electronics to communicate with serial port. With using VB I have succesfully read a character using
    Code:
    Serialport1.ReadByte
    or
    Code:
    Serialport1.ReadChar
    . Does anybody know how to read a whole string directly?

    Regards,




    Elektronika

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Using Serial Port to Read String

    What other members does the SerialPort class have? I can see three other methods that all start with "Read". Have you read the documentation for those to see if they are suitable for what you want to do?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    8

    Re: Using Serial Port to Read String

    Actually I -last time- use this code in a timer :

    Code:
    SerialPort1.Write("A")
    kalimatarray(d) = SerialPort1.ReadExisting
    RichTextBox1.Text = RichTextBox1.Text & g & vbNewLine
    The Electronics will send just a "HELLO" string to the PC, if it receives an "A" from the PC. This works but the vbNewLine is never there on the RichTextBox1.

    I am using the PC to receive some string of numbers, like :
    "1235 3303 2101 3030"

    First time I send them byte per byte and it is rather slow to read.

    Now I am considering making them a string and just send them in one step only. Do you have any Idea? I am new in VB so I don't understand 100% about the classes.

  4. #4
    Member
    Join Date
    May 2001
    Posts
    50

    Re: Using Serial Port to Read String

    Wouldn't the SerialPort.ReadLine method be more appropriate?

    Also does your electronics sends a newline character at the end of the string to mark the end of it?

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    8

    Re: Using Serial Port to Read String

    I will try it. . .and will post the result soon.

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    8

    Question Re: Using Serial Port to Read String

    Nope, still not working (working if I put "\nHELLO\n") and even after some time it freezes. (the electronics software is in C, and I have set everything correct there.)

    I have another idea. Does anyone knows on how the serial port knows that it has data on its buffer, so it is like polling (always check the buffer) and if something goes inside, it reads it immediately. What happens in my experiment is the data get deposited inside the buffer. (I realize it when I unplug the electronics, serialport1 still reads for some time, before actually freezes, and the longer I deposit the data, the longer I can have it read without the electronics.)

    Regards,




    Elektronika

    Code:
    SerialPort1.Write("A")
    'I think of putting some code here, to check whether the buffer already has some data
    RichTextBox1.Text = RichTextBox1.Text & SerialPort1.ReadLine & vbNewLine

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width