Skip to content

Commit 2a595b2

Browse files
committed
Explicitly log when upstream server closes tcp connection
+Update client list
1 parent e42853a commit 2a595b2

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

README.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
# Serial to TCP Bridge Protocol
22
An error tolerant serial UART to TCP connection, raw data bridge.
33

4-
PC side service written in Go that listens on COM ports for serial clients.
4+
Host side gateway service written in Go that listens on COM ports for serial clients.
55
This is meant to bridge the gap between tcp connections and serial devices using UART/RS-232/Virtual COM over USB, etc.
6-
Clients implementing the protocol have a tcp like api that they can use to make connections to real servers.
6+
Clients implementing the protocol client have a tcp like api that they can use to make connections to real servers.
77
The goal of the project is to have the means to connect the simplest and cheapest devices to the internet, albeit indirectly.
8-
For now, I use this to connect microcontrollers on development boards to servers running on localhost through the Virtual COM port over USB, without requiring any Ethernet/Wi-Fi hardware.
8+
For now, I use this to connect microcontrollers on development boards to servers running on localhost through the Virtual COM port over USB, without requiring any Ethernet/Wi-Fi hardware.
99

10-
An implementation of the Protocol Gateway and Client, written in Go, is included here.
11-
See [STM32SerialToTCPBridgeClient](https://github.com/RoanBrand/STM32SerialToTCPBridgeClient) for an example of another Client, written in c, that connects to a MQTT broker from a STM32 Nucleo F334R8 development board (ARM Cortex-M4).
10+
Included in this repo is an implementation of the Protocol **Gateway** and **Client**, written in Go. They work on Windows, Linux and even Raspbian.
11+
The following clients are also available:
12+
13+
| Client | Platform | Language |
14+
| ---------------------------------------------------------------------------------------------- |:--------:| --------:|
15+
| [ArduinoSerialToTCPBridgeClient](https://github.com/RoanBrand/ArduinoSerialToTCPBridgeClient) | Arduino | C++ |
16+
| [STM32SerialToTCPBridgeClient](https://github.com/RoanBrand/STM32SerialToTCPBridgeClient) | STM32 | C |
1217

1318
#### Use
14-
- Open a terminal, then run `go get -u github.com/RoanBrand/SerialToTCPBridgeProtocol`
15-
- Run the binary in `$GOPATH/bin`
19+
- Open a terminal, then run `go get -u github.com/RoanBrand/SerialToTCPBridgeProtocol`.
20+
- Run the binary in `$GOPATH/bin`.
1621

1722
#### Details
1823
- The protocol provides the app an in order, duplicates free and error checked byte stream by adding a CRC32 and simple retry mechanism. See [this](https://en.wikibooks.org/wiki/Serial_Programming/Error_Correction_Methods) for background.
1924
- The **Protocol Gateway** opens a real TCP connection to a set destination on behalf of the Protocol Client.
20-
- The **Protocol Client** connects to the Protocol Gateway over serial-like connection.
21-
- The Client specifies the destination IPv4 address and port.
22-
- The Gateway forwards traffic bi-directionally, as long as tcp connection is open and serial line is good.
23-
25+
- The **Protocol Client** connects to the Protocol Gateway over a serial-like connection, which can possibly corrupt data.
26+
- The client specifies the destination IPv4 address and port.
27+
- The gateway forwards traffic bi-directionally, as long as tcp connection is open and serial line is good.
2428

2529
#### Tests
26-
- Open a terminal, then run `go get -u github.com/RoanBrand/goBuffers`
27-
- In the terminal, change directory to the `protocol` folder inside the repository
28-
- Run `go test -v` in the terminal
30+
- Open a terminal, then run `go get -u github.com/RoanBrand/goBuffers`.
31+
- In the terminal, change directory to the `protocol` folder inside the repository.
32+
- Run `go test -v` in the terminal.
2933

3034
#### Future plans
3135
- Add config. Turn into OS service.
3236
- Add ping option to periodically test serial line and drop upstream connection if timeout.
3337
- Multiple connections per client to servers.
3438
- Capability to scan system and listen on all found COM ports for clients.
35-
- Create a Arduino lib/client that extends the [Arduino Client class](https://www.arduino.cc/en/Reference/ClientConstructor) so that libraries for existing Ethernet/Wi-Fi shields can theoretically work.

example.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
)
88

99
const (
10-
COMPortName = "COM3"
10+
COMPortName = "COM4" // Windows
11+
//COMPortName = "/dev/serial0" // Linux
1112
COMBaudRate = 115200
1213
)
1314

protocol/protocolGateway.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ func (g *gateway) handleRxPacket(packet *Packet) {
7979
go g.packetSender(func() (p Packet, err error) {
8080
// Publish data downstream received from upstream tcp server.
8181
n, err := g.uStream.Read(tx)
82-
p = Packet{command: publish, payload: tx[:n]}
82+
if err != nil {
83+
if err.Error() == "EOF" {
84+
log.Println("Upstream TCP server closed connection unexpectedly")
85+
}
86+
} else {
87+
p = Packet{command: publish, payload: tx[:n]}
88+
}
8389
return
8490
}, g.dropLink)
8591
g.state = Connected

protocol/transport.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ func (t *protocolTransport) rxSerial(onReadFail func()) {
5151
return
5252
}
5353
/*
54+
// For debugging
5455
logLock.Lock()
5556
log.Println("Gateway RX:")
5657
log.Println(rx[:nRx])
57-
log.Println(string(rx[2:nRx]))
58+
log.Println(string(rx[:nRx]))
5859
logLock.Unlock()
5960
*/
6061
for _, v := range rx[:nRx] {
@@ -80,6 +81,7 @@ func (t *protocolTransport) txSerial(onWriteFail func()) {
8081
return
8182
}
8283
/*
84+
// For debugging
8385
logLock.Lock()
8486
log.Println("Gateway TX:")
8587
log.Println(serialPacket)

0 commit comments

Comments
 (0)