代码拉取完成,页面将自动刷新
package coder
import (
"context"
"fmt"
"github.com/lishimeng/app-starter/stream"
"github.com/lishimeng/app-starter/tool"
"github.com/sigurn/crc8"
"testing"
"time"
)
func TestCrc(t *testing.T) {
var packet []byte
var crc byte
packet = []byte{0x68, 0x00, 0x00, 0x35, 0x06, 0x07, 0x04, 0x5A, 0x6B, 0x7C, 0x8D}
crc = Crc(packet)
t.Logf("%#x", crc)
if crc != 0x5B {
t.Fatal("expect 0x5B")
}
packet = []byte{0x68, 0x01, 0x07, 0x68, 0x00, 0x00, 0x30, 0x00, 0x5B, 0x16}
crc = Crc(packet)
t.Logf("%#x", crc)
if crc != 0x4E {
t.Fatal("expect 0x4E")
}
packet = []byte{0x68, 0x11, 0x00}
crc = Crc(packet)
t.Logf("%#x", crc)
if crc != 0xfe {
t.Fatal("expect 0xfe")
}
t.Logf("%#x", packet)
}
func TestCrcReuseTable(t *testing.T) {
payload1 := []byte{0x68, 0x00, 0x00, 0x30, 0x00}
payload2 := []byte{0x68, 0x11, 0x00}
var p = crc8.CRC8_DARC
p.RefOut = false // 芯片的实现,和标准DARC本处不同: reverse_out为false
table := crc8.MakeTable(p)
crc1 := crc8.Checksum(payload1, table)
crc2 := crc8.Checksum(payload2, table)
t.Logf("%x--%x", crc1, crc2)
}
func TestPacketBuilder(t *testing.T) {
var cmd byte = 0x02
var data = []byte{0x0a, 0x00, 0x00, 0x00, 0x00}
p := BuildPacket(cmd, data)
content := fmt.Sprintf("%#x", p)
expect := "0x6802050a00000000b216"
if content != expect {
t.Fatal("expect:" + expect)
}
}
func TestCoder(t *testing.T) {
stream.SetIOLog(true)
var err error
ctx, cancel := context.WithCancel(context.Background())
defer func() { cancel() }()
s, err := New(ctx, "COM5", 115200)
if err != nil {
t.Fatal(err)
}
ci, err := s.OpenAll()
if err != nil {
t.Fatal(err)
}
t.Logf("%v", ci)
time.Sleep(time.Millisecond * 500)
err = s.CloseAll()
if err != nil {
t.Fatal(err)
}
}
func TestReadUid(t *testing.T) {
stream.SetIOLog(true)
// open channel 1
// read uid+passwd
var err error
t.Logf("open client")
ctx, cancel := context.WithCancel(context.Background())
defer func() { cancel() }()
s, err := New(ctx, "COM5", 115200)
if err != nil {
t.Fatal(err)
}
t.Logf("open all")
//err = s.OpenAll()
t.Logf("channel 1 state")
ch := &Channel{}
ch.Enable(3)
ci, err := s.ChannelState(ch)
if err != nil {
t.Fatal(err)
}
t.Logf("%v", ci)
time.Sleep(time.Millisecond * 500)
t.Logf("read channel info")
uid, passwd, err := s.GetInfo()
if err != nil {
t.Fatal(err)
}
t.Logf("soc: %s[%s]", tool.BytesToHex(uid), tool.BytesToHex(passwd))
time.Sleep(time.Millisecond * 500)
t.Logf("close channels")
err = s.CloseAll()
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)
}
func TestNestedCmd(t *testing.T) {
var packet []byte
sub := BuildNestedSubPacket(NestSubCmdDelayReq, []byte{0x04, 0xf0})
nest := BuildNestedPacket(NestCmdWriteReq, sub)
packet = BuildPacket(CmdReqNested, nest)
t.Logf("%s", tool.BytesToHex(packet, " 0x"))
}
func TestEmptyBody(t *testing.T) {
var b = []byte{0x68, 0x11, 0x00, 0xfe, 0x16}
size := int(b[2])
body := b[3 : 3+size]
t.Logf("body size: %d", len(body))
t.Logf("%#x", body)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。