Results 1 to 14 of 14

Thread: RS232 Communication

  1. #1
    Guest

    Lightbulb

    Hi, I want to create an application that reads from a RS232 port (COM) and print the data received on the screen. I have an RF module connected to the RS232 so the data is an analog waveform.
    I want to be able to read and transmit data through the COM port. I do not have much experience with comm. ports.
    Any help or source where I can look into is appreciated.
    I'm working on a Robotics project.

    Thanks,
    Diego.-

    Using VB6 (SP3)

  2. #2
    Addicted Member
    Join Date
    Oct 1999
    Location
    Dallas,TX
    Posts
    170

    ...

    Well, an analog waveform is not digital. RS232 and all other types of PC communication ARE digital. Your analog waveform MUST be converted to Binary\Digital. An easy way to do this is using an ADC(Analog to Digital converter) to convert your waveform to digital, then to use a microcontroller to grab the data and send it via RS232 to the PC.


    Hope that clears it up!
    Phil

  3. #3
    Guest
    You are perfectly correct. Sorry for the misunderstanding. That is not really the problem, I can do that very easily and have 8-bit packets which is enough for the kind of communication that I'm doing.
    My problem is how to read the data from the port using VB6.
    Once I can read the data I can device a simple protocol to communicate with the device.

    Thanks,
    Diego

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Smile

    Hope this sample code can help you much.

    Code:
    'Main Form >> frmMain.frm
    Option Explicit
    Dim ZZ$
    Private Sub CmdAction_Click(Index As Integer)
    Select Case Index
    Case 0 'Send
        MSComm1.InBufferCount = 0
        MSComm1.Output = txtSend.Text
        txtRx.Text = txtRx.Text & "<Tx> " & txtSend.Text & vbCrLf
        txtRx.SelStart = Len(txtRx)
    Case 1 'Setting
        Load frmSetting
        frmSetting.Show
    End Select
    End Sub
    
    Private Sub Form_Load()
    SaveSetting "xCOM", "Parameter", "ComPort", MSComm1.CommPort
    SaveSetting "xCOM", "Parameter", "Settings", MSComm1.Settings
    End Sub
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If MSComm1.PortOpen Then MSComm1.PortOpen = False
    Dim xForm As Form
    For Each xForm In Forms
        Unload xForm
    Next
    End Sub
    
    Private Sub MSComm1_OnComm()
    Select Case MSComm1.CommEvent
    Case comEvReceive
        txtRx.Text = txtRx.Text & "<Rx> " & MSComm1.Input & vbCrLf
        txtRx.SelStart = Len(txtRx)
    End Select
    
    End Sub
    
    'Port Configuration form >> frmSetting.frm
    Option Explicit
    Private xComSetting(3) As String
    Private Sub CmdAction_Click()
    On Error GoTo Open_Err
    SaveSetting "xCOM", "Parameter", "ComPort", Mid(Trim(Combo1.Text), 4, 1)
    SaveSetting "xCOM", "Parameter", "Settings", Trim(Combo2.Text) & Chr(44) & Trim(Combo3.Text) & Chr(44) & Trim(Combo4.Text) & Chr(44) & Trim(Combo5.Text)
    
    frmMain.MSComm1.CommPort = CInt(GetSetting("xCOM", "Parameter", "ComPort", 1))
    frmMain.MSComm1.Settings = GetSetting("xCOM", "Parameter", "Settings", "9600,n,8,1")
    frmMain.MSComm1.PortOpen = True
    frmMain.CmdAction(0).Enabled = True
    frmMain.Shape1.FillColor = vbGreen
    Unload Me
    
    Exit Sub
    Open_Err:
    MsgBox "(" & Err.Number & ") " & Err.Description, vbExclamation + vbOKOnly, "RS232"
    frmMain.Shape1.FillColor = vbRed
    If frmMain.MSComm1.PortOpen Then frmMain.MSComm1.PortOpen = False
    End Sub
    
    Private Sub Form_Load()
    Dim xArray, xParse
    Dim xCnt%
    If frmMain.MSComm1.PortOpen Then frmMain.MSComm1.PortOpen = False
    frmMain.Shape1.FillColor = vbRed
    frmMain.CmdAction(0).Enabled = False
    xCnt = 0
    Combo1.ListIndex = CInt(GetSetting("xCOM", "Parameter", "ComPort", 1)) - 1
    xArray = Split(GetSetting("xCOM", "Parameter", "Settings", "9600,n,8,1"), Chr(44), -1)
    For Each xParse In xArray
        xComSetting(xCnt) = xParse
        xCnt = xCnt + 1
    Next
    Select Case xComSetting(0)
    Case "1200"
        xCnt = 0
    Case "2400"
        xCnt = 1
    Case "4800"
        xCnt = 2
    Case "9600"
        xCnt = 3
    Case "14400"
        xCnt = 4
    Case "28800"
        xCnt = 5
    Case "33600"
        xCnt = 6
    Case "55600"
        xCnt = 7
    End Select
    Combo2.ListIndex = xCnt
    
    Select Case xComSetting(1)
    Case "n"
        xCnt = 0
    Case "e"
        xCnt = 1
    Case "o"
        xCnt = 2
    Case "m"
        xCnt = 3
    Case "s"
        xCnt = 4
    End Select
    Combo3.ListIndex = xCnt
    
    Select Case xComSetting(2)
    Case "6"
        xCnt = 0
    Case "7"
        xCnt = 1
    Case "8"
        xCnt = 2
    End Select
    Combo4.ListIndex = xCnt
    
    Select Case xComSetting(3)
    Case "1"
        xCnt = 0
    Case "1.5"
        xCnt = 1
    Case "2"
        xCnt = 2
    End Select
    Combo5.ListIndex = xCnt
    
    End Sub

  5. #5
    Addicted Member c@lle's Avatar
    Join Date
    Oct 1999
    Location
    Belgium
    Posts
    179

    to chris

    Where can i find the 'MSComm1' object.
    If I put this code into a form, the compiler can't find 'MSComm1'???
    Thanks.

  6. #6
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Originally posted by c@lle
    Where can i find the 'MSComm1' object.
    If I put this code into a form, the compiler can't find 'MSComm1'???
    Thanks.
    If you paste the code into the form with (Name) = frmMain, then the MSComm1 control should pun in the frmMain form.


  7. #7
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    In case you can't find it:
    Project --> Components --> Microsoft Comm Control

  8. #8
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Question

    This control should ready after you install the Visual Basic 6.0 and it store at tehe following location:

    C:\Windows\system\MSCOMM32.OCX or
    C:\WinNT\system32\MSCOMM32.OCX



  9. #9
    choonchin
    Guest
    Hai Chris, i need to send a text file listed in the file list box of my form thrugh the serial port to a external device that contain a Motorola 8051 microcontroller. Can i use the code u post here. Or do u got any code for it???.Thanks.

  10. #10
    Member
    Join Date
    Jun 2001
    Location
    malaysia
    Posts
    44

    just say hello!

    haha!! choon chin above me is my coursemate. we r taking 2 different projects but using same UART 8051 microp in final part.
    chris: thx for ur code.
    diego01: u r so lucky!! got so many replies. unlike me !!

    well, hope this can help u too!! ( but it is using modem )
    Attached Files Attached Files
    give me some ideas!!!!!!

  11. #11
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Hi, choonchin, basically you can ignore the email that i sent to you. which i have misunderstand your question. To setup a serial communication between your app & the 8051 uController, all you need is to ensure both side is having the samebaud rate, start, data bit & parity settings and then juz open the serial port with MSComm control.

    For sample code, you can get it from my home page too.

    regards,

  12. #12
    choonchin
    Guest
    Thanks to my dear coursemate, hsye for ur support. Try the code but i still don't get what i want.
    Chris : can i have ur home page address please?.Thanks You.
    All The Best.

    Regards.
    choonchin

  13. #13
    Member
    Join Date
    Jun 2001
    Location
    malaysia
    Posts
    44
    Chris:

    u posted:

    "setup a serial communication between your app & the 8051 uController, all you need is to ensure both side is having the samebaud rate, start, data bit & parity settings and then juz open the serial port with MSComm control. "

    but how can we know when the 8051 or other UART is available, or power on at the time?! (as far as i know, 8051 didn't have the RTSenable func. ). b'cos if not, then what we send will not be received, isn't that?!

    n about ur sample codes, i did download it n then got few places that not quite understand.
    in mscomm, what i understand is the program just copy text from txtSend.txt to txtRs.txt only. i tried to delect the procdata loop n it still ca display teh text, same as original. so, can u plis kindly explain it to me?!
    n also the 'commport' file, i can't open my port for all the settings ( Com 1 to Com 4 ), n again, i don't know why?

    ur kindly explanations really can help me much. as u can c, i'm doing same topic but no one ( i luv sastraxi ) help me!!!!
    thx!!!!!!!!!!!!!!!!
    ( i did try to send u thru ur homepage, but it loads too slow. so, i'm not sure about it! )
    give me some ideas!!!!!!

  14. #14
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    When you click the send button, the following code will be executed:

    VB Code:
    1. MSComm1.InBufferCount = 0
    2.         MSComm1.Output = txtSend.Text
    3.         txtRx.Text = txtRx.Text & "<Tx> " & txtSend.Text & vbCrLf
    4.         txtRx.SelStart = Len(txtRx)

    yup, i did copy the txtSend.Text to txtRx.Text, i do this just wanna to display what i have sent out from this MSComm control.

    As for the Procdata routine, it will be fire by the comEvReceive event on the MSComm control:

    VB Code:
    1. Private Sub MSComm1_OnComm()
    2.     Select Case MSComm1.CommEvent
    3.     Case comEvReceive
    4.         Buf = MSComm1.Input
    5.         '//Parse the input data
    6.         ProcData
    7.     End Select
    8. End Sub

    I do this is, so that there will be not much routine under this event. again inside the Procdata routine, it also can scna the input buffer size and continue download the data from the input buffer and continue to process the received data. For more efficient, you can make th Procdata as a thread. Well this should be the way to handle heavy data flow through the MSComm control.

    As for you fail to open the com 1 to 4, i need to info from you before i can give some comment on it

    regards

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