Skip to content

Commit

Permalink
separate package for com port wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
RoanBrand committed Jun 30, 2017
1 parent 49d8392 commit 4132680
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 76 deletions.
61 changes: 61 additions & 0 deletions comwrapper/comport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Examples of using RS-232/Virtual-Serial over USB
// as the transport layer for the protocol.

package comwrapper

import (
"github.com/RoanBrand/SerialToTCPBridgeProtocol/protocol"
"github.com/tarm/serial"
"log"
"net"
"time"
)

// Connect to a address on a tcp network, using a protocol Client.
// We connect to a Gateway over a serial connection first.
func Dial(portName string, baudRate int, address string) (net.Conn, error) {
p, err := serial.OpenPort(&serial.Config{Name: portName, Baud: baudRate})
if err != nil {
return nil, err
}
return protocol.Dial(p, address)
}

// A Protocol Gateway listening on a COM port.
type comGateway struct {
protocol.Gateway
ComConfig *serial.Config
}

func NewComPortGateway(portName string, baudRate int) *comGateway {
return &comGateway{
ComConfig: &serial.Config{Name: portName, Baud: baudRate},
}
}

// Start Gateway on a COM port interface to service single protocol Client.
func (com *comGateway) ListenAndServe() {
for {
var port *serial.Port
firstTryDone := false
// Attempt to open the COM port on the system.
for {
p, err := serial.OpenPort(com.ComConfig)
if err == nil {
port = p
break
}
if !firstTryDone {
log.Printf("Gateway @ '%s': Error opening COM port -> %v\nRetrying every 5s..\n", com.ComConfig.Name, err)
firstTryDone = true
}
time.Sleep(time.Second * 5)
}

// Open success.
log.Printf("Gateway @ '%s': Started service.\n", com.ComConfig.Name)
com.Listen(port)
log.Printf("Gateway @ '%s': Fatal error. Closing COM port\n", com.ComConfig.Name)
time.Sleep(time.Second * 2)
}
}
7 changes: 4 additions & 3 deletions example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"encoding/json"
"github.com/RoanBrand/SerialToTCPBridgeProtocol/protocol"
"github.com/RoanBrand/SerialToTCPBridgeProtocol/comwrapper"
"log"
"os"
"sync"
Expand All @@ -21,14 +21,15 @@ func main() {
for _, v := range c.Gateways {
w.Add(1)
go func(v gatewayConfig) {
com := protocol.NewComGateway(v.COMPortName, v.COMBaudRate)
com.ServeCOM()
com := comwrapper.NewComPortGateway(v.COMPortName, v.COMBaudRate)
com.ListenAndServe()
w.Done()
}(v)
}
w.Wait()
}

// Configuration by json file.
type gatewayConfig struct {
GatewayName string `json:"gateway name"`
COMPortName string `json:"comport name"`
Expand Down
65 changes: 0 additions & 65 deletions protocol/COMPortWrappers.go

This file was deleted.

File renamed without changes.
10 changes: 5 additions & 5 deletions protocol/protocolGateway.go → protocol/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
)

// Implementation of the Protocol Gateway.
type gateway struct {
type Gateway struct {
protocolTransport // Connection between Protocol Gateway & Client.
uStream net.Conn // Upstream connection to tcp Server.
}

// Initialize downstream RX and listen for a protocol Client.
func (g *gateway) Listen(ds serialInterface) {
func (g *Gateway) Listen(ds serialInterface) {
g.com = ds
g.rxBuff = make(chan byte, 512)
g.acknowledgeEvent = make(chan bool)
Expand All @@ -27,7 +27,7 @@ func (g *gateway) Listen(ds serialInterface) {
}

// Packet RX done. Handle it.
func (g *gateway) handleRxPacket(packet *Packet) {
func (g *Gateway) handleRxPacket(packet *Packet) {
switch packet.command & 0x0F {
case publish:
// Payload from serial client
Expand Down Expand Up @@ -100,7 +100,7 @@ func (g *gateway) handleRxPacket(packet *Packet) {
}

// End link session between upstream server and downstream client.
func (g *gateway) dropLink() {
func (g *Gateway) dropLink() {
if g.uStream != nil {
g.uStream.Close()
}
Expand All @@ -112,7 +112,7 @@ func (g *gateway) dropLink() {
}

// Stop activity and release downstream interface.
func (g *gateway) dropGateway() {
func (g *Gateway) dropGateway() {
g.dropLink()
g.com.Close()
close(g.rxBuff)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package protocol
package protocol_test

import (
"bytes"
"github.com/RoanBrand/SerialToTCPBridgeProtocol/protocol"
"github.com/RoanBrand/goBuffers"
"io"
"net"
Expand All @@ -23,12 +24,12 @@ func TestEcho(t *testing.T) {

// start protocol gateway server
serialTransport := NewFakeTransport()
gateway := gateway{}
gateway := protocol.Gateway{}
go gateway.Listen(&fakeTransportServerInterface{serialTransport})
t.Log("Protocol Gateway started")

// start protocol client
endClient, err := Dial(&fakeTransportClientInterface{serialTransport}, "127.0.0.1:"+strconv.Itoa(PORT))
endClient, err := protocol.Dial(&fakeTransportClientInterface{serialTransport}, "127.0.0.1:"+strconv.Itoa(PORT))
if err != nil {
t.Fatalf("Protocol client unable to connect to gateway: %v", err)
}
Expand Down

0 comments on commit 4132680

Please sign in to comment.