View previous topic :: View next topic |
Author |
Message |
pinkfloyd11
Joined: 02 Jul 2007 Posts: 247
|
Posted: Thu Oct 25, 2007 7:06 pm Post subject: Porting code from VB6 to Bascom |
|
|
Dear Friends
I'm porting a program from VB6 to Bascom AVR.
Now when compile the follow code I have "Invalid Datatype Error"
MSCOMM1.Output = Chr(2) & Chr(3) & Chr(4)
Stringaloopback = Chr(2) & Chr(3) & Chr(4)
Which is the correct way in Bascom?
Thanks |
|
Back to top |
|
|
Evert :-)
Joined: 18 Feb 2005 Posts: 2156
|
Posted: Thu Oct 25, 2007 7:34 pm Post subject: |
|
|
Bonjorno,
MSCOMM1.Output = Chr(2) & Chr(3) & Chr(4)
Print Chr(2) ; Chr(3) ; Chr(4)
Stringaloopback = Chr(2) & Chr(3) & Chr(4)
Stringaloopback = Chr(2) + Chr(3) + Chr(4)
That's it. _________________ www.evertdekker.com Bascom code vault |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Thu Oct 25, 2007 10:00 pm Post subject: |
|
|
Your second example will not work in Bascom. The compiler will only parse two variables per statement.
Stringaloopback = Chr(2) + Chr(3) + Chr(4)
has to be written
Stringaloopback = chr(2) + chr(3) 'combine the first two
Stringaloopback = Stringaloopback + chr(4) 'add the next one on
You often need some temporary variables to get over this limitation _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
pinkfloyd11
Joined: 02 Jul 2007 Posts: 247
|
Posted: Thu Oct 25, 2007 10:19 pm Post subject: Porting CRC Function in Bascom from VB |
|
|
dear Friends,
1)
Error: type expected [string *1]
Error: type expected [string *30]
Why??
2)
Since I don't know the length of string "stringa_dati", since here there isn't string type like in VB, can I use a "big" string like String * 30 and after do a trim??
Declare Function Crc(byval Stringa_dati As String * 30) As String * 1
Function Crc(byval Stringa_dati As String * 30) As String * 1
stringa_dati=trim(stringa_dati)
........
Crc= chr(my computation)
End Function |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Thu Oct 25, 2007 11:57 pm Post subject: |
|
|
You will have to post a bit more of your code, so we can see where the errors occur.
But in general, you have to remember you are dealing with a *very* small processor by comparison with a PC. VB will allow you to define a string up to around 2 GB, essentially unlimited, for all practical purposes. RAM limitations in an AVR will never allow this. So you always have to define all your variables in advance, so the compiler can allocate space, and get a chance to warn you if you exceed the allowable RAM.
Yes of course you can define a string 'long enough' and then use only a small part of it.
One other thing, when passing strings to a Sub or Function, always pass ByRef, then you only pass the address of the string, and can use any part of it inside the Sub/Function. When you pass ByVal, the whole string needs to be put on the stack. You quickly run out of stack, even if Bascom allows it ( which I doubt ) _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
pinkfloyd11
Joined: 02 Jul 2007 Posts: 247
|
Posted: Fri Oct 26, 2007 11:20 am Post subject: Error in declare function |
|
|
Dear Friends
the error are:
Type Expected [String * 1] in line 93
Type Expected [String * 30] in line 93
And this is the code
93: Declare Function Crc(byref Stringa_dati As String * 30) As String * 1
Public Function Crc(byref Stringa_dati As String * 30) As String * 1
Dim i As Integer
Dim somma As Integer
Dim Moltiplicatore As Integer
For i = 1 To Len(Stringa_dati)
somma = somma + Asc(Mid(Stringa_dati, i, 1))
Next
If ((somma / 256) - (Int(somma / 256))) = 0 Then
CRC = Chr(0)
Else
Moltiplicatore = Int(somma / 256) + 1
CRC = Chr((256 * Moltiplicatore) - somma)
End If
End Function |
|
Back to top |
|
|
Luciano
Joined: 29 Nov 2004 Posts: 3149 Location: Italy
|
Posted: Fri Oct 26, 2007 12:20 pm Post subject: |
|
|
Hi,
Try the code below in the Bascom simulator.
Once you have done that, try to change BYVAL in BYREF.
Best regards,
Luciano
Code: | $regfile = "m48def.dat"
$crystal = 4000000
$baud = 9600
$hwstack = 32
$swstack = 16
$framesize = 40
Declare Function ColoreFrutta(Byval TipoFrutto As String) As String
Dim Frutto As String * 15
Do
Frutto = "Banana"
Print "Colore del frutto " ; Frutto ; " = " ; ColoreFrutta(Frutto)
Frutto = "Mela"
Print "Colore del frutto " ; Frutto ; " = " ; ColoreFrutta(Frutto)
Print "Colore del frutto " ; "Limone" ; " = " ; ColoreFrutta("Limone")
Loop
End
Function ColoreFrutta(Byval TipoFrutto As String) As String
If TipoFrutto = "Banana" then
ColoreFrutta = "giallo"
Endif
If TipoFrutto = "Mela" then
ColoreFrutta = "rosso giallo oppure verde"
Endif
If TipoFrutto = "Limone" then
ColoreFrutta = "giallo oppure verde"
Endif
End Function |
|
|
Back to top |
|
|
|