Deciphering Serial Port results..

Everything else that doesn't fall into one of the other PB categories.
User avatar
blueb
Addict
Addict
Posts: 1116
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Deciphering Serial Port results..

Post by blueb »

Hi
I have a Nextion 7" 'Intelligent" Touch screen see:
https://itead.cc/product/7-0-nextion-in ... enclosure/

It features a HMI (Human Machine Interface) which basically means it comes with it's own Visual Designer.

You design your canvas (pages), then you send your program (flash) to your screen.
The screen then responds to the messages sent/received.

From their web pages: Nextion HMI display connects to peripheral MCU via TTL Serial (5V, TX, RX ,GND) to provide event notifications that peripheral MCU can act on, the peripheral MCU can easily update progress and status back to Nextion display utilizing simple ASCII text based instructions.

I realize they'd like to dump the info into a microchip, such as Raspberry Pi, but I'd like to do this directly.

Using:

Code: Select all

Define Ok.i, Timeout.i
Define Byte.b

If OpenSerialPort(1, "COM4", 115200, #PB_SerialPort_NoParity, 8, 1,  #PB_SerialPort_NoHandshake, 1024, 1024)
   Ok = #False
   Timeout = 500
   
   Repeat
      If AvailableSerialPortInput(1)
         
         If ReadSerialPortData(1, @Byte, 1) = 1
            
            Debug RSet(Hex(PeekB(@Byte), #PB_Byte), 2, "0")
            
         EndIf         
      Else
         Delay(10)
         Timeout - 1
      EndIf
      
   Until Timeout = 0 Or Ok
EndIf


I created a screen with a button that sends the number '105' to the serial port.

This is what is returned from the serial port: 1E 86 7E 1E 86 98 66 66 1E 66 18 98 E6 98 F8

But from the Visual Designer, I get:
HexValue: 43 3A 43 4E 55 4D 31 30 35 3F
String Value: C:CNUM105?

And the 7" Display Simulator returns:
HexValue: 43 3A 43 4E 55 4D 31 30 35 3F
String Value: C:CNUM105?

The results from both are the same:
C:C = Unique ID
NUM = says that a number is being sent
105 = the value sent
? = Stop character

So I need to find a way to corelate the output to something that makes sense to me. haha

I'd prefer the output send a string, to that i can see if it is correct.

Any ideas from you serial port experts?

Thanks in advance
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Deciphering Serial Port results..

Post by Paul »

Maybe something like this...
(can't actually test cause I don't have that hardware)

Code: Select all

Define Ok.i, Timeout.i
Define Byte.a,Dat.i,build=#False,result$

If OpenSerialPort(1, "COM4", 115200, #PB_SerialPort_NoParity, 8, 1,  #PB_SerialPort_NoHandshake, 1, 1)
  Ok = #False
  Timeout = 500
  
  Repeat
    If AvailableSerialPortInput(1)
    
      ReadSerialPortData(1,@Byte,1)
      Dat=PeekB(@Byte)
    
      Select Dat
        Case $43 ;C unique ID detected, start building string
          build=#True
          result$=""
          
        Case $3F ;? Stop Character detected, end string and display
          build=#False
          Debug result$
          ;ok=#True to quit or leave out to continue polling port
        
        Default
          If build
            result$+Chr(Dat)
          EndIf
      EndSelect
      
     
      Else
      Delay(10)
      Timeout - 1
    EndIf
      
  Until Timeout = 0 Or Ok
EndIf
Image Image
User avatar
blueb
Addict
Addict
Posts: 1116
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Deciphering Serial Port results..

Post by blueb »

Thanks for the interest Paul
I tried commenting out everything, so I could see what the results of 'Dat' was:

Code: Select all

Define Ok.i, Timeout.i
Define Byte.a,Dat.i,build=#False,result$

If OpenSerialPort(1, "COM4", 115200, #PB_SerialPort_NoParity, 8, 1,  #PB_SerialPort_NoHandshake, 1, 1)
  Ok = #False
  Timeout = 1000
  
  Repeat
    If AvailableSerialPortInput(1)
    
      ReadSerialPortData(1,@Byte,1)
      Dat=PeekB(@Byte)
      
    Debug Dat
     
      Else
      Delay(10)
      Timeout - 1
    EndIf
      
  Until Timeout = 0 Or Ok
EndIf
and this is what I saw...
30
-122
126
30
-122
-104
102
102
30
102
24
-104
-26
-104
-8
This is for the same button #105 from the first post....
------------------------------
This is what is returned from the serial port: 1E 86 7E 1E 86 98 66 66 1E 66 18 98 E6 98 F8

But from the Visual Designer, I get:
HexValue: 43 3A 43 4E 55 4D 31 30 35 3F
String Value: C:CNUM105?
-----------------------------

This time the numbers were different.. maybe you can decipher the results?

Thanks again.
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
infratec
Always Here
Always Here
Posts: 7619
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Deciphering Serial Port results..

Post by infratec »

I think it depends more on how you send the 105 to the device.
In ASCII or as binary number?

Or better do you send it the same way as the other tool?
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Deciphering Serial Port results..

Post by fsw »

I suppose you looked at the instruction set:
https://nextion.tech/instruction-set/

I don't know these HMI displays, but it's surely nice idea.
(not cheap though...)

EDIT:
Here is a Arduino library:
https://github.com/itead/ITEADLIB_Arduino_Nextion

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
blueb
Addict
Addict
Posts: 1116
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Deciphering Serial Port results..

Post by blueb »

Hi fsw,

Thanks for the interest. Yes I read these docs.

I bought the screen with the intention of connecting the unit to an ESP32 microchip controller, but did a little testing and found that each button press from the screen sent a series of (bytes?) directly to the serial port.

I then got the brilliant idea (?) of intercepting the signal by watching the COM port and having PureBasic react to the signals (bypassing the microchip altogether).

That would save me from using the Arduino IDE and hard connections to the microchip, etc.

The Flight Simulator (X-Plane 11) I intend to use this with can interact with Lua instructions and there's some Lua code on the forum to make this happen.

All I have to do is sniff the output from the screen correctly. That's the goal. I may have to give up on the idea, but it's worth a shot.


Where I am at the moment...

When I open COM4 (screen display)

my Debug window displays: (I assume this is some type of code that says: "yes, I found you")
44
FD

I can change displays (Panels in PureBasic speak) and nothing happens until I press a button.

Starting with the basics (pun intended) I programmed a simple panel with one LED...

I press ON then I get:
1E
86
7E
1E
06
98
66
06
60
FE
98

F8
98

FE
F8

I press OFF the I get:
1E
86
7E
1E
06
98
66
06
60
FE
98

78
86
60

FE
F8
-------------------------------------------
As you can see, changes only take place at the 12th byte position for 2 bytes. OFF sends an additional byte in the 14th position.

It shouldn't take too long to convert the signals to something PureBasic can use, and eliminate the MCU and Arduino stuff (I'm hoping)

I have no problem using these... it's just that I like PureBasic.. that all. {grin}

PS - These are some of the 'Panels' being built. I have seen well over 40 of the panels using one screen.
https://www.dropbox.com/s/bt53wlc9c4ibh ... t.jpg?dl=0
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Post Reply