Bascom code to arduino code

Someone has given me some BASCOM code so that my mega1280 can talk to my solar inverter. For some unknown reason the bascom program refuses to program the arduino. After 20+ hours of trying various solutions its time to give it the finger.

Is there any way to convert bascom to arduino? Or is it a manual job?

Here is the code:

'Interface between MPP solar products and PC terminal

$regfile = “m2560def.dat” ’ specify the used micro

$crystal = 16000000 ’ used crystal frequency
'$baud = 57600 ’ use baud rate
$hwstack = 50
$swstack = 50
$framesize = 50

Dim Alen As Byte
Dim Onlongs(17) As Long 'numbers to sort Long, Word, Byte as needed
'Dim X As Byte

Dim Crc_16 As String * 2
Dim Work2 As Word At Crc_16 Overlay
Dim Work(35) As String * 40
Dim Thecrc As Word
Dim Rs232 As Byte
Dim Uart_command_in As String * 50
Dim Uart_command As String * 254
Dim Aruart_command(253) As Byte At Uart_command Overlay
Dim Send_command As String * 50
Dim Send_bytes(49) As Byte At Send_command Overlay
Dim Crcstr As String * 2
Dim Crcwrd As Word At Crcstr Overlay
Dim Z As Byte
Dim Task As Byte

Declare Sub Chopup
Declare Sub Find_command
Declare Sub Cal_crc16

Config Com1 = 57600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

Config Com2 = 2400 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

Open “com2:” For Binary As #1

Do
Task = 0
Do
If Ischarwaiting() = 1 Then
Rs232 = Inkey() 'get a character from the buffer

If Rs232 = 13 Or Len(uart_command_in) > 49 Then 'uart_command full or enter key sent

Print
Uart_command_in = Ucase(uart_command_in) 'make uper case

Exit Do

Else
Uart_command_in = Uart_command_in + Chr(rs232) 'add the character to the uart_command buffer
Print Chr(rs232) ;
End If
End If

Loop

Select Case Uart_command_in

Case “INFO” 'get the state of inverter
Uart_command_in = “QPIGS”
Task = 1

End Select

Send_command = Uart_command_in
Uart_command_in = “”

Z = Len(send_command)

Thecrc = Crc16(send_bytes() , Z) 'workout the CRC
Swap Thecrc
'Print Hex(thecrc)
Crcwrd = Thecrc

Print #1 , Send_command ; Crcstr ; Chr(13) ;

Do
If Ischarwaiting(#1) = 1 Then
Rs232 = Inkey(#1) 'get a character from the buffer

If Rs232 = 13 Or Len(uart_command) > 253 Then 'uart_command full or enter key sent
Delchars Uart_command ,(13)
Call Find_command

Print “RETURNED DATA>” ; Uart_command
Uart_command = “”
Exit Do
Else
Uart_command = Uart_command + Chr(rs232) 'add the character to the uart_command buffer

End If
End If

Loop

Loop

End 'end program

Sub Find_command
Local A As String * 3
Local B As Byte
Local C As Byte
Local D As Byte
Local E As Long
C = &B00000111

Call Chopup
Select Case Task
Print “Status bits>” ; Work(17)
Case 1 'get the state of inverter
Delchar Work(1) , 1 'remove the (
'work(17) staus bits

Print “>>Inverter Stats<<”
Print “Grid Volts=” ; Work(1)
Print “Grid Frequency=” ; Work(2)
Print “AC Output Volts=” ; Work(3)
Print “AC Output Frequency” ; Work(4)
Print “Apparent Power=” ; Work(5)
Print “Active Power=” ; Work(6)
Print “Load=” ; Work(7) ; “%”
Print “Buss Volts=” ; Work(8)
Print “Battery Volts=” ; Work(9)
Print “Battery Current=” ; Work(10) ; “Amps”
Print “Battery Capacity=” ; Work(11) ; “%”
Print “Inverter Heat Sink Temp=” ; Work(12) ; “C”
Print “PV Current into Battery=” ; Work(13) ; “Amps”
Print “PV Voltage=” ; Work(14) ; “Volts”
Print “Solar Charger Battery Voltage=” ; Work(15) ; “Volts”
Print “Battery Discharge Current=” ; Work(16) ; “Amps”
A = Mid(work(17) , 1)
If A = “1” Then
Print “SBU Priority=Yes”
Else
Print “SBU Priority=No”
End If
A = Mid(work(17) , 2)
If A = “1” Then
Print “Configuration Change”
Else
Print “Configuration No Change”
End If
A = Mid(work(17) , 3)
If A = “1” Then
Print “Solar Charger Firmware Updated”
Else
Print “Solar Charger Firmware Not Updated”
End If
A = Mid(work(17) , 4)
If A = “1” Then
Print “Load is On”
Else
Print “Load is Off”
End If

A = Mid(work(17) , 6 , 3)
D = Binval(a)
Select Case D

Case 0

Print “No charging On”

Case 6

Print “Solar charging On”

Case 5

Print “AC charging On”

Case 7

Print “Solar & AC Charging On”

End Select
End Select
Call Cal_crc16

End Sub

'get the individual parameters returned
Sub Chopup

Local Lx As Byte

Lx = Split(uart_command , Work(1) , " ")
'Print “we had " ; Lx ; " lots of data returned”

End Sub

Sub Cal_crc16
Local Z As Word
Local Rcrc As Word

Z = Len(uart_command)
Z = Z - 2 'last two bytes are crc
'Print “Z is>” ; Z
Rcrc = Crc16(aruart_command() , Z) 'calculate the CRC
Swap Rcrc 'bytes are in reverse order to what is sent to us
Print “Cal CRC is>” ; Hex(rcrc)
Crc_16 = Right(uart_command , 2) 'get the last two bytes the crc
Rcrc = Work2 'this is a word variable overlayed on Crc_16

Print “The sent crc>” ; Hex(rcrc)

End Sub

For some unknown reason the bascom program refuses to program the arduino.

That will be because the Arduino uses C++ rather than Bascom

Or is it a manual job?

Yes, but do not try to do it statement by statement. Work out the logic and inputs/outputs required then write the code from scratch.

I think you might be better served figuring out how to get BASCOM to program the Arduino board, or to find some other way to get the compiled BASCOM code onto the Arduino. It will be the easiest way from where you are to where you want to be.

RChadwick: I think you might be better served figuring out how to get BASCOM to program the Arduino board, or to find some other way to get the compiled BASCOM code onto the Arduino. It will be the easiest way from where you are to where you want to be.

Doesn't really seem that likely, does it?

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your code. (This assumes C++ code, not BASCOM code!)
  2. Paste the autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

Good Luck!

The original code probably did not have emoticons.

Northland:
Someone has given me some BASCOM code so that my mega1280 can talk to my solar inverter. For some unknown reason the bascom program refuses to program the arduino. After 20+ hours of trying various solutions its time to give it the finger.

Is there any way to convert bascom to arduino? Or is it a manual job?
<…>

There seems to be an old BASCOM-AVR IDE out in the wild. Some info:

-and-

http://avrhelp.mcselec.com/index.html?arduino.htm

Please note that the links above are aged… likely you will require downloading code meant for Win7. Successful use with more modern tools and hardware is unknown.

Ray

To be honest that BASIC code looks like it would readily translate to C/C++. Most of the functions have a direct equivalent in the 'Arduino world'. Debugging and verifying it works correctly would be hard without the device attached, though.