Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
agent
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
7
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e4646076
authored
Sep 10, 2014
by
Guba Sándor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
replace line endings anf fix reading bug
parent
5e32d6a6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
138 additions
and
137 deletions
+138
-137
w32serial.py
+138
-137
No files found.
w32serial.py
View file @
e4646076
# Copyright (c) Twisted Matrix Laboratories.
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
# See LICENSE for details.
"""
"""
Serial port support for Windows.
Serial port support for Windows.
Requires PySerial and pywin32.
Requires PySerial and pywin32.
"""
"""
# system imports
# system imports
import
win32file
import
win32file
import
win32event
import
win32event
import
win32con
import
win32con
# twisted imports
# twisted imports
from
twisted.internet
import
abstract
from
twisted.internet
import
abstract
# sibling imports
# sibling imports
import
logging
import
logging
logger
=
logging
.
getLogger
()
logger
=
logging
.
getLogger
()
class
SerialPort
(
abstract
.
FileDescriptor
):
class
SerialPort
(
abstract
.
FileDescriptor
):
"""A serial device, acting as a transport, that uses a win32 event."""
"""A serial device, acting as a transport, that uses a win32 event."""
connected
=
1
connected
=
1
def
__init__
(
self
,
protocol
,
deviceNameOrPortNumber
,
reactor
):
def
__init__
(
self
,
protocol
,
deviceNameOrPortNumber
,
reactor
):
#self._serialFactory = VirtioSerial
self
.
hComPort
=
win32file
.
CreateFile
(
#self._serial = self._serialFactory(port=deviceNameOrPortNumber)
deviceNameOrPortNumber
,
self
.
hComPort
=
win32file
.
CreateFile
(
deviceNameOrPortNumber
,
win32con
.
GENERIC_READ
|
win32con
.
GENERIC_WRITE
,
win32con
.
GENERIC_READ
|
win32con
.
GENERIC_WRITE
,
0
,
# exclusive access
0
,
# exclusive access
None
,
# no security
None
,
# no security
win32con
.
OPEN_EXISTING
,
win32con
.
OPEN_EXISTING
,
win32con
.
FILE_ATTRIBUTE_NORMAL
|
win32con
.
FILE_FLAG_OVERLAPPED
,
win32con
.
FILE_ATTRIBUTE_NORMAL
|
win32con
.
FILE_FLAG_OVERLAPPED
,
0
)
0
)
self
.
reactor
=
reactor
self
.
reactor
=
reactor
self
.
protocol
=
protocol
self
.
protocol
=
protocol
self
.
outQueue
=
[]
self
.
outQueue
=
[]
self
.
closed
=
0
self
.
closed
=
0
self
.
closedNotifies
=
0
self
.
closedNotifies
=
0
self
.
writeInProgress
=
0
self
.
writeInProgress
=
0
self
.
protocol
=
protocol
self
.
protocol
=
protocol
self
.
_overlappedRead
=
win32file
.
OVERLAPPED
()
self
.
_overlappedRead
=
win32file
.
OVERLAPPED
()
self
.
_overlappedRead
.
hEvent
=
win32event
.
CreateEvent
(
None
,
1
,
0
,
None
)
self
.
_overlappedRead
.
hEvent
=
win32event
.
CreateEvent
(
None
,
1
,
0
,
None
)
self
.
_overlappedWrite
=
win32file
.
OVERLAPPED
()
self
.
_overlappedWrite
=
win32file
.
OVERLAPPED
()
self
.
_overlappedWrite
.
hEvent
=
win32event
.
CreateEvent
(
None
,
0
,
0
,
None
)
self
.
_overlappedWrite
.
hEvent
=
win32event
.
CreateEvent
(
None
,
0
,
0
,
None
)
self
.
reactor
.
addEvent
(
self
.
reactor
.
addEvent
(
self
.
_overlappedRead
.
hEvent
,
self
.
_overlappedRead
.
hEvent
,
self
,
self
,
'serialReadEvent'
)
'serialReadEvent'
)
self
.
reactor
.
addEvent
(
self
.
reactor
.
addEvent
(
self
.
_overlappedWrite
.
hEvent
,
self
.
_overlappedWrite
.
hEvent
,
self
,
self
,
'serialWriteEvent'
)
'serialWriteEvent'
)
self
.
protocol
.
makeConnection
(
self
)
self
.
protocol
.
makeConnection
(
self
)
self
.
_finishPortSetup
()
self
.
_finishPortSetup
()
def
_finishPortSetup
(
self
):
def
_finishPortSetup
(
self
):
"""
"""
Finish setting up the serial port.
Finish setting up the serial port.
This is a separate method to facilitate testing.
This is a separate method to facilitate testing.
"""
"""
rc
,
self
.
read_buf
=
win32file
.
ReadFile
(
self
.
hComPort
,
rc
,
self
.
read_buf
=
win32file
.
ReadFile
(
self
.
hComPort
,
win32file
.
AllocateReadBuffer
(
1
),
win32file
.
AllocateReadBuffer
(
1
),
self
.
_overlappedRead
)
self
.
_overlappedRead
)
def
serialReadEvent
(
self
):
def
serialReadEvent
(
self
):
# get that character we set up
# get that character we set up
try
:
try
:
n
=
win32file
.
GetOverlappedResult
(
n
=
win32file
.
GetOverlappedResult
(
self
.
hComPort
,
self
.
hComPort
,
self
.
_overlappedRead
,
self
.
_overlappedRead
,
0
)
0
)
except
:
except
:
import
time
n
=
0
time
.
sleep
(
10
)
if
n
:
n
=
0
first
=
str
(
self
.
read_buf
[:
n
])
if
n
:
# now we should get everything that is already in the buffer
first
=
str
(
self
.
read_buf
[:
n
])
win32event
.
ResetEvent
(
self
.
_overlappedRead
.
hEvent
)
# now we should get everything that is already in the buffer (max
rc
,
buf
=
win32file
.
ReadFile
(
self
.
hComPort
,
# 4096)
win32file
.
AllocateReadBuffer
(
n
),
win32event
.
ResetEvent
(
self
.
_overlappedRead
.
hEvent
)
self
.
_overlappedRead
)
rc
,
buf
=
win32file
.
ReadFile
(
self
.
hComPort
,
n
=
win32file
.
GetOverlappedResult
(
win32file
.
AllocateReadBuffer
(
4096
),
self
.
hComPort
,
self
.
_overlappedRead
)
self
.
_overlappedRead
,
n
=
win32file
.
GetOverlappedResult
(
1
)
self
.
hComPort
,
# handle all the received data:
self
.
_overlappedRead
,
self
.
protocol
.
dataReceived
(
first
+
str
(
buf
[:
n
]))
1
)
# handle all the received data:
# set up next one
self
.
protocol
.
dataReceived
(
first
+
str
(
buf
[:
n
]))
win32event
.
ResetEvent
(
self
.
_overlappedRead
.
hEvent
)
# set up next one
# rc, self.read_buf = win32file.ReadFile(self.hComPort,
win32event
.
ResetEvent
(
self
.
_overlappedRead
.
hEvent
)
# win32file.AllocateReadBuffer(1),
rc
,
self
.
read_buf
=
win32file
.
ReadFile
(
self
.
hComPort
,
# self._overlappedRead)
win32file
.
AllocateReadBuffer
(
1
),
self
.
_overlappedRead
)
def
write
(
self
,
data
):
if
data
:
def
write
(
self
,
data
):
if
self
.
writeInProgress
:
if
data
:
self
.
outQueue
.
append
(
data
)
if
self
.
writeInProgress
:
logger
.
debug
(
"added to queue"
)
self
.
outQueue
.
append
(
data
)
else
:
logger
.
debug
(
"added to queue"
)
self
.
writeInProgress
=
1
else
:
win32file
.
WriteFile
(
self
.
hComPort
,
data
,
self
.
_overlappedWrite
)
self
.
writeInProgress
=
1
logger
.
debug
(
"Writed to file"
)
win32file
.
WriteFile
(
self
.
hComPort
,
data
,
self
.
_overlappedWrite
)
logger
.
debug
(
"Writed to file"
)
def
serialWriteEvent
(
self
):
try
:
def
serialWriteEvent
(
self
):
dataToWrite
=
self
.
outQueue
.
pop
(
0
)
try
:
except
IndexError
:
dataToWrite
=
self
.
outQueue
.
pop
(
0
)
self
.
writeInProgress
=
0
except
IndexError
:
return
self
.
writeInProgress
=
0
else
:
return
win32file
.
WriteFile
(
else
:
self
.
hComPort
,
win32file
.
WriteFile
(
dataToWrite
,
self
.
hComPort
,
self
.
_overlappedWrite
)
dataToWrite
,
self
.
_overlappedWrite
)
def
connectionLost
(
self
,
reason
):
"""
def
connectionLost
(
self
,
reason
):
Called when the serial port disconnects.
"""
Called when the serial port disconnects.
Will call C{connectionLost} on the protocol that is handling the
serial data.
Will call C{connectionLost} on the protocol that is handling the
"""
serial data.
self
.
reactor
.
removeEvent
(
self
.
_overlappedRead
.
hEvent
)
"""
self
.
reactor
.
removeEvent
(
self
.
_overlappedWrite
.
hEvent
)
self
.
reactor
.
removeEvent
(
self
.
_overlappedRead
.
hEvent
)
abstract
.
FileDescriptor
.
connectionLost
(
self
,
reason
)
self
.
reactor
.
removeEvent
(
self
.
_overlappedWrite
.
hEvent
)
win32file
.
CloseHandle
(
self
.
hComPort
)
abstract
.
FileDescriptor
.
connectionLost
(
self
,
reason
)
self
.
protocol
.
connectionLost
(
reason
)
win32file
.
CloseHandle
(
self
.
hComPort
)
self
.
protocol
.
connectionLost
(
reason
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment