Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Incomprehensible behavior asc (x)

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
pierscintilla

Bascom Member



Joined: 08 Aug 2005
Posts: 54
Location: italy

PostPosted: Sun Nov 07, 2021 4:11 pm    Post subject: Incomprehensible behavior asc (x) Reply with quote

I'm simulating these few lines of code.

Code:

$regfile = "m328pdef.dat"                                  
$crystal = 16000000                                        
$baud = 9600                                                
$hwstack = 32                                              
$swstack = 10                                              
$framesize = 40                                            

Dim Nm As String * 1
Config Serialin = Buffered , Size = 50
portd.0 = 1                                       'pullup Rx
Enable Interrupts

do
   If Ischarwaiting() = 1 Then              
       Nm = inkey()                              
       print  asc(Nm)                            
   end if
loop
End
 


Everything works fine up to the ASCII character 126 while from the character 127 everything becomes incomprehensible. For example with the characters 127, 128 and 129 ASC (character) gives respectively 166, 199 and 252.
I enter the characters by keypad with numeric keypad + ALT.
If instead of entering the characters using the keyboard, I enter them via code - eg. A = CHR (130) print ASC (A) - the printed result is correct.
Results obtained with the compiler 2.0.8.4 build 2.0.8.4.001.
What am I doing wrong?
Solutions?

Thanks in advance
Pier

(BASCOM-AVR version : 2.0.8.4 )
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun Nov 07, 2021 4:38 pm    Post subject: Reply with quote

Welcome to the wonderworld of ASCII, ANSI, multibyte UTF-8, UTF-16 and differing codepages.
Back to top
View user's profile
pierscintilla

Bascom Member



Joined: 08 Aug 2005
Posts: 54
Location: italy

PostPosted: Sun Nov 07, 2021 10:48 pm    Post subject: Reply with quote

Possible that the reason is that but the strange thing is that the limit of regular operation is 126 and not 127. Another oddity is that if I assign the value of CHR (127) or more to a variable, the subsequent conversion with ASC () gives a exact result ...
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Mon Nov 08, 2021 11:23 am    Post subject: Reply with quote

pierscintilla wrote:
Possible that the reason is that but the strange thing is that the limit of regular operation is 126 and not 127.

Nonsense. As string in Bascom is set up as byte-array and a byte within Bascom holds 8 bits, equals a range of 0..255.

Your problem - and btw., you'd be already smarter if you'd had searched for the keywords I've offered - is, that the input-device "keyboard" may use different mapping than the output-device "display" and the controller may use another, different one.

Mappings are also called "code pages" and translates a key pressed with printed "A" on into a certain value, which seems to be in your case an one byte value.
There are other character tables, which use more than one byte to hold all available characters.
Quote:
I enter them via code - eg. A = CHR (130)

If "A" is not simply mentioned as variable name: the ASCII-code for "A" equals dec 65.

If 130 shows "A" on your display, then already an extended character is shown.
Extended ASCII starts at dec 128 and they are many different ones, said "code pages", for example code page 1252 is associated with the term "ANSI".

The very point you are wondering likely is, that in your case your input device does not use the same code-page than your output device does.
This starts as early you send characters via a terminal software, which uses a special code page.
Keycodes of characters entered are translated into values according the code page and sent out.
If your little code only returns the same byte values without creating new ones, basically nothing should go wrong, as long the receiving part within your PC (terminal software) uses the same code page.
The controller does not know about code-pages, it knows only a bout bytes.

Thus if you use some self-made software PC software for sending and receiving, it must use the same code-page for both.
And, in case the controller sends/receives its own messages too, it also must stick to this code-page.
As the controller does not know about certain characters of your code page, you have to teach it.
Back to top
View user's profile
pierscintilla

Bascom Member



Joined: 08 Aug 2005
Posts: 54
Location: italy

PostPosted: Mon Nov 08, 2021 2:01 pm    Post subject: Reply with quote

I had assumed a problem of this type but if I insert a character in a text file by typing with my keyboard ALT 127, 128, etc I get exactly the symbols expected for these codes. Furthermore, the fact that the problem occurs not from 128 but from 127 seems rather strange to me.
I am using my keyboard (tested as described) to enter data (characters) in the integrated bascom AVR simulator
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5917
Location: Holland

blank.gif
PostPosted: Mon Nov 08, 2021 2:33 pm    Post subject: Reply with quote

just that 127 (DEL) does not work as you think it should does not mean that MWS is not right.

In ASCII there are 7 bits which can be used to create 128 characters. Not all characters are printable. There was a time that there were different ASCII table implementations. And just like with different languages : this makes communication hard. So the first halve of the table is standardized.
But do not forget that also the bytes that are a standard are just an encoding : 065 give an A.
The second halve of the table is used for EASCII (extended ASCII). There can be many different tables. code pages are used to know which table to use.
1252 is default for MS windows. It is also named ANSI coding.
Later UTF-7 , UTF-8, UTF-16 and UTF32 were created. Today UTF-8 is used most. this because the lower table is compatible with the lower table of ASCII.

The DOS chcp commands gives the actual codepage. 850 in my case.
These pages you can look up for example here : https://www.ascii-codes.com/cp850.html
with chcp you can also change the codepage.

terminals or other software can have settings for the codepage too. And even your keyboard has options to change the character set and language.

_________________
Mark
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Mon Nov 08, 2021 6:15 pm    Post subject: Reply with quote

pierscintilla wrote:
I am using my keyboard (tested as described) to enter data (characters) in the integrated bascom AVR simulator

I've ran the simulation in loop, no breakpoints, this is what I got from manually entering alt-127 to alt-255.
This translation table is what makes out part of a code page.
Code:
cp:
' starting alt-127
Data 166
Data 199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197
Data 201,230,198,244,246,242,251,249,255,214,220,248,163,216,215,131
Data 225,237,243,250,241,209,170,186,191,174,172,171,188,161,171,187
Data 166,166,166,166,166,193,194,192,169,166,166,43,43,162,165,43
Data 43,45,45,43,45,43,227,195,43,43,45,45,166,45,43,164
Data 240,208,202,203,200,105,205,206,207,43,43,166,95,166,204,175
Data 211,223,212,210,245,213,181,254,222,218,219,217,253,221,175,180
Data 173,177,61,190,182,167,247,184,176,168,183,185,179,178,166,160

What exactly do you want to find out?
The type of extended code page of the terminal window? Seems to be of little use.
And yes, alt-127 does already translate into 166.
But very likely a 256 byte translation table is doing its work and then every character can be translated.
Back to top
View user's profile
pierscintilla

Bascom Member



Joined: 08 Aug 2005
Posts: 54
Location: italy

PostPosted: Mon Nov 08, 2021 10:30 pm    Post subject: Reply with quote

This is exactly what I get too.
Changing the keyboard layout does not change the result
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5917
Location: Holland

blank.gif
PostPosted: Wed Nov 10, 2021 8:57 pm    Post subject: Reply with quote

when you want the code you entered you need to use 4 digits : ALT+0127 for example.
otherwise you get a code from a code page.

_________________
Mark
Back to top
View user's profile Visit poster's website
pierscintilla

Bascom Member



Joined: 08 Aug 2005
Posts: 54
Location: italy

PostPosted: Thu Nov 11, 2021 2:23 pm    Post subject: Reply with quote

Good!
Now works fine.

Thanks
Pier
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
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