Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Porting code from VB6 to Bascom

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
pinkfloyd11

Bascom Member



Joined: 02 Jul 2007
Posts: 248

italy.gif
PostPosted: Thu Oct 25, 2007 7:06 pm    Post subject: Porting code from VB6 to Bascom Reply with quote

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
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2111

netherlands.gif
PostPosted: Thu Oct 25, 2007 7:34 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2472
Location: Queensland

australia.gif
PostPosted: Thu Oct 25, 2007 10:00 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
pinkfloyd11

Bascom Member



Joined: 02 Jul 2007
Posts: 248

italy.gif
PostPosted: Thu Oct 25, 2007 10:19 pm    Post subject: Porting CRC Function in Bascom from VB Reply with quote

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
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2472
Location: Queensland

australia.gif
PostPosted: Thu Oct 25, 2007 11:57 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
pinkfloyd11

Bascom Member



Joined: 02 Jul 2007
Posts: 248

italy.gif
PostPosted: Fri Oct 26, 2007 11:20 am    Post subject: Error in declare function Reply with quote

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
View user's profile
Luciano

Bascom Member



Joined: 29 Nov 2004
Posts: 3149
Location: Italy

blank.gif
PostPosted: Fri Oct 26, 2007 12:20 pm    Post subject: Reply with quote

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
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive 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