locked
serial port - com RRS feed

  • Question

  • I toke from the VB6 the example about using the serial port with MSComm.
    I run this program in VB2005 after it was upgrade ofcourse by the VB2005
    and try to send and receive strings in a loop(1-1000).
    But sometimes in this loop the string that I'm sending is stuck or something that I don't understand why???

    I run this loop with the some strings a lot of times and everytime is a different
    string that stuck. if someone can tell me why???

    In VB2005 there is something similar to MSComm that I can use??

    Friday, October 7, 2005 9:52 PM

Answers

All replies

  • Hi,

    Visual Basic 2005 has a separate SerialPort control which you can use for serial communication.
    http://msdn2.microsoft.com/en-us/library/system.io.ports.serialport.aspx

    Sample available only in C# - http://www.gotdotnet.com/team/clr/bcl/demos/demos/zipfiles/SerialLCDCS.zip

    Regards,
    Vikram
    Saturday, October 8, 2005 5:09 AM
  • Thanks for the quick answer,

    But I'm Beginner in VB2005 programming and I be glad if I can get example
    of how to add this SerialPort control and how to use like the basic command
    as open the SerialPort,write,read and close.

    Regards,
    yaniv

    Saturday, October 8, 2005 12:00 PM
  • Hi,

    Here's a simpler example - but still in C#.

    Let me know if you can figure out, or I will convert it to VB2005 and send it to you:
    http://www.codeproject.com/csharp/SerialCommunication.asp

    Regards,
    Vikram
    Saturday, October 8, 2005 1:43 PM
  • Hi, thanks again

    But I can't figure out how can I use it in VB2005.
    I tried to drag "SERIALPORT CONTROL" from the "toolbox" to the "form1.vb(design)"
    but after that I didn't know how to apply to this serialport.

    If you can convert it to VB2005 it be great

    thanking you in advance
    yaniv




     

    Saturday, October 8, 2005 2:12 PM
  • Hi,

    You do not even drag drop the control. You can create an instance by declaring a variable of type SerialPort and use that. Anyways, I have that sample converted.

    Let me know your email Id so that I can email the project to you.

    Regards,
    Vikram
    Saturday, October 8, 2005 3:06 PM
  • I'm really appreciate it...

    my email : yaniv@turbototo.com

    thanks again,

    yaniv

    Saturday, October 8, 2005 3:09 PM
  • Hi,

    I have e-mailed the sample in VB2005 to you.

    Regards,
    Vikram
    Saturday, October 8, 2005 4:08 PM
  • Hi,

    Thanks for the files it was very helpful.

    I have a little problem that I can't figure out how can I solve it.

    I want that my program will work in a loop and check if something come from the serialport or if there a file that another program sent to me.
    something like this:

    Dim a
    Dim i as integer, flag_file as short
    i=0
    flag_file=0

    Do
      If Not (a = Dir("c:\temp.txt")) Then flag_file = 1 
       i = sp.readbyte
    loop untill (i>0 or flag_ file=1)


    but the problam is that if nothing comming from the serialport it's stuck and wait.
    I'm using readbyte and not readline cause I can get chr(0) or enter (asci 0 till asci 255) from the serailport.

    there is solution for that ?

    thanks again,
    yaniv



    Saturday, October 8, 2005 7:47 PM
  • Hi,
    I didn't notice that you use with " try " and "catch" so now it is working as I wanted.

    But now  I have another problem I tried to sent chr(129) and I received in the other computer chr(63).

    when I sent chr(0) till chr(127) it work fine.

    from chr(128) till chr(255) some of them are fine and some of them
    I'm getting chr(63)

    So how can I fix that?

    yaniv



    Sunday, October 9, 2005 2:54 PM
  • try using chrw(x) instead of chr(x)

    Dustin
    Sunday, October 9, 2005 3:04 PM
  • I tried chrw(x) it's still work the same,

    this is how I open the port: (maybe I didn't do something right?)

    sp.PortName = "COM1"

    sp.ReadTimeout = 500

    sp.DtrEnable = True

    sp.RtsEnable = True

    sp.BaudRate = 19200

    sp.DataBits = 8

    sp.StopBits = StopBits.One

    sp.Parity = Parity.None

    sp.Open()

    if you have another idea I'll be happy to try....

    Sunday, October 9, 2005 3:25 PM

  • It looks like his code is looking for eight bit enties and not 16 so I'm really sure that Chrw will not work. My understanding is that these days the chrw chr are both 16 bit entities.

    It looks as if the byte high-bit is being used as a sign bit in places.
    Sunday, October 9, 2005 7:48 PM
  • Hi

    I have graduation project that is about RS-232 serial cominication. I am developing my  code with .NET 2005.

    I want to control RS-232,

    code;

     

    private void write_click(object sender, EventArgs e)
    {
         if (!serialPort1.IsOpen)
         {
             serialPort1.Open();
         }
         string sendmesaj = textBox1.Text+"-->>>>";
         serialPort1.WriteLine(sendmesaj);
    }

    private void read_click(object sender, EventArgs e)
    {

         string i=serialPort1.ReadLine();
         textBox2.Text ="<<<<---"+i;
         serialPort1.Close();

    }

     

    when I send data that is ony between 1 and 5 characters, it is ok. However when send characters more than 6 for exmaple "abcdefghi" it is faild.

    please help me

    thanks a lot

    Thursday, February 23, 2006 11:37 AM
  • Hi,

    Look I'm still a rookie in VB2005 but I'll try to help you...

    So I guess that you are trying to connect between two computers.
    and you have problem with sending characters but, I'm thinking that the problem
    is receiving the characters on the other computer.

    You need to know how many characters (or till when?) to receive from the serial port otherwise after you sent the characters that you want,
    the receiveing computer will still wait for more data till time out will appear.

    When I wrote a similar program in VB2005 I had a problem that I sent char(13) (as you know that equal to end of line) and I receive on the other computer chr(129).

    that mean that the receiving computer didn't know that was the end of the line and wait for more data.

    So I worked differently I read the first byte that tell me how many bytes I need to receive. and then I know when to stop receiving characters.

    But then again I'm not sure that is the same problem in your program...

    Hope that I help you some how.....

    Regards,

    Yaniv

     

     

    Thursday, February 23, 2006 1:28 PM
  • hellow

     

    is it possible to mail the converted version of the serial communications also to me ?

    I have the same problem with the vb2005.

     

    many greetings , sierjacobs, ghent, belgium

    Tuesday, March 14, 2006 11:09 AM
  • Hi!
     
    I'm trying to make a project that consists in communicate an hp IPAQ hx 2700 with serial port to my pc. This Ipaq works with windows mobile 5.0.  I communicated them by making an application in vb 2005 for the pda and communicating with the hyperterminal in the pc. I'm having troubles because i can write things in the pda to the pc, but i can't write things in the pc and view them on the pda.
     
    I think that maybe i have to add a library or i have to configure some propierties, but i dont know what to do. Here's the code i used:
     

    Imports System.IO.Ports.SerialPort

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    SerialPort1.Open()

    End Sub

    Private Sub CmdEnviar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdEnviar.Click

    SerialPort1.WriteLine(TxtTX.Text)

    TxtTX.Text = ""

    End Sub

    Private Sub DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

    Dim Returnstr As String = ""

    Do

    Dim Incoming As String = SerialPort1.ReadExisting

    If Incoming Is Nothing Then

    Exit Do

    Else

    Returnstr &= Incoming & vbCrLf

    End If

    Loop

    TxtRX.Text = TxtRX.Text + Returnstr

    End Sub

    Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click

    Application.Exit()

    End Sub

    Private Sub Form1_Close()

    SerialPort1.Close()

    End Sub

    End Class

     

    The propierties i used in the serial port are:

     

    Name: SerialPort1

    Generate Member: True

    Modifiers: Friend

    Baud Rate: 9600

    Data bits: 8

    Discard Null: False

    DTR Enable: True

    Handshake: None

    Parity: None

    Parity replace: 63

    Port Name: COM 1

    ReadBufferSize: 4096

    Read Timeout: -1

    Received bytes threshold: 1

    RTS Enable: True

    Stop bits: 1

    Write buffer size: 2048

    Write timeout: -1

     

    Hope you can help me please.....

     

    Thank you VERY MUCH!!!!!

     

    Lina

    Monday, February 11, 2008 4:21 PM