-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
shenyuguang
committed
Sep 15, 2023
1 parent
8a53d32
commit 335f7aa
Showing
17 changed files
with
386 additions
and
168 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,131 @@ | ||
package commu | ||
|
||
import _ "github.com/akingbrDu/iotbase/core" | ||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/tarm/serial" | ||
"sync" | ||
) | ||
|
||
type ICommunicator interface { | ||
Send(cmd InstrCommand) error | ||
var ( | ||
cuFactoryInstance *CommunicatorFactory | ||
recLock sync.Mutex | ||
) | ||
|
||
//const ( | ||
// Serial = "serial" | ||
// TcpClient = "tcpclient" | ||
// TcpServer = "tcpserver" | ||
//) | ||
|
||
//type ICommunicator interface { | ||
// Start() error | ||
// Close() error | ||
//} | ||
|
||
const ( | ||
SerialPortName = "port" | ||
SeralBaudRate = "baudrate" | ||
SerialDataBit = "databit" | ||
SerialStopBit = "stopbit" | ||
SerialParity = "parity" | ||
) | ||
|
||
type CommunicatorFactory struct { | ||
} | ||
|
||
func (factory CommunicatorFactory) CreateTcpServerCommunicator(param map[string]interface{}) (*TcpServerCommunicator, error) { | ||
return nil, nil | ||
} | ||
|
||
func (factory CommunicatorFactory) CreateTcpClientCommunicator(param map[string]interface{}) (*TcpClientCommunicator, error) { | ||
return nil, nil | ||
} | ||
|
||
func (factory CommunicatorFactory) CreateSerialCommunicator(param map[string]interface{}) (*SerialCommunicator, error) { | ||
portName, ok := param[SerialPortName].(string) | ||
if !ok { | ||
fmt.Println("can't parse port field in param information") | ||
return nil, errors.New("can't parse port field in param information") | ||
} | ||
|
||
baudRate, ok := param[SeralBaudRate].(int) | ||
if !ok { | ||
fmt.Println("can't parse baudrate field in param information") | ||
return nil, errors.New("can't parse baudrate field in param information") | ||
} | ||
|
||
dataBits, ok := param[SerialDataBit].(byte) | ||
if !ok { | ||
fmt.Println("can't parse databit field in param information") | ||
return nil, errors.New("can't parse databit field in param information") | ||
} | ||
|
||
stopBits, ok := param[SerialStopBit].(int) | ||
if !ok { | ||
fmt.Println("can't parse stopbit field in param information") | ||
return nil, errors.New("can't parse stopbit field in param information") | ||
} | ||
|
||
sb, err := factory.ConvertStopBits(stopBits) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
parity, ok := param[SerialParity].(int) | ||
if !ok { | ||
fmt.Println("can't parse parity field in param information") | ||
return nil, errors.New("can't parse parity field in param information") | ||
} | ||
|
||
pr, err := factory.ConvertParity(parity) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &SerialCommunicator{ | ||
portName: portName, | ||
baudRate: baudRate, | ||
dataBits: dataBits, | ||
stopBits: sb, | ||
parity: pr, | ||
}, nil | ||
} | ||
|
||
func (factory CommunicatorFactory) ConvertStopBits(stopBits int) (serial.StopBits, error) { | ||
switch stopBits { | ||
case 1: | ||
return serial.Stop1, nil | ||
case 2: | ||
return serial.Stop2, nil | ||
default: | ||
return serial.Stop1, errors.New("unsupported stop bit setting") | ||
} | ||
} | ||
|
||
func (factory CommunicatorFactory) ConvertParity(parity int) (serial.Parity, error) { | ||
switch parity { | ||
case 0: | ||
return serial.ParityNone, nil | ||
case 1: | ||
return serial.ParityOdd, nil | ||
case 2: | ||
return serial.ParityEven, nil | ||
case 3: | ||
return serial.ParityMark, nil | ||
case 4: | ||
return serial.ParitySpace, nil | ||
default: | ||
return serial.ParityNone, errors.New("unsupported parity setting") | ||
} | ||
} | ||
|
||
func GetCommunicatorFactory() *CommunicatorFactory { | ||
recLock.Lock() | ||
defer recLock.Unlock() | ||
|
||
if cuFactoryInstance == nil { | ||
cuFactoryInstance = &CommunicatorFactory{} | ||
} | ||
return cuFactoryInstance | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package commu | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/tarm/serial" | ||
) | ||
|
||
type SerialCommunicator struct { | ||
portName string | ||
baudRate int | ||
dataBits byte | ||
stopBits serial.StopBits | ||
parity serial.Parity | ||
|
||
io *serial.Port | ||
} | ||
|
||
func (s *SerialCommunicator) Connect() error { | ||
config := &serial.Config{ | ||
Name: s.portName, // 串口设备名称 | ||
Baud: s.baudRate, // 波特率 | ||
Size: s.dataBits, // 数据位 | ||
StopBits: s.stopBits, | ||
Parity: s.parity, | ||
} | ||
|
||
port, err := serial.OpenPort(config) | ||
if err != nil { | ||
fmt.Println("fail to open serial port with err: ", err) | ||
return errors.New(fmt.Sprintf("fail to open serial port with err: %v", err)) | ||
} | ||
|
||
fmt.Println("success to open serial port: ", s.portName) | ||
s.io = port | ||
return nil | ||
} | ||
|
||
func (s *SerialCommunicator) Send(data []byte) (int, error) { | ||
if s.io == nil { | ||
fmt.Println("serial port is not opened") | ||
return 0, errors.New("serial port is not opened") | ||
} | ||
|
||
byteLen, err := s.io.Write(data) | ||
if err != nil { | ||
fmt.Println("fail to write serial port with err: ", err) | ||
return byteLen, errors.New(fmt.Sprintf("fail to write serial port with err: %v", err)) | ||
} else { | ||
return byteLen, nil | ||
} | ||
} | ||
|
||
func (s *SerialCommunicator) Receive(data []byte) (int, error) { | ||
if s.io == nil { | ||
fmt.Println("serial port is not opened") | ||
return 0, errors.New("serial port is not opened") | ||
} | ||
|
||
byteLen, err := s.io.Read(data) | ||
if err != nil { | ||
fmt.Println("fail to read serial port with err: ", err) | ||
return byteLen, errors.New(fmt.Sprintf("fail to read serial port with err: %v", err)) | ||
} else { | ||
return byteLen, nil | ||
} | ||
} | ||
|
||
func (s *SerialCommunicator) Close() error { | ||
if s.io != nil { | ||
err := s.io.Close() | ||
if err != nil { | ||
fmt.Println("fail to close serial port with err: ", err) | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package commu | ||
|
||
type TcpClientCommunicator struct { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package commu | ||
|
||
type TcpServerCommunicator struct { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package core | ||
|
||
type IDriver interface { | ||
// Init * | ||
//@brief: init | ||
// * @param: configJson: 配置参数 | ||
// * @param: modelJson: 物模型参数 | ||
// * @return: 错误码 | ||
Init(device Device, configJson string, modelJson string, handler IEventHandler) error | ||
|
||
// Run * | ||
// 驱动启动运行 | ||
Run() error | ||
|
||
// ExeCommand * | ||
//@brief: execute command | ||
// * @param: cmd: 命令结构 | ||
// * @return: 错误码 | ||
ExeCommand(cmd Command) error | ||
|
||
// SetOption * | ||
//@brief: set option | ||
// * @param: key: 键 | ||
// * @param: value: 值 | ||
// * @return: 错误码 | ||
SetOption(key string, value string) error | ||
} | ||
|
||
type IQueryDriver interface { | ||
IDriver | ||
IQuery | ||
} | ||
|
||
type ICallbackDriver interface { | ||
IDriver | ||
ICallback | ||
} | ||
|
||
type IQueryAndCallbackDriver interface { | ||
IDriver | ||
IQuery | ||
ICallback | ||
} | ||
|
||
// IInitHook 初始化解析钩子 | ||
type IInitHook interface { | ||
// OnBefInitialize *初始化解析参数前的钩子 | ||
OnBefInitialize() error | ||
// OnInitialized *初始化解析参数后的钩子 | ||
OnInitialized() error | ||
} |
Oops, something went wrong.