BxCmdFileBitmap.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package bx
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. )
  7. type bitmapFile struct {
  8. FileType byte //0x04
  9. FileName string //4byte Txxx
  10. FileLen uint32
  11. LibData []byte
  12. CHK uint16
  13. }
  14. func NewBitmapFile(filename string, libdata []byte) bitmapFile {
  15. chk := CRC16(libdata, 0, len(libdata))
  16. fmt.Printf("位图文件校验:% 02x\n", chk)
  17. return bitmapFile{
  18. FileType: 0x04,
  19. FileName: filename,
  20. FileLen: uint32(len(libdata)),
  21. LibData: libdata,
  22. CHK: chk,
  23. }
  24. }
  25. func (bf *bitmapFile) NewCmd() *CmdWriteBitmapFile {
  26. return &CmdWriteBitmapFile{
  27. baseBxCmd: newBaseCmd(CMD_WRITE_FILE.group, CMD_WRITE_FILE.code),
  28. file: bf,
  29. LastBlockFlag: 1,
  30. }
  31. }
  32. type CmdWriteBitmapFile struct {
  33. baseBxCmd
  34. state byte
  35. file *bitmapFile
  36. LastBlockFlag byte
  37. BlockNum uint16 //包号,如果是单包发送,则默认为 0x00。
  38. BlockLen uint16 //包长,若是单包发送,此处为文件长度。
  39. BlockAddr uint32 //本包数据在文件中的起始位置,如果是单包发送,此处默认为 0。
  40. temp []byte
  41. }
  42. func (cmd *CmdWriteBitmapFile) Build() []byte {
  43. if cmd.state == 0 {
  44. //Write File
  45. w1 := bytes.NewBuffer(make([]byte, 0, 1024))
  46. //文件描述数据
  47. binary.Write(w1, binary.LittleEndian, cmd.Group())
  48. binary.Write(w1, binary.LittleEndian, cmd.Cmd())
  49. binary.Write(w1, binary.LittleEndian, byte(0x01))
  50. binary.Write(w1, binary.LittleEndian, []byte{0x00, 0x00})
  51. binary.Write(w1, binary.BigEndian, []byte(cmd.file.FileName))
  52. binary.Write(w1, binary.LittleEndian, cmd.LastBlockFlag) //是否是最后一包,0x00——不是最后一包 0x01——最后一包。
  53. binary.Write(w1, binary.LittleEndian, cmd.BlockNum) //包号,单包为0x00
  54. binary.Write(w1, binary.LittleEndian, cmd.file.FileLen) //包长,若是单包发送,此处为文件长度。
  55. binary.Write(w1, binary.LittleEndian, cmd.BlockAddr) //本包数据在文件中的偏移量,单包为0x00
  56. //文件内容数据
  57. w2 := bytes.NewBuffer(make([]byte, 0, 1024))
  58. binary.Write(w2, binary.LittleEndian, cmd.file.FileType)
  59. binary.Write(w2, binary.BigEndian, []byte(cmd.file.FileName))
  60. binary.Write(w2, binary.LittleEndian, cmd.file.FileLen)
  61. binary.Write(w2, binary.BigEndian, cmd.file.LibData)
  62. data := w2.Bytes()
  63. crc := CRC16(data, 0, w2.Len())
  64. binary.Write(w1, binary.BigEndian, data)
  65. binary.Write(w1, binary.LittleEndian, crc)
  66. cmd.temp = w1.Bytes()
  67. //Start Write File "开始写文件",写文件前先检查内存是否够用
  68. w3 := bytes.NewBuffer(make([]byte, 0, 64))
  69. binary.Write(w3, binary.LittleEndian, CMD_START_WRITE_FILE.group)
  70. binary.Write(w3, binary.LittleEndian, CMD_START_WRITE_FILE.code)
  71. binary.Write(w3, binary.LittleEndian, byte(0x01))
  72. binary.Write(w3, binary.LittleEndian, []byte{0x00, 0x00})
  73. binary.Write(w3, binary.LittleEndian, byte(0x01)) //同名是否覆盖,0不覆盖,1覆盖
  74. binary.Write(w3, binary.BigEndian, []byte(cmd.file.FileName))
  75. binary.Write(w3, binary.LittleEndian, cmd.file.FileLen)
  76. cmd.state = 1
  77. return w3.Bytes()
  78. } else {
  79. return cmd.temp
  80. }
  81. }