Piszemy rozwiązanie komunikacji przy użyciu ASCII
Standardowo ustawienia konfiguracyjne, zmienne i przerwania:
Code: [Zaznacz cały] [Rozwiń/Zwiń]
- '$sim
- 'Debug On
- Debug Off
- $regfile = "m2560def.dat"
- $crystal = 16000000
- $hwstack = 40
- $swstack = 16
- $framesize = 32
- $baud = 19200
- $projecttime = 0
- $version 0 , 0 , 0
- Config Submode = New
- Config Com1 = 19200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
- Config Com2 = 19200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
- Config Serialin0 = Buffered , Size = 100 'buforowoanie pierwszy uart
- Config Serialin1 = Buffered , Size = 100 'buforowoanie drugi uart
- Config Serialout0 = Buffered , Size = 100 'buforowoanie pierwszy uart
- Config Serialout1 = Buffered , Size = 100 'buforowoanie drugi uart
Deklarujemy zmienne dla przykładu zmienne dla uart 0 i uart 1
Code: [Zaznacz cały] [Rozwiń/Zwiń]
- Dim Ile_elementow As Byte '
- Dim Znak_ As Byte
- Dim Uart_komenda As String * 20 'musi byc wystarczjaco dlugie
- Dim Getcommands(10) As String * 10 'tablica na pociete komendy
- '*******************************************************************************
- '**********************zmienne dla uart1****************************************
- Dim Ile_elementow_1 As Byte '
- Dim Znak_1 As Byte
- Dim Uart_komenda_1 As String * 20 'musi byc wystarczjaco dlugie
- Dim Getcommands_1(10) As String * 10 'tablica na pociete komendy
- '*******************************************************************************
Standardowo zaczynamy od sprawdzania czy znak jest w buforze
przykład dla uart0:
Code: [Zaznacz cały] [Rozwiń/Zwiń]
- If Ischarwaiting() > 0 Then 'calls _gotchar and use URXC
- Znak_ = Waitkey() 'pobierz znak z bufora
- If Znak_ = 13 Or Len(uart_komenda) > 20 Then ' jeslii ENTER lub wiecej niz 20 znaków to call uartrx
- Call Uartrx '
- Print '
- Print Prompt; 'slowo zachety jako stala
- Uart_komenda = ""
- Else
- Uart_komenda = Uart_komenda + Chr(znak_) 'kluczowe budowanie bufora
- Print Chr(znak_) ; 'to jest echo zeby by³o widac co przychodzi
- End If
- End If
Teraz jeśli nasza zmienna Znak_ zawiera 13 ASCII czyli enter lub Len(uart_komenda) > 20 Then ilość odebranych znaków jest większa niż 20 to Call Uartrx
Ilość znaków musisz dobrać według swoich upodobań nie wiem jak długie będą twoje komendy.
Teraz przechodzimy do uartrx wspomniane na początku split będzie znajdować się właśnie w tym miejscu.
Tniemy nasze komendy żeby można było je rozróżnić
Ile_elementow = Split(uart_komenda , Getcommands(1) , " ")
Zmienna Ile_elementow zawiera jak sama nazwa mówi ilość pociętych elementów.
Zmienna uart_komenda to nasza źródłowy string do którego dodawaliśmy znaki podczas odbioru i pobierania z bufora Uart_komenda = Uart_komenda + Chr(znak_) 'kluczowe budowanie bufora
Zmienna Getcommands(1) to nasza tablica o rozmiarze 10 na 9 znaków, zapewne zauważyłeś po przecinku cudzysłów to właśnie znak spacji miedzy cudzysłowiem będzie naszym punktem w którym będzie następowało cięcie i umieszczanie kolejno w komórkach tablicy Getcommands(1).
Po uzyskaniu już pociętych komend reszta jest już z górki. musimy tylko zdecydować jak reagować na nasze komendy.
Code: [Zaznacz cały] [Rozwiń/Zwiń]
- Uartrx:
- Ile_elementow = Split(uart_komenda , Getcommands(1) , " ") 'tniemy na komendy
Code: [Zaznacz cały] [Rozwiń/Zwiń]
- Select Case Getcommands(1) 'miejsce w tablicy dla komendy pierwszej
- Case Komenda1
- 'cos do zrobienia
- Case Komenda2
- 'cos do zrobienia
- Case Komenda3
- 'cos do zrobienia
- Case Komenda4
- 'cos do zrobienia
- Select Case Getcommands(2) ' druga komenda po spacji
- Case Komenda5
- 'cos do zrobienia
- Case Komenda6
- 'cos do zrobienia
- Case Else
- Print Prompt ; " " ; Uart_komenda ; " " ; Notincommands ' nie rozpoznanan komenda
- Getcommands(1) = "" 'czyszczenie bufora
- Getcommands(2) = ""'czyszczenie bufora
- Getcommands(3) = ""'czyszczenie bufora
- End Select
- Case Help
- 'cos do zrobienia
- Case Reboot
- Print 'odstep na ekranie
- Print " Wait rebooting "
- Start Watchdog
- Case Else ' nie rozpoznanan komenda
- Print Prompt ; " " ; Uart_komenda ; " " ; Notincommands
- Print Prompt
- Getcommands(1) = "" 'czysczenie bufora
- Getcommands(2) = "" 'czysczenie bufora
- Getcommands(3) = "" 'czysczenie bufora
- End Select
- Getcommands(1) = "" 'czysczenie bufora
- Getcommands(2) = "" 'czysczenie bufora
- Getcommands(3) = "" 'czysczenie bufora
- Return
- GeSHi
Wracamy na początek naszego programu i dodajemy stałe:
Code: [Zaznacz cały] [Rozwiń/Zwiń]
- Const Change = "change"
- Const Prompt = "wpisz jakies zwierze "
- Const Komenda1 = "pies"
- Const Komenda2 = "kot"
- Const Komenda3 = "krowa"
- Const Komenda4 = "kon"
- Const Komenda5 = "paszcza"
- Const Komenda6 = "kopyta"
- Const Help = "?"
- Const Reboot = "reboot"
- Const Notincommands = "Cos poszlo nie tak spróbuj jeszcze raz "
Code: [Zaznacz cały] [Rozwiń/Zwiń]
- '(
- 'Set Attribute Mode <ESC>[{attr1};...;{attrn}m
- 'Sets multiple display attribute settings. The following lists standard attributes:
- '0 Reset all attributes
- '1 Bright
- '2 Dim
- '4 Underscore
- '5 Blink
- '7 Reverse
- '8 Hidden
- 'Foreground Colours
- '30 Black
- '31 Red
- '32 Green
- '33 Yellow
- '34 Blue
- '35 Magenta
- '36 Cyan
- '37 White
- 'Background Colours
- '40 Black
- '41 Red
- '42 Green
- '43 Yellow
- '44 Blue
- '45 Magenta
- '46 Cyan
- '47 White
- Print Chr(27) ; "[1;31;47m"; ' set atributte
- PRINT "Your name ?>"; ' text
- Print Chr(27) ; "[0m"; ' reset all
- ')
- GeSHi
Code: [Zaznacz cały] [Rozwiń/Zwiń]
- '$sim
- Debug On
- Debug Off
- $regfile = "m2560def.dat"
- $crystal = 16000000
- $hwstack = 40
- $swstack = 16
- $framesize = 32
- $baud = 19200
- $projecttime = 65
- $version 0 , 0 , 41
- Config Submode = New
- Config Com1 = 19200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
- Config Com2 = 19200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
- Config Serialin0 = Buffered , Size = 100 'buforowoanie pierwszy uart
- Config Serialin1 = Buffered , Size = 100 'buforowoanie drugi uart
- Config Serialout0 = Buffered , Size = 100 'buforowoanie pierwszy uart
- Config Serialout1 = Buffered , Size = 100 'buforowoanie drugi uart
- '**********************zmienne dla uart0****************************************
- Dim Ile_elementow As Byte '
- Dim Znak_ As Byte
- Dim Uart_komenda As String * 20 'musi byc wystarczjaco dlugie
- Dim Getcommands(10) As String * 10 'tablica na pociete komendy
- '*******************************************************************************
- '**********************zmienne dla uart1****************************************
- Dim Ile_elementow_1 As Byte '
- Dim Znak_1 As Byte
- Dim Uart_komenda_1 As String * 20 'musi byc wystarczjaco dlugie
- Dim Getcommands_1(10) As String * 10 'tablica na pociete komendy
- '*******************************************************************************
- '(
- 'Set Attribute Mode <ESC>[{attr1};...;{attrn}m
- 'Sets multiple display attribute settings. The following lists standard attributes:
- '0 Reset all attributes
- '1 Bright
- '2 Dim
- '4 Underscore
- '5 Blink
- '7 Reverse
- '8 Hidden
- 'Foreground Colours
- '30 Black
- '31 Red
- '32 Green
- '33 Yellow
- '34 Blue
- '35 Magenta
- '36 Cyan
- '37 White
- 'Background Colours
- '40 Black
- '41 Red
- '42 Green
- '43 Yellow
- '44 Blue
- '45 Magenta
- '46 Cyan
- '47 White
- Print Chr(27) ; "[1;31;47m"; ' set atributte
- PRINT "Your name ?>"; ' text
- Print Chr(27) ; "[0m"; ' reset all
- ')
- '*******************************************************************************
- '*******************************************************************************
- Const On = 1
- Const Off = 0
- Const Change = "change"
- Const Prompt = "wpisz jakies zwierze "
- Const Komenda1 = "pies"
- Const Komenda2 = "kot"
- Const Komenda3 = "krowa"
- Const Komenda4 = "kon"
- Const Komenda5 = "paszcza"
- Const Komenda6 = "kopyta"
- Const Help = "?"
- Const Reboot = "reboot"
- Const Notincommands = "Cos poszlo nie tak spróbuj jeszcze raz "
- Declare Sub Uartrx()
- Declare Sub Uartrx_1()
- Enable Interrupts
- Config Watchdog = 4096
- Enable Serial
- Enable Serial1
- Enable Serial2
- Print Version(2)
- Print Chr(27) ; "[1;31;47m"; ' set atributte
- Print "BY PIKCZU"; ' text
- Print Chr(27) ; "[0m"; ' reset all
- Print
- Print Prompt
- Print 'nowa linia
- Wait 1
- '*******************************************************************************
- Open "com2:" For Binary As #1
- Do
- '*****************************Uart0*********************************************
- If Ischarwaiting() > 0 Then 'calls _gotchar and use URXC
- Znak_ = Waitkey() 'pobierz znka z bufora
- If Znak_ = 13 Or Len(uart_komenda) > 20 Then ' jelsi ENTER lub wiecej niz 20 znaków to call uartrx
- Call Uartrx '
- Print 'puste linia zeby ³adniej wyglada³o na ekranie '
- Print Prompt; 'slowo zachety jako stala
- Uart_komenda = ""
- Else
- Uart_komenda = Uart_komenda + Chr(znak_) 'kluczowe budowanie bufora
- Print Chr(znak_) ; 'to jest echo zeby by³o widac co przychodzi
- End If
- End If
- '*****************************Uart1*********************************************
- While Ischarwaiting(#1) > 0 'calls _gotchar and use URXC
- Znak_1 = Waitkey(#1) 'get char from buffer
- If Znak_1 = 13 Or Len(uart_komenda_1) > 10 Then ' check for ENTER of more then 4 characters
- Call Uartrx_1 '
- 'Print '
- 'Print Prompt; 'Using a constant will let us change the prompt
- Uart_komenda_1 = ""
- Else
- Uart_komenda_1 = Uart_komenda_1 + Chr(znak_1) 'buffor building
- 'Print Chr(Znak_1); 'this is echo
- End If
- Wend
- '*******************************************************************************
- Loop
- Close #1
- End
- Uartrx:
- Ile_elementow_1 = Split(uart_komenda , Getcommands(1) , " ") 'This choping coomand after space
- Debug Chr(13)
- Debug "lcount= " ; Lcount
- Debug "uart_command= " ; Uart_komenda
- Debug "Getcommands(1)= " ; Getcommands(1)
- Debug "Getcommands(2)= " ; Getcommands(2)
- Debug "Getcommands(3)= " ; Getcommands(3)
- Print 'to dla wyœwietlania poprawnych odstepow
- Select Case Getcommands(1) 'miesce w tablicy dla komendy pierwszej
- Case Komenda1
- Print ' "pies"
- Print "hal hal hal"
- Case Komenda2
- Print '"kot"
- Print "mial mial"
- Case Komenda3
- Print '"krowa"
- Print "muuu"
- Case Komenda4
- Print '"kon"
- Select Case Getcommands(2)
- Case Komenda5
- Print '"paszcza"
- Print "ihhhaaa"
- Case Komenda6
- Print '"kopyta"
- Print "patataj pataj pataj"
- Case Else
- Print "jestes pewien?" ; " " ; Uart_komenda ; " " ; "Popelniles jakis blad sprobuj jeszcze raz"
- Getcommands(1) = ""
- Getcommands(2) = ""
- Getcommands(3) = ""
- End Select
- Case Help
- Print '?
- Print "pies"
- Print "kot"
- Print "krowa"
- Print "kon"
- Print "paszcza"
- Print "kopyta"
- Print "?"
- Print "reboot"
- Case Reboot
- Print 'odstep na ekranie
- Print " Wait rebooting "
- Start Watchdog
- Case Else ' nie rozpoznanan komenda
- Print Prompt ; " " ; Uart_komenda ; " " ; Notincommands
- Print Prompt
- Getcommands(1) = "" 'czysczenie bufora
- Getcommands(2) = "" 'czysczenie bufora
- Getcommands(3) = "" 'czysczenie bufora
- End Select
- Getcommands(1) = "" 'czysczenie bufora
- Getcommands(2) = "" 'czysczenie bufora
- Getcommands(3) = "" 'czysczenie bufora
- Return
- Uartrx_1:
- Ile_elementow_1 = Split(uart_komenda_1 , Getcommands_1(1) , " ") 'This choping coomand after space
- Debug Chr(13)
- Debug "lcount#1= " ; Lcount_1
- Debug "Uart_komenda#1= " ; Uart_komenda_1
- Debug "Getcommands#1(1)= " ; Getcommands_1(1)
- Debug "Getcommands#1(2)= " ; Getcommands_1(2)
- Debug "Getcommands#1(3)= " ; Getcommands_1(3)
- Print " "; 'to dla wyœwietlania poprawnych odstepow
- Select Case Getcommands(1) 'miesce w tablicy dla komendy pierwszej
- Case Komenda1
- Print ' "pies"
- Print "hal hal hal"
- Case Komenda2
- Print '"kot"
- Print "mial mial"
- Case Komenda3
- Print '"krowa"
- Print "muuu"
- Case Komenda4
- Print '"kon"
- Select Case Getcommands(2)
- Case Komenda5
- Print '"paszcza"
- Print "ihhhaaa"
- Case Komenda6
- Print '"kopyta"
- Print "patataj pataj pataj"
- Case Else
- Print "jestes pewien?" ; " " ; Uart_komenda ; " " ; "Popelniles jakis blad sprobuj jescze raz"
- Getcommands(1) = ""
- Getcommands(2) = ""
- Getcommands(3) = ""
- End Select
- Case Help
- Print '?
- Print "pies"
- Print "kot"
- Print "krowa"
- Print "kon"
- Print "paszcza"
- Print "kopyta"
- Print "?"
- Print "reboot"
- Case Reboot
- Print 'odstep na ekranie
- Print " Wait rebooting "
- Start Watchdog
- Case Else ' nie rozpoznanan komenda
- Print Prompt ; " " ; Uart_komenda ; " " ; Notincommands
- Print Prompt
- Getcommands(1) = "" 'czysczenie bufora
- Getcommands(2) = "" 'czysczenie bufora
- Getcommands(3) = "" 'czysczenie bufora
- End Select
- Getcommands(1) = "" 'czysczenie bufora
- Getcommands(2) = "" 'czysczenie bufora
- Getcommands(3) = "" 'czysczenie bufora
- Return
- GeSHi
Obsługa drugiego uarta jest zrobiona podobnie ale nie identycznie porównaj sam.
dla testu wpisz w terminalu "kot" sprawdź tez co robi "kon paszcza"
Pozdrawiam
Pikczu