View Full Version : sample script for RS485
hendryawan
15-02-2011, 18:27
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
ErosOlmi
15-02-2011, 19:19
thinBasic has serial communication module calle COMM (http://www.thinbasic.com/public/products/thinBasic/help/html/comm.htm)
Check example in \thinBasic\SampleScripts\COMM\ directory that shows how to check COM ports for modem info.
Let me know.
Eros
Michael Clease
16-02-2011, 03:07
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?
hendryawan
16-02-2011, 15:43
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
ErosOlmi
16-02-2011, 17:35
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
hendryawan
18-02-2011, 16:37
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
ErosOlmi
18-02-2011, 20:20
Great !
Happy to see you got the way.