예제 #1
0
func (c *SerialController) OpenSerial() {
	err := comm.OpenSerial()
	var result string
	if err != nil {
		result = err.Error()
	} else {
		result = "ok"
	}
	c.Ctx.WriteString(result)
}
예제 #2
0
func Test_concurrency() {
	err := comm.OpenSerial()

	if err != nil {
		log.Fatal("Test_concurrency:", err)
	}
	var wg sync.WaitGroup
	wg.Add(len(funclist))

	go generator(time.Duration(200))
	go writer(time.Duration(1))
	go reader(time.Duration(1))
	go worker(time.Duration(1))
	//	go done(time.Duration(1000000), &wg)

	wg.Wait()

}
예제 #3
0
func Test_GetSensorData() {
	//	var serial comm.Serial
	err := comm.OpenSerial()

	if err != nil {
		log.Println(err.Error())
	}
	var sessionkey []byte = []byte{0x46, 0x46}
	var sequence byte = 0x30
	for i := 0; ; i++ {

		if i == 0 {
			var r ftprotocol.RequestSession
			r.DeviceID = 0xD8
			r.ProtocolVersion = 0x2729
			r.SessionKey = sessionkey
			r.Sequence = sequence

			r.NoAck = true
			log.Printf("send requestsession : %X", r.Message())

			comm.Writer(r.Message())

			res, _ := comm.Reader()
			log.Printf("received requestsession:%X", res)
			if len(res) > 13 {
				var s ftprotocol.RequestSessionResponse
				s.Parse(res[2:])
				sessionkey = s.SessionKey
				sequence = s.Sequence
				log.Println("session status:", s.SessionStatus)
				log.Println("device id:", s.DeviceID)
				log.Println("protocol version:", s.ProtocolVersion)
			}

		}
		var k ftprotocol.KeepAlive
		k.SessionKey = sessionkey
		k.Sequence = sequence
		log.Printf("send keepalive : %X", k.Message())
		comm.Writer(k.Message())

		res, _ := comm.Reader()
		log.Printf("received keepalive:%X", res)
		if len(res) > 1 {
			if res[0] == 0x06 {
				sequence = res[1]
			}
		}
		sequence = IncreaseSeq(sequence)

		if i == 10 {
			var s ftprotocol.GetSensor
			s.SessionKey = sessionkey
			s.Sequence = sequence
			s.IsBroadcast = true
			s.Broadcastperiod = 10
			s.IsAllSensorData = true
			log.Printf("send GetSensor : %X", s.Message())
			comm.Writer(s.Message())

			res, _ := comm.Reader()
			log.Printf("received GetSensor:%X", res)
			sequence = IncreaseSeq(sequence)
		}

		if i == 20 {
			var s ftprotocol.GetSensor
			s.SessionKey = sessionkey
			s.Sequence = sequence
			s.IsBroadcast = false
			s.Broadcastperiod = 10
			s.IsAllSensorData = true
			log.Printf("send GetSensor : %X", s.Message())
			comm.Writer(s.Message())

			res, _ := comm.Reader()
			log.Printf("received GetSensor:%X", res)
			sequence = IncreaseSeq(sequence)

		}

	}

}