none
VB.net sending and receiving data to or from PIC through RS-232

    Question

  • Good Day,

    Currently I had write a simple VB code, my aim is when i click "Forward" button then the vb will send a character "A" to PIC, "Reverse" button will send "B" so I write my vb code for sending as below:

    Dim Forward As Char = "A"

    SerialPort1.Open()
    SerialPort1.Write(Forward)

     

    For the receiving part, after I received the data from PIC then I will show it at the label which act it as a screen. the receiving code as below:

    SerialPort1.Open()
            receive = SerialPort1.ReadChar()
           
        
            If receive = "A" Then
                Label4.Text = "FORWARD"
            ElseIf receive = "B" Then
                Label4.Text = "REVERSE"
          
            End If
            SerialPort1.Close()

     

    That is my code. Can anybody please tell me either the code is correct or not? The project is 2 people in a group so the PIC part is another group mate will be doing but she had stuck so I need to confirm if my VB code is correct then I will go to help her on her PIC due to I don't know PIC at all so need time to figure out. Thank for your helping.

    Monday, November 14, 2011 1:46 PM

Answers

  • Unless you have a good reason, do not open and close the port continuously.  Open it at the start of the program and close it when you are done.

     

    The write looks OK, but I would change the ReadChar to ReadExisting.

     

    I have a PIC micro controller board that I send and receive bytes to all the time.


    Serial Port      Random      Microsoft® Community Contributor 2011
    Monday, November 14, 2011 1:55 PM
  • Remember that all of this is binary.  The string "A" is represented, in decimal as 65, and in binary as 01000001.  So

            'this
            SerialPort1.Write("A")

            'and this
            Dim foo() As Byte = New Byte() {65}
            SerialPort1.Write(foo, 0, 1)

    are equivalent.  I would suggest that you use bytes and that the PIC programmer do the same.  My guess is that the PIC programmer is writing in C and the problem? with strings in C is that they are terminated with a null char (0). 

    That means that when the "A" is received as a string it is stored in memory in the PIC as

    65 0

     

    When I first started with the PIC I quickly switched from strings to byte arrays.  Like I said previously, the first thing to do is to send 1 byte to the PIC and receive it back from the PIC.

     

    Here is a list of all of the codes for ASCII

    http://www.asciitable.com/


    Serial Port      Random      Microsoft® Community Contributor 2011

    Friday, November 18, 2011 2:53 PM
  • You can't write "A" to the serial port because the port only understands binary.  You can send the character "A" to the serial port writing routine and the system will write the corresponding binary data to the serial port, but you need to be sure that the correct binary data for a particular character is transmitted.  VB.Net works in Unicode characters,  the PIC probably works in ASCII characters.  In order to eliminate any possibility of a difference between the two, you should always work in byte vaues (65 in this case) and not in characters.

    You have indicated that the PIC expects "A", but does it also expect something else?  If your friend interpreted the process as "Send a line of data with the charater "A" in it" then that is code for "Send the character "A" followed by a carriage return (or a line feed, or both)".  That is, send the character sequence 65 13 or 65 10 or 65 13 10.  You need to get from your friend the exact details of the byte values that the PIC requires. Are you sure it's "A" and not "a"?

    How did your friend test his programming to confirm that the PIC was working correctly?  You should use the same procedure in your testing.  That may give you some clues as to why your program isn't working.

    Friday, November 18, 2011 8:39 PM

All replies

  • Unless you have a good reason, do not open and close the port continuously.  Open it at the start of the program and close it when you are done.

     

    The write looks OK, but I would change the ReadChar to ReadExisting.

     

    I have a PIC micro controller board that I send and receive bytes to all the time.


    Serial Port      Random      Microsoft® Community Contributor 2011
    Monday, November 14, 2011 1:55 PM
  • Thank dbasnett.

    Means I only need to open the port at the beginning during connecting COMPort then it will will keep on open until I give the command close? It wont affect any action even I didn't close it? Sorry for lot questions because just learned the VB in 1 month.

     

    What the difference on ReadChar with ReadExisting? So change to as below?

       receive = SerialPort1.ReadExisting()
           
        
            If receive = "A" Then
                Label4.Text = "FORWARD"
            ElseIf receive = "B" Then
                Label4.Text = "REVERSE"
          
            End If

    Monday, November 14, 2011 2:03 PM
  • Monday, November 14, 2011 8:13 PM
  •  

    I had tried to connect the PC with the PIC, it's successfully connect but while sending data from vb, the PIC didn't have any respond. Below is my full code:

    Imports System
    Imports System.IO
    Imports System.Text
    Imports Microsoft.VisualBasic.Strings


    Public Class Form1

        Dim Up As Char = "A"
        Dim Down As Char = "B"
        Dim Forward As Char = "C"
        Dim Reverse As Char = "D"
        Dim Leftt As Char = "E"
        Dim Rightt As Char = "F"
        Dim receive As String

        Private Sub Con_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Con.Click

            SerialPort.PortName = ComboBox1.Text
            SerialPort.BaudRate = 9600

            Try

                SerialPort.Open()
                Label4.Text = "CONNECTED"

            Catch ex As Exception

                Label4.Text = "ERROR!!!"
                MsgBox(ComboBox1.Text & " Connection Failed. Please Try again.", vbExclamation, "Message")

            End Try

            If Label4.Text = "CONNECTED" Then
                Con.Enabled = False
                Discon.Enabled = True
                HA.Enabled = True
                WC.Enabled = True
                Timer1.Enabled = True
                Timer2.Enabled = True
            End If

        End Sub

        Private Sub Discon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Discon.Click

            SerialPort.Close()
            Label4.Text = "DISCONNECTED"
            Con.Enabled = True
            Discon.Enabled = False
            HA.Enabled = False
            WC.Enabled = False
            Timer1.Enabled = False
            Timer2.Enabled = False

        End Sub

        Private Sub SP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SP.Click

            FD.Checked = False
            RE.Checked = False
            Timer1.Stop()

        End Sub

        Private Sub FD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FD.CheckedChanged

            RE.Checked = False
            Timer1.Start()
            SerialPort.Write(Forward)

        End Sub

        Private Sub RE_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RE.CheckedChanged

            FD.Checked = False
            Timer1.Start()
            SerialPort.Write(Reverse)

        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

            If FD.Checked = True Then
                SerialPort.Write(Forward)
            ElseIf RE.Checked = True Then
                SerialPort.Write(Reverse)
            End If

        End Sub

        Private Sub UU_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UU.CheckedChanged

            DN.Checked = False
            Timer2.Start()
            SerialPort.Write(Up)

        End Sub

        Private Sub DN_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DN.CheckedChanged

            UU.Checked = False
            Timer2.Start()
            SerialPort.Write(Down)

        End Sub

        Private Sub LT_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LT.CheckedChanged

            RT.Checked = False
            Timer2.Start()
            SerialPort.Write(Leftt)

        End Sub

        Private Sub RT_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RT.CheckedChanged

            LT.Checked = False
            Timer2.Start()
            SerialPort.Write(Rightt)

        End Sub

        Private Sub SP1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SP1.Click

            UU.Checked = False
            DN.Checked = False

        End Sub

        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

            If RT.Checked = True Then
                SerialPort.Write(Rightt)
            ElseIf LT.Checked = True Then
                SerialPort.Write(Leftt)
            ElseIf UU.Checked = True Then
                SerialPort.Write(Up)
            ElseIf DN.Checked = True Then
                SerialPort.Write(Down)
            End If

        End Sub

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

            receive = SerialPort.ReadExisting()

            If receive = "A" Then
                Label4.Text = "UP"
            ElseIf receive = "B" Then
                Label4.Text = "DOWN"
            ElseIf receive = "C" Then
                Label4.Text = "FORWARD"
            ElseIf receive = "D" Then
                Label4.Text = "REVERSE"
            ElseIf receive = "E" Then
                Label4.Text = "LEFT"
            ElseIf receive = "F" Then
                Label4.Text = "RIGHT"
            End If

        End Sub
    End Class

    By right I press button "UP" there should be sent a character "A" to PIC, and for the PIC part already set while receive "A" then the portD7 will active. But there is no any respond on the port, can anyone help me please? I am thinking will the RS232 cable having problem? Due to bought the cheap cable without driver some more. but don't know how to check. Please help.... Thank a lot...

    Friday, November 18, 2011 2:55 AM
  • A cheap cable shouldn't be a problem, but it depends on the protocol required by the PIC.  Does it need any of the RS232 control signals to be set?  You may need DTR or RTS set in order to get the PIC to respond.

    It is strongly recommended that you also set the parity, data bits and stop bits to what the PIC expects before opening the port.

    Not all error conditions will throw an exception on Open.  You should always check the IsOpen property of the serial port to confirm that the Open method succeeded.

    Are you quite sure the PIC is programmed correctly to respond to a these Unicode characters, and that a terminator such Line Feed or Carriage Return (or both) is not also required?

    Also, it is more likely that you should be sending bytes not characters - for instance a byte value of 65 instead of character "A".

    Friday, November 18, 2011 4:53 AM
  • A cheap cable shouldn't be a problem, but it depends on the protocol required by the PIC.  Does it need any of the RS232 control signals to be set?  You may need DTR or RTS set in order to get the PIC to respond.

    It is strongly recommended that you also set the parity, data bits and stop bits to what the PIC expects before opening the port.

    Not all error conditions will throw an exception on Open.  You should always check the IsOpen property of the serial port to confirm that the Open method succeeded.

    Are you quite sure the PIC is programmed correctly to respond to a these Unicode characters, and that a terminator such Line Feed or Carriage Return (or both) is not also required?

    Also, it is more likely that you should be sending bytes not characters - for instance a byte value of 65 instead of character "A".

    Friday, November 18, 2011 4:54 AM
  • For the PIC part I not really sure it is correct programmed or not because that my friend programmed it and I only handle the VB code. But for PIC part I only just start to learn so that don't know what kind of error and actual code should need to be include.

    If not mistaken my friend didn't set any DTR or RTS. Because for the female port we only connect GND, Tx and Rx. So is it need to be set for the DTR and RTS??

    "terminator such Line Feed or Carriage Return (or both) is not also required?". This means in VB or PIC? What the meaning of it? Sorry because new in programming.

    By the way, I had just tested using the SPMC to check either my VB code successfully sent to com port, and it show the reading as what you said A=65 so it's read the 65. So is it means for the VB part for sending is ok?

    Please forgive me for asking a lot of questions and lack of experiences in the programming but I am here to learn from mistake. Thank you guys for guiding me to success..!! Cheer!!

    Friday, November 18, 2011 1:09 PM
  • I am OK at the VB .Net end of the RS232, but when I started on the PIC I crawled before I tried to run.  On the VB .Net end I wrote a simple program that sent one byte to the pic and waited for one byte to be returned.  On the PIC end I wrote code that waited for a byte to be received, and when it was it was sent back. 

     

    When that worked I modified the VB code to send a series of bytes, and wait for them to be returned.  I made changes to the PIC to accommodate the receipt of multiple bytes.

     

     


    Serial Port      Random      Microsoft® Community Contributor 2011
    Friday, November 18, 2011 2:00 PM
  • with SerialPort.Write("A") the output is 65 but not A?? Even passing the RS232?? Because 1 of my friends told me the Rs232 will automatically convert the ASCII code to character, so that even write "A" and output is 65 but go through RS232 the PIC will be receive as "A", so as what my friend told me is it true?? Please help...

    If the PIC won't receive as "A" then can you guys teach me the way that i can convert the 65 to "A"?? Thank..

    Please forgive me for asking a lot of questions and lack of experiences in the programming but I am here to learn from mistake. Thank you guys for guiding me to success..!! Cheer!!

    Friday, November 18, 2011 2:23 PM
  • Remember that all of this is binary.  The string "A" is represented, in decimal as 65, and in binary as 01000001.  So

            'this
            SerialPort1.Write("A")

            'and this
            Dim foo() As Byte = New Byte() {65}
            SerialPort1.Write(foo, 0, 1)

    are equivalent.  I would suggest that you use bytes and that the PIC programmer do the same.  My guess is that the PIC programmer is writing in C and the problem? with strings in C is that they are terminated with a null char (0). 

    That means that when the "A" is received as a string it is stored in memory in the PIC as

    65 0

     

    When I first started with the PIC I quickly switched from strings to byte arrays.  Like I said previously, the first thing to do is to send 1 byte to the PIC and receive it back from the PIC.

     

    Here is a list of all of the codes for ASCII

    http://www.asciitable.com/


    Serial Port      Random      Microsoft® Community Contributor 2011

    Friday, November 18, 2011 2:53 PM
  • You can't write "A" to the serial port because the port only understands binary.  You can send the character "A" to the serial port writing routine and the system will write the corresponding binary data to the serial port, but you need to be sure that the correct binary data for a particular character is transmitted.  VB.Net works in Unicode characters,  the PIC probably works in ASCII characters.  In order to eliminate any possibility of a difference between the two, you should always work in byte vaues (65 in this case) and not in characters.

    You have indicated that the PIC expects "A", but does it also expect something else?  If your friend interpreted the process as "Send a line of data with the charater "A" in it" then that is code for "Send the character "A" followed by a carriage return (or a line feed, or both)".  That is, send the character sequence 65 13 or 65 10 or 65 13 10.  You need to get from your friend the exact details of the byte values that the PIC requires. Are you sure it's "A" and not "a"?

    How did your friend test his programming to confirm that the PIC was working correctly?  You should use the same procedure in your testing.  That may give you some clues as to why your program isn't working.

    Friday, November 18, 2011 8:39 PM
  • thank to both of your advices, then better I choose to send as byte.

    dbasnett,

    On the VB .Net end I wrote a simple program that sent one byte to the pic and waited for one byte to be returned.

    May I know how to test this, must have PIC exist with some programming as well? Or I can test it only with VB? Because I bit confuse how to write the byte i sent and can be read back what I had sent.


    Saturday, November 19, 2011 2:59 AM
  •  thank to both of your advices, then better I choose to send as byte.

    dbasnett,

    On the VB .Net end I wrote a simple program that sent one byte to the pic and waited for one byte to be returned.

    May I know how to test this, must have PIC exist with some programming as well? Or I can test it only with VB? Because I bit confuse how to write the byte i sent and can be read back what I had sent.


     

    Both the PIC and the VB code need to start off simple.  Both ends need to start with the ability to send and receive one byte.  Didn't I say that before?


    Serial Port      Random      Microsoft® Community Contributor 2011
    Saturday, November 19, 2011 10:53 AM
  • dbasnett,

    ok now i get it.. Before that a bit blur.. Thank..

    Sunday, November 20, 2011 2:25 AM