Skip to content

Instantly share code, notes, and snippets.

@ericc59
Created February 21, 2012 03:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ericc59/1873492 to your computer and use it in GitHub Desktop.
Save ericc59/1873492 to your computer and use it in GitHub Desktop.
RscMgr.h
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace RedparkSDK
{
[BaseType (typeof (NSObject))]
interface RscMgr {
//- (void) setDelegate:(id <RscMgrDelegate>) delegate;
[Export ("setDelegate:")]
void SetDelegate (NSObject delegate1);
//- (void) open;
[Export ("open")]
void Open ();
//- (void) setBaud:(int)baud;
[Export ("setBaud:")]
void SetBaud (int baud);
//- (void) setDataSize:(DataSizeType)dataSize;
[Export ("setDataSize:")]
void SetDataSize (DataSizeType dataSize);
//- (void) setParity:(ParityType)parity;
[Export ("setParity:")]
void SetParity (ParityType parity);
//- (void) setStopBits:(StopBitsType)stopBits;
[Export ("setStopBits:")]
void SetStopBits (StopBitsType stopBits);
//- (int) write:(UInt8 *)data Length:(UInt32)length;
[Export ("write:Length:")]
int Write (IntPtr data, UInt32 length);
//- (int) read:(UInt8 *)data Length:(UInt32)length;
[Export ("read:Length:")]
int Read (IntPtr data, UInt32 length);
//- (int) getReadBytesAvailable;
[Export ("getReadBytesAvailable")]
int GetReadBytesAvailable { get; }
//- (int) getModemStatus;
[Export ("getModemStatus")]
int GetModemStatus { get; }
//- (BOOL) getDtr;
[Export ("getDtr")]
bool GetDtr { get; }
//- (BOOL) getRts;
[Export ("getRts")]
bool GetRts { get; }
//- (void) setDtr:(BOOL)enable;
[Export ("setDtr:")]
void SetDtr (bool enable);
//- (void) setRts:(BOOL)enable;
[Export ("setRts:")]
void SetRts (bool enable);
/*
//- (void) setPortConfig:(serialPortConfig *)config RequestStatus:(BOOL)reqStatus;
[Export ("setPortConfig:RequestStatus:")]
void SetPortConfig (serialPortConfig config, bool reqStatus);
//- (void) setPortControl:(serialPortControl *)control RequestStatus:(BOOL)reqStatus;
[Export ("setPortControl:RequestStatus:")]
void SetPortControl (serialPortControl control, bool reqStatus);
//- (void) getPortConfig:(serialPortConfig *) portConfig;
[Export ("getPortConfig:")]
void GetPortConfig (serialPortConfig portConfig);
//- (void) getPortStatus:(serialPortStatus *) portStatus;
[Export ("getPortStatus:")]
void GetPortStatus (serialPortStatus portStatus);
*/
//- (int) writeRscMessage:(int)cmd Length:(int)len MsgData:(UInt8 *)msgData;
[Export ("writeRscMessage:Length:MsgData:")]
int WriteRscMessage (int cmd, int len, IntPtr msgData);
//- (void) testGpsCable;
[Export ("testGpsCable")]
void TestGpsCable ();
}
[Model]
[BaseType(typeof(NSObject))]
public interface RscMgrDelegate
{
// Redpark Serial Cable has been connected and/or application moved to foreground.
// protocol is the string which matched from the protocol list passed to initWithProtocol:
// - (void) cableConnected:(NSString *)protocol;
[Abstract, Export("cableConnected:")]
void CableConnected(string protocol);
[Abstract, Export ("cableDisconnected")]
void CableDisconnected ();
[Abstract, Export ("readBytesAvailable:")]
void ReadBytesAvailable (int len);
[Export ("didReceivePortConfig")]
void DidReceivePortConfig ();
[Export ("didGpsLoopTest:")]
void DidGpsLoopTest (bool pass);
}
}
/****************************************************************************************
*
* redparkSerial.h
*
* Data structures and interface for iThing app <--> Redpark Serial Cable
*
* Copyright © 2010-2011 Redpark
*
* Revisions:
* 03mar2010 First Release Docs
* 01jun2011 modify for libRsc (public api)
*
*
* Note:
*
* In the info.plist of your iThing application, you need to add the Key
*
* ' UISupportedExternalAccessoryProtocols' and set it to: 'com.redpark.serdb9'
*
*
* Usage Notes:
*
* The serial port of the cable is always reset to the default settings
* when the cable is first connected to the iThing exits.
*
******************************************************************************************/
#ifndef __REDPARKSERIAL_H
#define __REDPARKSERIAL_H
// modem control status bits
#define MODEM_STAT_CTS 0x01
#define MODEM_STAT_RI 0x02
#define MODEM_STAT_DSR 0x04
#define MODEM_STAT_DCD 0x08
#define MODEM_STAT_RTS 0x01
#define MODEM_STAT_DTR 0x02
// defines for dataLen, parity, stopBits
#define SERIAL_DATABITS_7 0x07
#define SERIAL_DATABITS_8 0x08
#define STOPBITS_1 0x01 // 1 stop bit for all byte sizes
#define STOPBITS_2 0x02 // 2 stop bits for 6-8 bit byte
#define SERIAL_PARITY_NONE 0x00
#define SERIAL_PARITY_ODD 0x01
#define SERIAL_PARITY_EVEN 0x02
// defines for rxFlowControl, txFlowControl
// if not 'NONE' flow control is handled within the accessory
#define TXFLOW_NONE 0x00
#define TXFLOW_CTS 0x01
#define TXFLOW_DSR 0x02
#define TXFLOW_DCD 0x03
#define TXFLOW_XOFF 0x04
#define RXFLOW_NONE 0x00 // default state: DTR=OFF & RTS=OFF
#define RXFLOW_XOFF 0x01 // default state: DTR=OFF & RTS=OFF
#define RXFLOW_RTS 0x02 // default state: DTR=OFF & RTS=ON
#define RXFLOW_DTR 0x03 // default state: DTR=ON & RTS=OFF
// the default config settings...
#define DEFAULT_TXFLOW TXFLOW_NONE
#define DEFAULT_RXFLOW RXFLOW_NONE
#define DEFAULT_SERIAL_DATABITS SERIAL_DATABITS_8
#define DEFAULT_SERIAL_PARITY SERIAL_PARITY_NONE
#define DEFAULT_STOPBITS STOPBITS_1
#ifdef LOCATION
#define DEFAULT_BAUD 4800
#else
#define DEFAULT_BAUD 9600
#endif
#define DEFAULT_RXFORWARDTIME 100
#define DEFAULT_RXFORWARDCOUNT 16
#define DEFAULT_TXACKSETTING 0
#define DEFAULT_XON_CHAR 0
#define DEFAULT_XOFF_CHAR 0
typedef struct serialPortStatus {
unsigned char
txDiscard, // non-zero if tx data msg from App discarded
rxOverrun, // non-zero if overrun error occurred
rxParity, // non-zero if parity error occurred
rxFrame, // non-zero if frame error occurred
txAck, // ack when tx buffer becomes empty (sent only if txAxkSetting non-zero in config)
msr, // 0-3 current modem status bits for CTS, DSR, DCD & RI, 4-7 previous modem status bits, MODEM_STAT_
rtsDtrState, // 0-3 current modem status bits for RTS & DTR, 4-7 previous modem status bits, MODEM_STAT_
rxFlowStat, // rx flow control off= 0 on = RXFLOW_RTS/DTR/XOFF
txFlowStat, // rx flow control off= 0 on = TXFLOW_DCD/CTS/DSR/XOFF
returnResponse; // Non-zero if returned in response to config or control
// message with returnStatus requested (non-zero). If non-zero the
// value will equal the returnStatus byte.
} serialPortStatus;
typedef struct serialPortConfig {
unsigned char baudLo, // baud rate lo/hi up to 57600
baudHi,
dataLen, // SERIAL_DATABITS_ 7, 8
parity, // SERIAL_PARITY_ NONE, ODD, EVEN
stopBits, // STOPBITS_ 1, 2
txFlowControl, // TXFLOW_
rxFlowControl, // RXFLOW_
xonChar, // XON/XOFF flow control chars if used
xoffChar,
// RX forwarding parameters:
rxForwardingTimeout, // forward rx data when idle timeout reached
rxForwardCount, // or received this many chars..
txAckSetting, // NON-zero if a txAck response is requested (sent when tx buffer becomes empty)
returnStatus; // NON-zero if a status response (to this config message) is requested
}serialPortConfig;
typedef struct serialPortControl {
unsigned char txFlush, // non-zero if tx buffer should be flushed
rxFlush, // non-zero if rx buffer should be flushed
setDtr, // change DTR state to..
dtr, // DTR state 1=On, 0=Off (ignored if setDtr == 0)
setRts, // change RTS state to..
rts, // RTS state 1=On, 0=Off (ignored if setRts == 0)
txBreak, // tx 'n' break characters
returnStatus; // non-zero if a status response (to this control message) is requested
}serialPortControl;
#endif // __REDPARKSERIAL_H
//
// RscMgr.h
//
// Copyright © 2011 Redpark All Rights Reserved
//
#import <Foundation/Foundation.h>
#import <ExternalAccessory/ExternalAccessoryDefines.h>
#import <ExternalAccessory/EAAccessoryManager.h>
#import <ExternalAccessory/EAAccessory.h>
#import <ExternalAccessory/EASession.h>
#include "redparkSerial.h"
enum
{
kMsrCts = 0x01,
kMsrRi = 0x02,
kMsrDsr = 0x04,
kMsrDcd = 0x08,
};
enum {
kRSC_StreamBufferSize = 4096,
kRSC_MaxMessageDataLength = 230,
kRSC_SerialReadBufferSize = 4096,
kRsc_TxFifoSize = 256,
kRSC_NoPasscode = 0
};
typedef enum DataSizeType
{
kDataSize7 = SERIAL_DATABITS_7,
kDataSize8 = SERIAL_DATABITS_8
} DataSizeType;
typedef enum ParityType
{
kParityNone = SERIAL_PARITY_NONE,
kParityOdd = SERIAL_PARITY_ODD,
kParityEven = SERIAL_PARITY_EVEN
} ParityType;
typedef enum StopBitsType
{
kStopBits1 = STOPBITS_1,
kStopBits2 = STOPBITS_2
} StopBitsType;
@protocol RscMgrDelegate;
@interface RscMgr : NSObject <NSStreamDelegate> {
id <RscMgrDelegate> theDelegate;
// EA api variables
EASession *theSession;
EAAccessory *theAccessory;
NSArray *supportedProtocols;
NSString *connectedProtocol;
// rsc port control/info structures
serialPortConfig portConfig;
serialPortStatus portStatus;
serialPortControl portControl;
// EASession stream handling
// for collecting RSC Messages
unsigned char *rxStreamBuffer;
int rxCount;
int rxCountTotal;
int txCountTotal;
int rxRemain;
int readLen;
unsigned char *txStreamBuffer;
int txIn;
int txOut;
int txStreamEmpty;
// internal dtr and rts state bits
int dtrState;
int rtsState;
// serial data buffer
// for collecting serial bytes received from the serial port
UInt8 *serialReadBuffer;
int serialReadIn;
int serialReadOut;
int serialReadBytesAvailable;
BOOL encodingEnabled;
UInt32 thePasscode;
}
- (void) setDelegate:(id <RscMgrDelegate>) delegate;
// Initializes the RscMgr and reigsters for accessory connect/disconnect notifications.
- (id) init;
// establish communication with the Redpark Serial Cable. This call will also
// configure the serial port based on defaults or prior calls to set the port config
// (see setBaud, setDataSize, ...)
- (void) open;
// simple serial port config interface
// can be called anytime (even after open: call)
- (void) setBaud:(int)baud;
- (void) setDataSize:(DataSizeType)dataSize;
- (void) setParity:(ParityType)parity;
- (void) setStopBits:(StopBitsType)stopBits;
// read write serial bytes
- (int) write:(UInt8 *)data Length:(UInt32)length;
- (int) read:(UInt8 *)data Length:(UInt32)length;
- (int) getReadBytesAvailable;
/*
returns a bit field (see redparkSerial.h)
0-3 current modem status bits for CTS, RI, DSR, DCD, 4-7 previous modem status bits
MODEM_STAT_CTS 0x01
MODEM_STAT_RI 0x02
MODEM_STAT_DSR 0x04
MODEM_STAT_DCD 0x08
*/
- (int) getModemStatus;
// returns true if DTR is asserted
- (BOOL) getDtr;
// returns true if RTS is asserted
- (BOOL) getRts;
// set DTR state
- (void) setDtr:(BOOL)enable;
// set RTS state
- (void) setRts:(BOOL)enable;
// advanced (full) serial port config interface (see redparkSerial.h)
- (void) setPortConfig:(serialPortConfig *)config RequestStatus:(BOOL)reqStatus;
- (void) setPortControl:(serialPortControl *)control RequestStatus:(BOOL)reqStatus;
- (void) getPortConfig:(serialPortConfig *) portConfig;
- (void) getPortStatus:(serialPortStatus *) portStatus;
// advanced advanced
// write a raw message
- (int) writeRscMessage:(int)cmd Length:(int)len MsgData:(UInt8 *)msgData;
// GPS cable only - requires loopback connector
- (void) testGpsCable;
@end
@protocol RscMgrDelegate <NSObject>
// Redpark Serial Cable has been connected and/or application moved to foreground.
// protocol is the string which matched from the protocol list passed to initWithProtocol:
- (void) cableConnected:(NSString *)protocol;
// Redpark Serial Cable was disconnected and/or application moved to background
- (void) cableDisconnected;
// serial port status has changed
// user can call getModemStatus or getPortStatus to get current state
- (void) portStatusChanged;
// bytes are available to be read (user calls read:)
- (void) readBytesAvailable:(UInt32)length;
@optional
// called when a response is received to a getPortConfig call
- (void) didReceivePortConfig;
// GPS Cable only - called with result when loop test completes.
- (void) didGpsLoopTest:(BOOL)pass;
@end
using System;
namespace RedparkSDK
{
public enum DataSizeType
{
DataSize7 = 0x07,
kDataSize8 = 0x08
}
public enum ParityType
{
kParityNone = 0x00,
kParityOdd = 0x01,
kParityEven = 0x02
}
public enum StopBitsType
{
kStopBits1 = 0x01,
kStopBits2 = 0x02
}
public struct rscMsgHdr
{
public byte hdr1;
public byte hdr2;
public byte len; // of data following 'cmd'
public byte cmd;
}
public struct serialPortStatus {
public byte
txDiscard, // non-zero if tx data msg from App discarded
rxOverrun, // non-zero if overrun error occurred
rxParity, // non-zero if parity error occurred
rxFrame, // non-zero if frame error occurred
txAck, // ack when tx buffer becomes empty (sent only if txAxkSetting non-zero in config)
msr, // 0-3 current modem status bits for CTS, DSR, DCD & RI, 4-7 previous modem status bits, MODEM_STAT_
rtsDtrState, // 0-3 current modem status bits for RTS & DTR, 4-7 previous modem status bits, MODEM_STAT_
rxFlowStat, // rx flow control off= 0 on = RXFLOW_RTS/DTR/XOFF
txFlowStat, // rx flow control off= 0 on = TXFLOW_DCD/CTS/DSR/XOFF
returnResponse; // Non-zero if returned in response to config or control
// message with returnStatus requested (non-zero). If non-zero the
// value will equal the returnStatus byte.
}
public struct serialPortConfig {
public byte baudLo, // baud rate lo/hi up to 57600
baudHi,
dataLen, // SERIAL_DATABITS_ 7, 8
parity, // SERIAL_PARITY_ NONE, ODD, EVEN
stopBits, // STOPBITS_ 1, 2
txFlowControl, // TXFLOW_
rxFlowControl, // RXFLOW_
xonChar, // XON/XOFF flow control chars if used
xoffChar,
// RX forwarding parameters:
rxForwardingTimeout, // forward rx data when idle timeout reached
rxForwardCount, // or received this many chars..
txAckSetting, // NON-zero if a txAck response is requested (sent when tx buffer becomes empty)
returnStatus; // NON-zero if a status response (to this config message) is requested
}
public struct serialPortControl {
public byte txFlush, // non-zero if tx buffer should be flushed
rxFlush, // non-zero if rx buffer should be flushed
setDtr, // change DTR state to..
dtr, // DTR state 1=On, 0=Off (ignored if setDtr == 0)
setRts, // change RTS state to..
rts, // RTS state 1=On, 0=Off (ignored if setRts == 0)
txBreak, // tx 'n' break characters
returnStatus; // non-zero if a status response (to this control message) is requested
}
}
public class CommDelegate : RscMgrDelegate
{
MainViewController controller;
public CommDelegate(MainViewController controller)
{
this.controller = controller;
}
public override void CableConnected (string protocol)
{
controller.Log("Cable was connected with protocol: " + protocol);
}
public override void CableDisconnected ()
{
controller.Log("Cable was disconnected");
}
public override void ReadBytesAvailable (int len)
{
// TODO: Implement - see: http://go-mono.com/docs/index.aspx?link=T%3aMonoTouch.Foundation.ModelAttribute
}
public override void DidGpsLoopTest (bool pass)
{
// TODO: Implement - see: http://go-mono.com/docs/index.aspx?link=T%3aMonoTouch.Foundation.ModelAttribute
}
public override void DidReceivePortConfig ()
{
// TODO: Implement - see: http://go-mono.com/docs/index.aspx?link=T%3aMonoTouch.Foundation.ModelAttribute
}
}
manager = new RscMgr();
managerDelegate = new CommDelegate(this);
manager.SetDelegate(managerDelegate);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment