Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.test
intelhex/intelhex
27 changes: 27 additions & 0 deletions intelhex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# IntelHex

Decode Intel Hex file, encode data in Intel Hex format. Hex files are use to programm eeprom or microcontroller

## Usage

intelhex [command]

Available Commands:

* read Read data from hex file
* write Write data from binary file


### Read
intelhex read "path" [flags]

Flags:

--binary display binary

### Write
intelhex write "path" [flags]

Flags:

--immediate immediate string in encoded
44 changes: 44 additions & 0 deletions intelhex/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/mash/go-intelhex/intelhex/reader"
"github.com/mash/go-intelhex/intelhex/writer"
)

/* Main Command to parse
command line */
var mainCommand = &cobra.Command{
Use: "intelhex",
Short: "Intel hex parser / encoder",
Long: "Parse or encode data to intel HEX",
Run: func(cmd *cobra.Command, args []string) {
viper.AutomaticEnv()
// Application statup here
err := mainApp()
if err != nil {
fmt.Println(err)
}
},
}

/**
* The Main application really starts here
*/
func mainApp() (err error) {

return nil
}

func main() {
mainCommand.Execute()
}

func init() {
mainCommand.AddCommand(reader.MainCmd)
mainCommand.AddCommand(writer.MainCmd)
}
61 changes: 61 additions & 0 deletions intelhex/reader/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package reader

import (
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"os"
"strings"

"github.com/mash/go-intelhex"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func init() {
flags := MainCmd.Flags()

flags.Bool("binary", false, "display binary")
viper.BindPFlag("binary", flags.Lookup("binary"))
}

// MainCmd is the main command manager
var MainCmd = &cobra.Command{
Use: "read <path>",
Short: "Read data from hex file",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatal("Missing file")
}

hexFile, err := os.Open(args[0])

if err != nil {
log.Fatal(err) //log.Fatal run an os.Exit
}
defer hexFile.Close()

bytes, err := ioutil.ReadAll(hexFile)
if err != nil {
log.Fatal(err) //log.Fatal run an os.Exit
}

_, records := intelhex.ParseString(string(bytes))
for record := range records {
if viper.GetBool("binary") {
src := strings.ToLower(record.Data)
dst, err := hex.DecodeString(src)
if err != nil {
log.Fatal(err)
}

fmt.Printf("%s", dst)
} else {
fmt.Printf("%s\n", record.Data)
}

}

},
}
83 changes: 83 additions & 0 deletions intelhex/writer/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package writer

import (
"bufio"
"fmt"
"log"
"os"
"strings"
"encoding/hex"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/mash/go-intelhex"
)

func init() {
flags := MainCmd.Flags()

flags.Bool("immediate", false, "immediate string is encoded")
viper.BindPFlag("immediate", flags.Lookup("immediate"))
}

// MainCmd is the main command manager
var MainCmd = &cobra.Command{
Use: "write <path>",
Short: "Write data from binary file",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatal("Missing file/immediate")
}

if viper.GetBool("immediate") {
str := args[0]

strB := []byte(str)
encodedStr := hex.EncodeToString(strB)

record := intelhex.Record{
ByteCount : int64(len(str)),
Address : int64(0),
Type : intelhex.RecordTypeData,
Data : strings.ToUpper(encodedStr),
}

fmt.Printf("%s:00000001FF\n", string(record.Format(16)))

} else {
binFile, err := os.Open(args[0])

if err != nil {
log.Fatal(err) //log.Fatal run an os.Exit
}
defer binFile.Close()

stats, statsErr := binFile.Stat()
if statsErr != nil {
log.Fatal("Cannot read file")
}

var size int64 = stats.Size()
bytes := make([]byte, size)

bufr := bufio.NewReader(binFile)
_,err = bufr.Read(bytes)
if err != nil {
log.Fatal(err) //log.Fatal run an os.Exit
}

encodedStr := hex.EncodeToString(bytes)

record := intelhex.Record{
ByteCount : int64(size),
Address : int64(0),
Type : intelhex.RecordTypeData,
Data : strings.ToUpper(encodedStr),
}

fmt.Printf("%s:00000001FF\n", string(record.Format(16)))

}

},
}