Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
clawson
PostPosted: Mar 08, 2012 - 11:27 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62354
Location: (using avr-gcc in) Finchingfield, Essex, England

Exactly how hard are you trying to answer your own questions or do you want everything haned to you on a plate? I already pointed you to the online manual for Bascom. Surely you read about this there?

http://avrhelp.mcselec.com/input.htm

I found that page by starting on the page for "CONFIG SERAILIN" - that you must already be using - and noticing that it uses "INPUT" so I then searched out INPUT to see if it had an example showing how to receive strings rather than single characters. As you can see there, simply using "INPUT S" where S is defined as a String causes it to read a whole string.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Mar 08, 2012 - 11:51 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18581
Location: Lund, Sweden

Quote:

please help me
as i cant find anything in the given above links.

How can i read and write using Uart in bascom

1. Go to http://avrhelp.mcselec.com/index.html
2. Click the Index link at the top left
3. Scroll down to "-U-" and select the link "Using the UART"
4. To the right is what looks like a very informative first article

After reading that you should be armed with new search terms to put into Google or the search function here at AVRfreaks.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
mahaveer
PostPosted: Mar 08, 2012 - 04:49 PM
Hangaround


Joined: Aug 14, 2011
Posts: 135


Dim Akey As Byte 'Here we declare a byte variable


Dim S As String * 16 , Z1 As String * 15 , A As String * 15 'Declare a string variable here


'Here we declare a byte variable
Do

Do
Akey = Waitkey()
If Akey = 13 Then Goto Thanks 'On enter key goto thanks
S = S + Chr(akey)

Loop


Print "Rotctl values " ; S ; " !"
Z1 = Mid(s , 3 , 5) : Print Z1


A = Mid(s , 11 , 5) :
Print A

Loop
End

Output :-when i type mahaveerhanuman i am getting
thanks mahaveerhanuman
aveer
numan
as it a loop when type some other string like veeruyadav

i am getting out put as

thanks mahaveerhanumanveeruyadav(here i dont want to get first string back again how can we avoid it)

aveer
numan

so that i can only get out put as veeruyadav and when ever i repeat this i shouldn't get previous string.

how can i do it.
 
 View user's profile Send private message  
Reply with quote Back to top
thygate
PostPosted: Mar 08, 2012 - 04:54 PM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

Quote:
so that i can only get out put as veeruyadav and when ever i repeat this i shouldn't get previous string.

Code:
S = S + Chr(akey)

maybe clear the string variable before appending new data ?!

Also, use code tags when posting code, to make it a little more readable.

I've never used bascom, but i'm guessing it should look like this
Code:
Dim S As String * 60 , Z1 As String * 15 , A As String * 15
Dim Akey As Byte

Do
  ' read string from uart that is terminated with CR
  S = "" '<===== CLEAR THE STRING
  Do
    Akey = Waitkey()
    If Akey = 13 Then Goto cmdRecvd
    S = S + Chr(akey)
  Loop

cmdRecvd:
  ' Parse string..
  Print "Rotctl values " ; S ; " !"
  Z1 = Mid(s , 3 , 5) : Print Z1
  A = Mid(s , 11 , 5) : Print A
Loop


You should use SPLIT first as this will never work if the values are not fixed width.
Then use RIGHT to get rid of the letters at the start of the substrings, then convert those strings to numeric variables.

So something like this :
again I've never used bascom or basic, so this might require some editing :
Code:
Dim str as string * 60
Dim substr(5) As String * 15
Dim azimuth as Single, elevation as Single

str = "az045.00 el110.00"

' split into substrings on space
Split( str , substr(1) , " ")

' get rid of first 2 characters in substrings
substr(1) = RIGHT(substr(1), len(substr(1)) - 2)
substr(2) = RIGHT(substr(2), len(substr(2)) - 2)

' convert strings to numeric values
azimuth = Val(substr(1))
elevation = Val(substr(2))
 
 View user's profile Send private message  
Reply with quote Back to top
mahaveer
PostPosted: Mar 08, 2012 - 07:40 PM
Hangaround


Joined: Aug 14, 2011
Posts: 135


thank you sir ,

how to check the ASCII values of my keyboard
because the values provide by bascom are totally different from my board .
 
 View user's profile Send private message  
Reply with quote Back to top
thygate
PostPosted: Mar 08, 2012 - 07:52 PM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

???
You do know what the "S" in "ASCII" stands for, right ?

explain in more detail where the problem lies, because this doesn't make much sense.

What is your setup and what input is generating what output where ?


Last edited by thygate on Mar 08, 2012 - 09:44 PM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Mar 08, 2012 - 09:43 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18581
Location: Lund, Sweden

http://en.wikipedia.org/wiki/ASCII
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
mahaveer
PostPosted: Mar 08, 2012 - 10:48 PM
Hangaround


Joined: Aug 14, 2011
Posts: 135


Thygate

Sir i am not talking about the code i am asking in general that

how find the ASCII value of the pressed button.

_________________
Thanking You
Mahaveer
 
 View user's profile Send private message  
Reply with quote Back to top
thygate
PostPosted: Mar 09, 2012 - 01:38 AM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

well then follow the link posted by JohanEkdahl, I'm not sure i see the relevance in the current context though.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Mar 09, 2012 - 08:10 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18581
Location: Lund, Sweden

As long as we're talking about ordinary (latin/english/whatever-you-call'em) characters it holds true that digit characters start with '0' having ASCII code 48, '1' is 49 and so on. Capital letters start with 'A' being 65 and so on, and 'a' being 97 and so on.

For "national characters" ('ä', 'ñ' and so on) "it depends". Those latter are really not ASCII - the original ASCII-7 code chart covers 128 characters only.

This really has nothing to do with embedded systems in general, or AVR in particular. There is abundant information on The Web for you to read up on. Start with the article I pointed to and with that read you should have plenty of ideas for new search terms to put into Google.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
clawson
PostPosted: Mar 09, 2012 - 09:57 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62354
Location: (using avr-gcc in) Finchingfield, Essex, England

When I were a nipper the BASIC language had CHR() and ASC() functions. So my 4th trip back to the Bascom help pages (why am I doing this and not the OP??) and I find:

http://avrhelp.mcselec.com/asc.htm

Well, whadda ya know?!?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
mahaveer
PostPosted: Mar 09, 2012 - 11:31 AM
Hangaround


Joined: Aug 14, 2011
Posts: 135


Sir,when ever i gave command/angle in cmd it generates some line string as shown in fig 1 ,from that string i need to use only numerical which i have done ,but i am facing two problems .

1st :-when i use Akey = 13 in my program i can extract the string from the cmd, but to execute it i need to press "ENTER Key "on my board that selecting the terminal window.as shown in 2nd fig.

2nd :- when change Akey = 88--(X)in my program i am unable to read nothing .as shown in fig3

so, how can i execute my program automatically after given commands in cmd without entering into terminal window and As soon as the last letter of the string is received (from cmd ) i:e it X
AZ65.0 EL78.0 UP000 XXX DW000 XXX

-----------------------------------------------------
Dim S As String * 15 , Z1 As String * 15 , A As String * 15 , B As String * 12, Instring As String * 10 , Varval As Integer

Do
S = ""
Do
Akey = Waitkey()

If Akey =13 Then Goto Thanks // i am facing problem at this line
S = S + Chr(akey)
Loop
Thanks:
Print "Rotctl values " ; S
Z1 = Mid(s , 3 , 5) : Print Z1
A = Mid(s , 10 , 5) : Print A
Instring = Z1
Varval = Val(instring)
Print Varval
If Varval > 45 Then
Porta.0 = 1
Porta.1 = 1
Porta.2 = 0
Else
Print "no operation "
End If
Loop
End
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Mar 09, 2012 - 11:38 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18581
Location: Lund, Sweden

Quote:

1st :-when i use Akey = 13 in my program i can extract the string from the cmd, but to execute it i need to press "ENTER Key "on my board

Yes, of-course. The terminal user need some way to tell the program that he has finished typing a value. How else would you know if the user has finished after typing "2", or if he will eventually continue entering another digit to say "25"?

If the input is of a fixed format, then you have other possibilities but this makes programming things more complicated programming-wise. Is it not OK to let the user finish his input with Enter?

The enter key generates the special character CarriageReturn which has ASCII code 13. That is why CHR(13) works for you.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
thygate
PostPosted: Mar 09, 2012 - 01:54 PM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

From this picture it is clear that the last character sent is 0x0A = 10 = LF (Line Feed).
Currently you are checking for the CR (Carriage Return) character (0x0D = 13).

So change "if akey = 13" to "if akey = 10".

And you're still not using SPLIT, RIGHT and then VAL to get the numerical value. And it is not an integer type, but Single.

Using MID to cut out fixed portions of the string will NOT WORK for all cases.

You also reserve only 15 bytes for the string while it is obviously much longer. You need to fix this ASAP as this will give you a lot of troubles later.

And for the love of god, put some CORRECT indentation in your code, this is just horrible and bad practice imo.

Why don't you just copy/paste the code i provided above.
 
 View user's profile Send private message  
Reply with quote Back to top
mahaveer
PostPosted: Mar 11, 2012 - 04:44 PM
Hangaround


Joined: Aug 14, 2011
Posts: 135


Sorry Thygate,I had used the split command but by mistake i have posted the previous code it self and their was an error in your code as there no code regarding the "substr(1) = RIGHT(substr(1), len(substr(1)) - 2) " sub ration of the length of the string. hence i used here again MID command.
______________________________________________________

Code:
Dim Akey As Byte , S As String * 80 , Azimuth As Integer , Elevation As Integer , Substr(5) As String * 10 , Bcount As Byte , J As Byte
Dim W1 As Word , A As Single , C As Integer , W2 As Word , A1 As Single , C1 As Integer
Do
 S = ""

Do
Akey = Waitkey()

If Akey = 13 Then Goto Thanks
                              '
 S = S + Chr(akey)


Loop


Thanks:
Print "Rotctl values " ; S

Bcount = Split(s , Substr(1) , " ")

For J = 1 To Bcount

Next

 If Substr(1) = "SA" Then
 Porta = 0
 Elseif Substr(1) = "p" Then
     W1 = Getadc(3)
     A = W1 + 0.6613
     C = A / 2.1867
     W2 = Getadc(7)
     Locate 2 , 1

     A1 = W2 + 33.21
     C1 = A1 / 6.261
     Print "AZ" ; C ; "EL" ; C1                             'this value should be returned to command window


 Else


Substr(1) = Mid(substr(1) , 3 , 6)                          ': Print Substr(1)
Substr(2) = Mid(substr(2) , 3 , 6 )                         ': Print Substr(2)

' convert strings to numeric values

Azimuth = Val(substr(1)) : Print Azimuth
Elevation = Val(substr(2)) : Print Elevation



If Azimuth > 45 Then
Porta.0 = 1
Porta.1 = 0
Porta.2 = 1
Else
Print "no operation "
End If
End If
Loop


 End

______________________________________________________

And if we want to send command for the command window is it enough by just giving a PRINT command or do we need to write any other command.

like when i gave a command "p" in the command window my controller should return the current positions to the command window.

but i am unable to do this is there any error in code.
 
 View user's profile Send private message  
Reply with quote Back to top
thygate
PostPosted: Mar 11, 2012 - 06:13 PM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

There is no need to apologize, if i come over a bit strong sometimes it is merely because I want to put a lot of emphasis on that particular point. We're putting effort in helping you, so we also want to see you succeed.

If you want to check for a single character received (like "P"), you're gonna want to do this BEFORE the Split command, as the split command will probably return nothing as the separation character does not exist in that string. eg. substr will be empty after split on space if there was no space in str.
so
Code:
if s = "p" Then
instead of
Code:
if Substr(1) = "p" Then

But then that will probably only work if the length of s is exactly one, and the character received was also in lower case.


In your screenshot, that I edited and posted again, it is clear that the last character in each packet is LineFeed (0x0A). So you still need to change
"If Akey = 13" to "If Akey = 10" to know when the the incoming string is complete, so you can process it.

Same thing goes for outbound messages, as the PC app will likely also be looking for the LF character. So try the following :
Code:
Print "AZ" ; C ; "EL" ; C1 ; Chr(10) ;

C and C1 should also be of type Single, going from the calculations you are doing.

from the bascom docs about CR/LF, and semicolons after PRINT statements :
Quote:
CARRIAGE RETURN (CR) AND LINE FEED (LF)

In the previous example you can also see that a second print statement always prints the printed text to the following line. This is caused by the fact that the print statement always adds the CR and LF characters.

Basically if we state:

Print “ABC”

We send 65 66 67 13 10 to the UART. (In binary format)

The carriage return character (13) returns the cursor back to column position 0 of the current line. The line feed (10) moves the cursor to the next line.

Print “ABC” ;

When we type a semicolon ( ; ) at the end of the line...

Bascom does not send a carriage return/line feed, so you can print another text after the ABC on the same line.

Print “ABC” ; Chr(13) ;

This would send only ABC CR.


Then about
Code:
substr(1) = RIGHT(substr(1), len(substr(1)) - 2)
As I told you i've never used bascom, and last time i used basic was 2 decades ago. Nevertheless this is the way it should be done. If this is giving an error on compilation, then what is the EXACT error, so we can fix it.

And for your own good, put some real effort in indenting your code correctly. As it is now you are more likely to make errors in the future. If the code grows larger, eventually no one is going to want to look at it anymore because of the unreadability due to the incorrect indentations.
 
 View user's profile Send private message  
Reply with quote Back to top
mahaveer
PostPosted: Mar 11, 2012 - 07:40 PM
Hangaround


Joined: Aug 14, 2011
Posts: 135


substr(1) = RIGHT(substr(1), len(substr(1)) - 2)

getting errors as

Invaild datatype[[100]200]
source variable does not match the target variable
 
 View user's profile Send private message  
Reply with quote Back to top
thygate
PostPosted: Mar 11, 2012 - 08:15 PM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

well, try splitting it up to see what is causing the problem.
Code:
Dim templen as integer
templen = len(substr(1))
substr(1) = RIGHT(substr(1), templen - 2)
 
 View user's profile Send private message  
Reply with quote Back to top
mahaveer
PostPosted: Mar 11, 2012 - 09:12 PM
Hangaround


Joined: Aug 14, 2011
Posts: 135


sir i have already tried this but even i am getting error as
source variable does not match the target variable[_A1 = templen - 2]

_________________
Thanking You
Mahaveer
 
 View user's profile Send private message  
Reply with quote Back to top
thygate
PostPosted: Mar 11, 2012 - 10:46 PM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

so the error is in passing "templen - 2" to the right function ? I'm out of ideas here, what a horrible language this bascom. Perhaps someone else that knows bascom can tell me why on earth it isn't accepting an integer where it should according to the docs.
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits