Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Nextion reading a number or string from a object
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Tue Nov 21, 2023 10:42 am    Post subject: Reply with quote

hi
As EDC said : take car to the baud rate
in the tab program.s to the to of Nextion IDE you have to write
[img]
with you baud rate


to get a text I use this function

Code:

'*******************************************************************************
'get a text from a tool sending a text
'*******************************************************************************
     Function Nex_gettxt() As String
   Local Gtext As String * 15
   Local Svalue As String * 30
   Svalue = ""
   Gtext = "get " + Text_id + ".txt"                        'Request The text of the device
   Print#nex , Gtext;
   Nexend                                                   'macro which send &hFF,&hFF,&hFF
   Call Nex_return()
   For Jb = 2 To 30
      If Tabreturn(jb) <> 255 Then
         Svalue = Svalue + Chr(tabreturn(jb))
      Else
         Exit For
      End If
   Next
   Nex_gettxt = Svalue
End Function


with the sub nex_return()
Code:

 '*****************************************************************************
' to know the reply from nextion
' this version use a 4 lines lcd display to visualize so you have to remark
'the lcd lines
'******************************************************************************
Sub Nex_return()

   Nexbyte = 0
   Flagcar = 0
   Do

      If Ischarwaiting(#nex) = 1 Then                       'we fill an array of byte received---
         Incr Nexbyte
         Tabreturn(nexbyte) = Inkey(#nex)
         Debug #usb , Hex(tabreturn(nexbyte)) ; " ";
         If Tabreturn(nexbyte) = 255 Then                   'we count the "end char"
            Flagcar = Flagcar + 1
         End If
      Elseif Ischarwaiting(#nex) = 0 And Flagcar = 3 Then   'We are waiting for the end of the reception  nbyte>0
         If Flagcar = 3 Then
            Flagcar = 0
            Nexbyte = 0
          '  Waitms 10
            Exit Do
         End If
 '     Elseif Ischarwaiting(#nex) = 0 And Flagcar = 0 Then
 '        Exit Sub
      End If
   Loop

   Select Case Tabreturn(1)

      Case 0
      'init ok
      Case 101                                              'hex 65     'touche event
         Page_id = Tabreturn(2)
         Device_id = Tabreturn(3)
         Event_id = Tabreturn(4)
      Case 102                                              'hex66 'current page number


      Case 103                                              'hex67  use for awake
'Tabreturn(2)   = coordinate x
' Tabreturn(3)  = coordinate x
'Tabreturn(4)   = coordinate Y
'Tabreturn(5)   = coordinate Y     '
      Case 104                                              'hex68 use for sleep
'Tabreturn(2)   = coordinate x
' Tabreturn(3)  = coordinate x
'Tabreturn(4)   = coordinate Y
'Tabreturn(5)   = coordinate Y     '

        ' Cls
      Case 112                                              'hex 70 chaine de caractère
' each byte of tabreturn(n) is converted to char(aascii)

      Case 113                                              ' hex 71    numeric value on 4bytes  see Function Nex_onevalue
  'Tabreturn(2)   =byte
' Tabreturn(3)  = *256
'Tabreturn(4)   = *65536
'Tabreturn(5)   = *16777216
      Case 136                                              'hex 88 'init ok
         Debug #usb , "Init ok"
         'Waitms 100
      Case Else
         Debug #usb , "error Next : " ; Hex(tabreturn(1))
         Ret_init                                           '*******************************************20-06-2020--------------
         Wait 1
         If Tabreturn(1) = 2 Then Nex_init
         Ret_init
   End Select
End Sub
 


the function give you the string in svalue , the var jb give you the ascii value of each character

I hope it can help you
nex time add your hmi file to give you a better answers

what kind of display are you using (discover.. intelligent ..)?
JP Wink

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
rwlee

Bascom Member



Joined: 04 Aug 2008
Posts: 36
Location: La Porte, Texas

usa.gif
PostPosted: Thu Nov 23, 2023 2:31 am    Post subject: Thanks For the Help Reply with quote

Thanks guy's, My Computer was baking cookies and i haven been able to post any reply's for a couple days.

I found how to change the baud in the Nextion Editor so I bumped up the speed to a typical used baud rate baud=115200

That workes fine, Thanks again for everyone help!
Back to top
View user's profile
rwlee

Bascom Member



Joined: 04 Aug 2008
Posts: 36
Location: La Porte, Texas

usa.gif
PostPosted: Thu Nov 23, 2023 10:15 pm    Post subject: Reply with quote

Duval JP,

Thanks,

I am working with 2 different displays, Nextion Enhanced NX3224K024 2.4" and a 5.0″ Enhanced NX8048K050. 2 years ago you helped me with so much, and I though I had it all figured out, then I lost a hard drive and lost all of my work and learning. So now back to the drawing board so to speak! I ran into a very strang issue yesterday, When I send a "t0.txt" via a button from the Nextion it will trigger the Serial interrupt but when I send a "n0.val" it wont and I get nothing, I hava 2 serial moniters runing one for com1 #Usb and one for com2 #Nex the incoming terminal shows the the Hex Val 03 00 00 00 from the Nextion display but nothing captured from the ATMEGA2560,
What Am I missing?



Code:

$Regfile="m2560def.dat"
$crystal = 16000000                 ' used crystal frequency
$hwstack = 256
$swstack = 128
$framesize = 256

Config Submode = New
'$baud = 19200

Config Submode = New
Debug On
Enable Interrupts                                                   'Off                                                                           '

'***debug serial port **************************************************************
Config Com1 = 115200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Echo Off
Config Serialin0 = Buffered , Size = 30                     'RX from Terminal
Config Serialout0 = Buffered , Size = 30
Open "COM1:" For Binary As #1
Const Usb_ = 1

   '---nextion serial port-------------------------------------------------------------------------------------------------------------

Config Com2 = 115200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0       '19200 modif pour ecran intelligent
Config Serialin1 = Buffered , Size = 50
Config Serialout1 = Buffered , Size = 30
Open "COM2:" For Binary As #2
Const Nex_ = 2

config PORTb.7 = Output
Led_ alias PORTb.7

Dim Buf_ as Byte
Dim Char As Byte
Dim Last_Bte(5) as Byte                                                 ' Byte Value
'Dim Last_str(5) as Byte                                        ' String Value
Dim Resultstring As String * 4
Dim Cntr_ as Byte

Print #Usb_ ,"Start"
Buf_ = Ischarwaiting(#Nex_)
Print #Usb_ , Buf_
If Buf_ = 1 then
  For Cntr_ = 0 to 4
     Char = Inkey(#Nex_)
     Toggle Led_
     Select Case Char
        Case 13  :                             'CarriageReturn ->swallow
        Case 255  :                            'NewLine ->got string ->processing
            Resultstring = ""                  'clear the string for new message
        Case Else
                        Cntr_ = Cntr_ + 1                                       ' Counter Bytes collected for testing purposes
            Resultstring = Resultstring + Chr(char)
            Last_Bte(Cntr_) = Resultstring
            Print #Usb_, Cntr_
            Print #Usb_ , Last_Bte(Cntr_)
                        'Print #Usb_ , Last_str(Cntr_)
        End Select

  Next
End If
 


Thanx
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1257
Location: SYDNEY

australia.gif
PostPosted: Fri Nov 24, 2023 4:02 am    Post subject: Reply with quote

Having programmed Nextion displays to directly control my inverters I can recommend looking into the reparse
mode where you control the serial of the display it opens great opportunity in data transmission where you add
CRC and start and end to your serial. Once you have start and end characters you can use Bascom bytematch
where it triggers an interrupt apon receiving a specific byte normally at the end of the data (use a byte that is an
not normally used character like 185) you can then in the interrupt find the beginning , check the CRC and quickly
process the data with an avr running at 16 to 20 Mhz
I am having no trouble processing data from a device that sends at 115200 baud every second and still able
to flash a led in the main loop once a second
I explain the reparse mode here https://unofficialnextion.com/t/how-to-use-the-reparse-mode/1270

I found using t0.txt and n0.val was not helpful as it did not tell me what they were so I gave them names
like tresult.txt and ninput.val then when I looked at the bascom code I know what it does.

Regards Paul
Back to top
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Fri Nov 24, 2023 4:14 pm    Post subject: Reply with quote

hi rwlee
now you can make backups of your disks. i made 3 of them Twisted Evil 1 every day, 1every week, one every month Twisted Evil

Nextion is Evil or Very Mad

Quote:
When I send a "t0.txt" via a button from the Nextion it will trigger the Serial interrupt but when I send a "n0.val" it wont and I get nothing,


did you try with the debug of nextion ide (put you page in first position to do) ?

maybe the touch release event or touch press event are not well assigned

othe problem to get numeric value , I use the function
Code:

'*******************************************************************************
' get a value from a tool sending a value from byte to long
'*******************************************************************************
Function Nex_value() As Long
   Local Gtext As String * 15

   Local Xvalue As Long
   Local Bvalue As Long
  ' Local Lvalue2 As Long
   Gtext = "get " + Text_id + ".val"                        'Request The Actual Value
   Print#nex , Gtext;
   Nexend                                                   'macro which send &hFF,&hFF,&hFF
   Call Nex_return() 'my sub to receive from nextion
   Debug #usb , Tabreturn(1) ; " " ; Tabreturn(2) ; " " ; Tabreturn(3) ; " " ; Tabreturn(4) ; " " ; Tabreturn(5) ; " " ; Tabreturn(6)
   Xvalue = Tabreturn(2)
   Bvalue = Tabreturn(3) * 256
   Xvalue = Xvalue + Bvalue
   Bvalue = Tabreturn(4) * 65536
   Xvalue = Xvalue + Bvalue
   Bvalue = Tabreturn(5) * 16777216
   Xvalue = Xvalue + Bvalue

   Debug #usb , Xvalue ; " " ;

   Nex_value = Xvalue

End Function



but you have to check with the nextion debbuger before use it to know the number of byte to prossess

in your code
Resultstring = Resultstring + Chr(char)=====> is a string
Last_Bte(Cntr_) = Resultstring ======> is a array of byte
you're mixing up type or I don't understand

JP

Wink

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
rwlee

Bascom Member



Joined: 04 Aug 2008
Posts: 36
Location: La Porte, Texas

usa.gif
PostPosted: Fri Nov 24, 2023 11:28 pm    Post subject: Thanks Duval JP Reply with quote

I downloaded your program (nextion AVR1284 first step)and changed Nothing,



I only added 2 Print lines, Because I was only gettin one input from the Bascom Serial Monitor



So I Added a print statment to try and Locate what the 80 was coming from.



I found it!



It it doesn't go through the Select Case Statment and wont print the Device_Id



Do you thinkit has anything to do with the fact that I'm using a ATMEGA2560?

I only modifed your code to use with my 2560 Everything else is original







When testing with my second Serial Monitor I can see that the Nextion screen is outputing the proper info on the touch screen event


Code:



'Fist steps with nextion and AT1284
'I use Nextion since 4years  now I can give some tips to my bascom friends
'Jp Duval 24-08-2022---------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------------
$Regfile="m2560def.dat"
$crystal = 16000000                 ' used crystal frequency
$hwstack = 256
$swstack = 128
$framesize = 256
Config Submode = New

'$regfile = "m1284pdef.dat"                                  ' "m1284def.dat"
'$crystal = 11052000
'$hwstack = 300       '300
'$swstack = 250                                              '200
'$framesize = 250                                            '300

Config Submode = New
Debug On                                                    'Off                                                'On                                                    '

'***debug serial port **************************************************************
Config Com1 = 115200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Echo Off
Config Serialin0 = Buffered , Size = 30                     'RX from Terminal
Open "COM1:" For Binary As #1
Const Usb = 1
'M1284------------------------------------
 ' RXD0= yellow to  PD0
' TXD0=yellow to  PD1
   '---nextion serial port-------------------------------------------------------------------------------------------------------------
Const Nex = 2
Config Com2 = 38400 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0       '34800   modif le 24-06-2022
'Config Com2 = 115200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0       '19200 modif pour ecran intelligent
Open "COM2:" For Binary As #2
Config Serialin1 = Buffered , Size = 50
Config Serialout1 = Buffered , Size = 30
 ' RXD1= yellow to  PD3
' TXD1= Blue to  PD2
' include files
'*******************************************************************************
$include "declaration_nextion.inc"
$include "sub_macro_nextion_base.inc"

'*******variable to use with nextion**************************************************
Dim Tabreturn(20) As Byte                                   'array of byte returned by nextion
Dim Flagcar As Byte
Dim Page_id As Byte , Device_id As Byte , Event_id As Byte
Dim Letter_id As String * 10
Dim Nexbyte As Byte
Dim Neximage As Byte , Xval As Word , Yval As Word
Dim Sentence As String * 30
Dim Text_id As String * 5
Dim Mynum As Long
'**** generic Variables*************************************************************
Dim Jb As Byte , Nbyte As Byte , Jbyte As Byte , Jword As Word
'*******************************************************************************
Enable Interrupts

Begin:
   Do
'      'Ret_init
'      Waitms 20
      Device_id = 0
      Call Nex_return()
      Print #usb , "Device_id " ; Device_id                                      '               Call Nex_returntime()
     Waitms 10
      If Device_id <> 0 Then Print #usb , "device_id menu :" ; Device_id
      Select Case Device_id
         Case 1                                             'play  with the frog
            If Neximage = 1 Then
               Neximage = 0
                 Letter_id = "t1"
                 Call Nex_text(letter_id , "Aaaarg" , White , Red , 0)
            Elseif Neximage = 0 Then
               Neximage = 1
                 Letter_id = "t1"
                  Call Nex_text(letter_id , "Croa-croa" , Red , Lightblue , 0)
            End If
            Xval = 39
            Yval = 11
            Call Nex_image(xval , Yval , Neximage)
            Letter_id = "t1"

            '  '-----------------------------------------------
        Case 4                                              'reading a text
        Text_id = "t2"
           Sentence = Nex_gettxt() As String
              Letter_id = "t1"
                  Call Nex_text(letter_id , Sentence , Green , Yellow , 0)

' '-----------------------------------------------
         Case 5                                             'reading a number
         Text_id = "n0"
         Mynum = Nex_value() As Long
             Letter_id = "t1"
             Sentence = Str(mynum)
                  Call Nex_text(letter_id , Sentence , Yellow , Green , 0)
                  '-----------------------------------------------
         Case 6                                             'reading a value from a slide bar

         Text_id = "h0"
         Mynum = Nex_value() As Long
             Letter_id = "t1"
             If Mynum > 0 And Mynum < 501 Then              'to cancel the erratic reading
             Sentence = Str(mynum)
                  Call Nex_text(letter_id , Sentence , Yellow , Green , 0)
                  Waitms 100
               End If
      End Select

   Loop
   End
 


And from the Inc file

Code:


Sub Nex_return()

   Nexbyte = 0
   Flagcar = 0
   Do
      If Ischarwaiting(#nex) = 1 Then       'we fill an array of byte received---
         Incr Nexbyte
         Print #usb, "Nexbyte " ; Nexbyte                        '  ********* Check how many bytes are recieved, I can only see one byte per touch event *********
         Tabreturn(nexbyte) = Inkey(#nex)
         Debug #usb , Hex(tabreturn(nexbyte)) ; " ";
         If Tabreturn(nexbyte) = 255 Then       'we count the "end char"
            Flagcar = Flagcar + 1
         End If
         'page_id = Tabreturn(2)
         'Device_id = Tabreturn(3)
         'event_id  = Tabreturn(4)
         'Print #usb , "page_id " ; page_id
         'Print #usb , "Device_id " ; Device_id
         'Print #usb , "event_id " ; event_id

      Elseif Ischarwaiting(#nex) = 0 And Flagcar = 3 Then       'We are waiting for the end of the reception  nbyte>0
         If Flagcar = 3 Then
            Flagcar = 0
            Nexbyte = 0
          '  Waitms 10
            Exit Do
         End If
 '     Elseif Ischarwaiting(#nex) = 0 And Flagcar = 0 Then
 '        Exit Sub
      End If
   Loop
   Print #usb , "Tabreturn " ; Tabreturn(1)
   Select Case Tabreturn(1)

      Case 0
      'init ok
      Case 101       'hex 65     'touche event
         page_id = Tabreturn(2)
         Device_id = Tabreturn(3)
         event_id  = Tabreturn(4)
      Case 102       'hex66 'current page number


      Case 103       'hex67  use for awake
'Tabreturn(2)   = coordinate x
' Tabreturn(3)  = coordinate x
'Tabreturn(4)   = coordinate Y
'Tabreturn(5)   = coordinate Y     '
      Case 104       'hex68 use for sleep
'Tabreturn(2)   = coordinate x
' Tabreturn(3)  = coordinate x
'Tabreturn(4)   = coordinate Y
'Tabreturn(5)   = coordinate Y     '

        ' Cls
      Case 112       'hex 70 chaine de caractère
' each byte of tabreturn(n) is converted to char(aascii)

      Case 113       ' hex 71    numeric value on 4bytes  see Function Nex_onevalue
  'Tabreturn(2)   =byte
' Tabreturn(3)  = *256
'Tabreturn(4)   = *65536
'Tabreturn(5)   = *16777216
      Case 136       'hex 88 'init ok
         Debug #usb , "Init ok"
         'Waitms 100
      Case Else
         Debug #usb , "error Next : " ; Hex(tabreturn(1))
         Ret_init       '*******************************************20-06-2020--------------
         Wait 1
         If Tabreturn(1) = 2 Then Nex_init
         Ret_init
   End Select
End Sub
'******************************************************************************

 


But I can tell it's not going through the Select case Statement!
Back to top
View user's profile
rwlee

Bascom Member



Joined: 04 Aug 2008
Posts: 36
Location: La Porte, Texas

usa.gif
PostPosted: Sat Nov 25, 2023 4:46 am    Post subject: Found the Problem! Reply with quote

It was as simple as I didn't notice that I had left your exsisting Baud Rate at 38400 and my Baud rate on my Nextion was 115200! messing with my head all day long. Everything is working now. Thanksfor your help JP! it was your simple program thathasme on the right track again!
Back to top
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Sat Nov 25, 2023 9:51 am    Post subject: Reply with quote

rwlee,
Thank you for your last message it's always a great pleasure to be able to help a friend on the other side of the world, that's the beauty of the internet.
and please make backups Wink JP

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Goto page Previous  1, 2, 3
Page 3 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum