Results 1 to 7 of 7

Thread: sample script for RS485

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    26
    Rep Power
    16

    sample script for RS485

    Dear all,
    I already made microcontroller module that work with RS485 protocol.
    I need simple sample script in thin basic.
    here is step by step my protocol:
    --------------------------------
    opening comm
    change state of RTS pin for transmiting
    send character "H" (without LF CR)
    delay
    change state of RTS pin for receiving
    receiving data
    delay
    change state of RTS pin for transmiting
    show the receiving data
    closing comm
    end
    --------------------------------

    in VB6 I use program like:
    --------------------------------
    Private Sub Command2_Click()
    MSComm1.Output = "H"
    Call Pause(0.2)
    MSComm1.RTSEnable = True
    Call Pause(0.2)
    at = MSComm1.Input
    Call Pause(0.2)
    MSComm1.RTSEnable = False
    Text1.Text = at
    End Sub
    --------------------------------

    I hope with simple script I can develope my experiment.
    thank
    regard

    hendryawan

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,817
    Rep Power
    10
    thinBasic has serial communication module calle COMM
    Check example in \thinBasic\SampleScripts\COMM\ directory that shows how to check COM ports for modem info.

    Let me know.

    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    160

    Try this

    Try this I dont know if it will work but it should be what you described


    Uses "COMM","REGISTRY"
    Uses "Console"
    
    Dim Ports() As String
    Dim Buffer  As String
    Dim N       As Long
    Dim hComm   As DWord
    
    hcomm = COMM_FreeFile
    '
    '  Get all Serial ports and store in Ports array
    '
      If Registry_PathExists("HKEYLM", "hardware\devicemap\serialcomm") Then
        Buffer = Registry_GetAllKeys("HKEYLM", "hardware\devicemap\serialcomm")
      EndIf
      Split(Buffer,$CRLF,Ports)
      For n = 1 To UBound(Ports)
        Ports(n) = Remain$(Ports(n),"=")
      Next                       
      
    'Ready To Send (Output signal).
    '0 = Disable
    '1 = Enable
    '2 = Handshake
    '3 = Toggle
      COMM_Open("\\.\"+Ports(1) , hComm)
      If Err = 0 Then  
        COMM_Set (hComm, %COMM_BAUD, 9600)
        COMM_Set (hComm, %COMM_STOP, 0)
        COMM_Set (hComm, %COMM_PARITYTYPE, 0)
        COMM_Set (hComm, %COMM_BYTE, 8)
        COMM_Set (hComm, %COMM_RTSFLOW, 1)
        COMM_Send(hComm, "H")
        Sleep (20)                 
        COMM_Set (hComm, %COMM_RTSFLOW, 0)
        If COMM_Get(hComm, %COMM_RXQUE) Then
          COMM_Recv(hComm, COMM_Get(hComm, %COMM_RXQUE), Buffer)
        End If
        Sleep (20)                 
        COMM_Set (hComm, %COMM_RTSFLOW, 1)
        If Buffer <> "" Then Print Buffer
        COMM_Close(hComm)
      End If
      PrintL
      Print "Finished....Press any Key"
      WaitKey(10)  
    Stop
    
    PS.. Have done any work with SPI bus?
    Last edited by Michael Clease; 16-02-2011 at 18:24.
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    26
    Rep Power
    16
    I has been try to made the program with little bit strange.

    here my code:

     
    '==============================================================
    ' Empty GUI script created on 02-16-2011 16:53:01 by (ThinAIR)
    ' programming for RS485 communication
    Uses "COMM"
    Uses "CONSOLE"
    Dim hcomm As Long
    Dim sBuffer As String
    '---Get Comms file number and assign 
    hcomm = COMM_FreeFile
    '---Set up all comms parameters 
    COMM_Set( hcomm, %COMM_BAUD, 4800 )
    COMM_Set( hcomm, %COMM_BYTE, 8 )
    COMM_Set( hcomm, %COMM_NULL, FALSE )
    COMM_Set( hcomm, %COMM_PARITY, FALSE )
    COMM_Set( hcomm, %COMM_PARITYTYPE, 0 )
    COMM_Set( hcomm, %COMM_STOP, 0 )
    COMM_Set( hcomm, %COMM_RXBUFFER, 64 )
    '---Open the port
    COMM_Open( "COM1", hcomm )
    COMM_Set( hcomm, %COMM_RTSFLOW, 0 ) 'enable SN75176 to transmiting
    Sleep (10) 
    COMM_Print(hComm, "H")
    Sleep (10) 
    COMM_Set( hcomm, %COMM_RTSFLOW, 1 ) 'enable SN75176 to receiving
    Sleep (10) 
    sBuffer = COMM_Line(hComm) 'waiting rx data with CRLF to continue 
    Console_WriteLine(sBuffer) 
    COMM_Close( hcomm )
    Console_WriteLine("end testing")
    Sleep (3000)
    '==============================================================
    
    in my code I found a strange condition, the command line is :

    sBuffer = COMM_Line(hComm) 'waiting rx data with CRLF to continue

    I must add extra CRLF (two times from my microcontroller) to made program continue writing to the console.
    Why this is happen?

    regard

    hendryawan
    Last edited by ErosOlmi; 16-02-2011 at 17:31.

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,817
    Rep Power
    10
    Really do not know because I cannot test but maybe RXTX buffer is not ready yet.
    Sleep accept milliseconds so maybe 20 ms are not enough.
    Maybe, as suggested by Michael, it is better to use some like:
     
    while not COMM_Get(hComm, %COMM_RXQUE)
      '---implement some timeout period otherwise you will enter into an infinite loop
      sleep(20)
    wend
    sBuffer = COMM_Line(hComm)
    
    or any other way to be sure there is something ready to be read.

    Just guessing
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    26
    Rep Power
    16
    My circuit done with program bellow:
    .......
    COMM_Set( hcomm, %COMM_RTSFLOW, 1 ) 'enable SN75176 to receiving
    Sleep 400
    nBytes = COMM_Get(hComm, %COMM_RXQUE)
    COMM_TRecv(hComm, nBytes, sBuffer, 10)
    Console_WriteLine(sBuffer)
    .......

    thank you for all

    best regard

    hendryawan

  7. #7
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,817
    Rep Power
    10
    Great !
    Happy to see you got the way.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Similar Threads

  1. new sample script is fun and neat
    By kryton9 in forum TBGL General
    Replies: 3
    Last Post: 12-03-2007, 21:47

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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