Skip to content

Commit

Permalink
Merge pull request euantorano#8 from euantorano/feature/easier-readin…
Browse files Browse the repository at this point in the history
…g-and-writing

Easier reading and writing
  • Loading branch information
euantorano committed Jun 26, 2018
2 parents 22a076e + c0faaa8 commit 9b15626
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ port.baudRate = 2400
var receiveBuffer = newString(1024)
while true:
let numReceived = port.read(addr receiveBuffer[0], int32 len(receiveBuffer))
let numReceived = port.read(receiveBuffer)
discard port.write(addr receiveBuffer[0], numReceived)
```

Expand Down
4 changes: 2 additions & 2 deletions src/serial/private/serialport/serialport_posix.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ var
TIOCMBIS {.importc, header: "<termios.h>".}: cint

type
SerialPortBase[THandle] = ref object of RootObj
SerialPortBase[HandleType] = ref object of RootObj
name*: string
handshake: Handshake
handle: THandle
handle: HandleType
readTimeout: int32
writeTimeout: int32

Expand Down
8 changes: 4 additions & 4 deletions src/serial/private/serialport/serialport_windows.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const
FileTemplateHandle: Handle = Handle(0)

type
SerialPortBase[THandle] = ref object of RootObj
SerialPortBase[HandleType] = ref object of RootObj
name*: string
handshake: Handshake
handle: THandle
handle: HandleType
commProp: CommProp
comStat: ComStat
dcb: DCB
Expand Down Expand Up @@ -491,14 +491,14 @@ proc initPort(port: SerialPort, tempHandle: Handle, baudRate: int32, parity: Par

if GetCommTimeouts(port.handle, addr port.commTimeouts) == 0:
raiseOSError(osLastError())

port.setTimeouts(readTimeout, writeTimeout)

discard SetCommMask(port.handle, ALL_EVENTS)
except:
discard closeHandle(tempHandle)
port.handle = InvalidFileHandle

raise

proc open*(port: SerialPort, baudRate: int32, parity: Parity, dataBits: byte, stopBits: StopBits,
Expand Down
14 changes: 14 additions & 0 deletions src/serial/serialport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,17 @@ elif defined(posix):
include ./private/serialport/serialport_posix
else:
{.error: "Serial port handling not implemented for your platform".}

proc read*(port: SerialPort, buff: var string): int32 =
## Read from the serial port into the buffer `buff`. This will return the actual number of bytes that were received.
if isNil(buff) or len(buff) == 0:
return 0

result = port.read(addr buff[0], int32(len(buff)))

proc write*(port: SerialPort, buff: var string): int32 =
## Write the data to the serialport `port` from the buffer `buff`. This will return the number of bytes that were written.
if isNil(buff) or len(buff) == 0:
return 0

result = port.write(addr buff[0], int32(len(buff)))

0 comments on commit 9b15626

Please sign in to comment.