Skip to content

Commit

Permalink
Added methods for RTS and DTR
Browse files Browse the repository at this point in the history
- Added methods to get and set the `RTS` and `DTR` values.
- Fixed some inconsistencies in the documentation.
- Fixed some typos where `LS_TNUMBER` was incorrectly used instead of `LS_TBOOLEAN`.
- Closes #3310
  • Loading branch information
latenitefilms authored and cmsj committed Nov 3, 2022
1 parent 22199ba commit 0723af3
Showing 1 changed file with 97 additions and 5 deletions.
102 changes: 97 additions & 5 deletions extensions/serial/libserial.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ @interface HSSerialPort : NSObject <ORSSerialPortDelegate>
@property BOOL usesDTRDSRFlowControl;
@property BOOL usesDCDOutputFlowControl;
@property BOOL allowsNonStandardBaudRates;
@property BOOL rts;
@property BOOL dtr;

@end

Expand Down Expand Up @@ -134,6 +136,8 @@ - (instancetype)init
_usesDTRDSRFlowControl = NO;
_usesDCDOutputFlowControl = NO;
_allowsNonStandardBaudRates = NO;
_rts = NO;
_dtr = NO;
}
return self;
}
Expand Down Expand Up @@ -230,7 +234,7 @@ - (void)serialPort:(ORSSerialPort *)serialPort didReceiveData:(NSData *)data

- (void)serialPort:(ORSSerialPort *)serialPort didReceivePacket:(NSData *)packetData matchingDescriptor:(ORSSerialPacketDescriptor *)descriptor
{
// TODO: Impliment `ORSSerialPacketDescriptor` functionality
// TODO: Implement `ORSSerialPacketDescriptor` functionality
}

- (void)serialPortWasRemovedFromSystem:(ORSSerialPort *)serialPort;
Expand Down Expand Up @@ -409,6 +413,8 @@ - (BOOL)open
self.serialPort.usesRTSCTSFlowControl = self.usesRTSCTSFlowControl;
self.serialPort.usesDTRDSRFlowControl = self.usesDTRDSRFlowControl;
self.serialPort.usesDCDOutputFlowControl = self.usesDCDOutputFlowControl;
self.serialPort.RTS = self.rts;
self.serialPort.DTR = self.dtr;
[self.serialPort open];
}
return self.serialPort && self.serialPort.isOpen;
Expand Down Expand Up @@ -447,6 +453,22 @@ - (void)changeNumberOfDataBits:(NSUInteger)numberOfDataBits
}
}

- (void)changeRTS:(BOOL)rtsEnabled
{
self.rts = rtsEnabled;
if ([self isOpen]) {
self.serialPort.RTS = rtsEnabled;
}
}

- (void)changeDTR:(BOOL)dtrEnabled
{
self.dtr = dtrEnabled;
if ([self isOpen]) {
self.serialPort.DTR = dtrEnabled;
}
}

- (void)changeUsesRTSCTSFlowControl:(BOOL)usesRTSCTSFlowControl
{
self.usesRTSCTSFlowControl = usesRTSCTSFlowControl;
Expand Down Expand Up @@ -872,10 +894,12 @@ static int serial_parity(lua_State *L) {
///
/// Returns:
/// * If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
///
/// Notes:
/// * The default value is `false`.
static int serial_usesDCDOutputFlowControl(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TNUMBER | LS_TOPTIONAL, LS_TBREAK];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK];
HSSerialPort *serialPort = [skin toNSObjectAtIndex:1];
if (lua_gettop(L) == 1) {
// Get:
Expand All @@ -898,10 +922,12 @@ static int serial_usesDCDOutputFlowControl(lua_State *L) {
///
/// Returns:
/// * If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
///
/// Notes:
/// * The default value is `false`.
static int serial_usesDTRDSRFlowControl(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TNUMBER | LS_TOPTIONAL, LS_TBREAK];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK];
HSSerialPort *serialPort = [skin toNSObjectAtIndex:1];
if (lua_gettop(L) == 1) {
// Get:
Expand All @@ -924,10 +950,12 @@ static int serial_usesDTRDSRFlowControl(lua_State *L) {
///
/// Returns:
/// * If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
///
/// Notes:
/// * The default value is `false`.
static int serial_usesRTSCTSFlowControl(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TNUMBER | LS_TOPTIONAL, LS_TBREAK];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK];
HSSerialPort *serialPort = [skin toNSObjectAtIndex:1];
if (lua_gettop(L) == 1) {
// Get:
Expand All @@ -941,6 +969,64 @@ static int serial_usesRTSCTSFlowControl(lua_State *L) {
return 1;
}

/// hs.serial:dtr([value]) -> boolean | serialPortObject
/// Method
/// Gets or sets the state of the serial port's DTR (Data Terminal Ready) pin.
///
/// Parameters:
/// * value - An optional boolean.
///
/// Returns:
/// * If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
///
/// Notes:
/// * The default value is `false`.
/// * Setting this to `true` is most likely required for Arduino devices prior to opening the serial port.
static int serial_dtr(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK];
HSSerialPort *serialPort = [skin toNSObjectAtIndex:1];
if (lua_gettop(L) == 1) {
// Get:
BOOL dtrEnabled = serialPort.dtr;
lua_pushboolean(L, dtrEnabled);
} else {
BOOL dtrEnabled = lua_toboolean(L, 2);
[serialPort changeDTR:dtrEnabled];
lua_pushvalue(L, 1);
}
return 1;
}

/// hs.serial:rts([value]) -> boolean | serialPortObject
/// Method
/// Gets or sets the state of the serial port's RTS (Request to Send) pin.
///
/// Parameters:
/// * value - An optional boolean.
///
/// Returns:
/// * If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
///
/// Notes:
/// * The default value is `false`.
/// * Setting this to `true` is most likely required for Arduino devices prior to opening the serial port.
static int serial_rts(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK];
HSSerialPort *serialPort = [skin toNSObjectAtIndex:1];
if (lua_gettop(L) == 1) {
// Get:
BOOL rtsEnabled = serialPort.rts;
lua_pushboolean(L, rtsEnabled);
} else {
BOOL rtsEnabled = lua_toboolean(L, 2);
[serialPort changeRTS:rtsEnabled];
lua_pushvalue(L, 1);
}
return 1;
}

/// hs.serial:shouldEchoReceivedData([value]) -> boolean | serialPortObject
/// Method
/// Gets or sets whether the port should echo received data.
Expand All @@ -950,10 +1036,12 @@ static int serial_usesRTSCTSFlowControl(lua_State *L) {
///
/// Returns:
/// * If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
///
/// Notes:
/// * The default value is `false`.
static int serial_shouldEchoReceivedData(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TNUMBER | LS_TOPTIONAL, LS_TBREAK];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK];
HSSerialPort *serialPort = [skin toNSObjectAtIndex:1];
if (lua_gettop(L) == 1) {
// Get:
Expand All @@ -976,6 +1064,8 @@ static int serial_shouldEchoReceivedData(lua_State *L) {
///
/// Returns:
/// * If a value is specified, then this method returns the serial port object. Otherwise this method returns the number of stop bits as a number.
///
/// Notes:
/// * The default value is 1.
static int serial_numberOfStopBits(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
Expand Down Expand Up @@ -1227,6 +1317,8 @@ static int meta_gc(lua_State* L) {
{"shouldEchoReceivedData", serial_shouldEchoReceivedData},
{"usesRTSCTSFlowControl", serial_usesRTSCTSFlowControl},
{"usesDTRDSRFlowControl", serial_usesDTRDSRFlowControl},
{"rts", serial_rts},
{"dtr", serial_dtr},
{"usesDCDOutputFlowControl", serial_usesDCDOutputFlowControl},
{"__tostring", userdata_tostring},
{"__eq", userdata_eq},
Expand Down

0 comments on commit 0723af3

Please sign in to comment.