2018-04-22 23:11:56 +00:00
|
|
|
#include "MacSerialStream.h"
|
|
|
|
|
|
|
|
#include <Serial.h>
|
|
|
|
#include <Devices.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-04-27 07:00:33 +00:00
|
|
|
MacSerialStream::MacSerialStream(int baud)
|
|
|
|
: curBaud(baud)
|
2018-04-22 23:11:56 +00:00
|
|
|
{
|
2018-04-27 00:06:54 +00:00
|
|
|
open();
|
2018-04-22 23:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MacSerialStream::close()
|
|
|
|
{
|
|
|
|
if(inRefNum == 0)
|
|
|
|
return;
|
|
|
|
SerSetBuf(inRefNum, NULL, 0);
|
|
|
|
|
|
|
|
CloseDriver(inRefNum);
|
|
|
|
CloseDriver(outRefNum);
|
|
|
|
inRefNum = outRefNum = 0;
|
|
|
|
}
|
|
|
|
|
2018-04-27 00:06:54 +00:00
|
|
|
void MacSerialStream::open()
|
|
|
|
{
|
|
|
|
OSErr err;
|
|
|
|
err = OpenDriver("\p.AOut", &outRefNum);
|
|
|
|
err = OpenDriver("\p.AIn", &inRefNum);
|
|
|
|
SerSetBuf(inRefNum, inputBuffer, kInputBufferSize);
|
|
|
|
|
|
|
|
|
|
|
|
SerShk shk;
|
|
|
|
memset(&shk, 0, sizeof(shk));
|
|
|
|
shk.fCTS = true;
|
|
|
|
Control(outRefNum, kSERDHandshake, &shk);
|
|
|
|
|
|
|
|
setBaud(curBaud);
|
|
|
|
}
|
|
|
|
|
2018-04-22 23:11:56 +00:00
|
|
|
MacSerialStream::~MacSerialStream()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MacSerialStream::write(const void* p, size_t n)
|
|
|
|
{
|
|
|
|
ParamBlockRec pb;
|
|
|
|
memset(&pb, 0, sizeof(pb));
|
|
|
|
pb.ioParam.ioRefNum = outRefNum;
|
|
|
|
pb.ioParam.ioBuffer = (Ptr)p;
|
|
|
|
pb.ioParam.ioReqCount = n;
|
|
|
|
OSErr err = PBWriteSync(&pb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MacSerialStream::idle()
|
|
|
|
{
|
|
|
|
long count = 0;
|
|
|
|
SerGetBuf(inRefNum, &count);
|
|
|
|
while(count > 0)
|
|
|
|
{
|
|
|
|
long count1 = count > kReadBufferSize ? kReadBufferSize : count;
|
|
|
|
ParamBlockRec pb;
|
|
|
|
memset(&pb, 0, sizeof(pb));
|
|
|
|
pb.ioParam.ioRefNum = inRefNum;
|
|
|
|
pb.ioParam.ioBuffer = (Ptr)&readBuffer;
|
|
|
|
pb.ioParam.ioReqCount = count1;
|
|
|
|
OSErr err = PBReadSync(&pb);
|
|
|
|
if(err)
|
|
|
|
return;
|
|
|
|
count -= count1;
|
|
|
|
|
|
|
|
notifyReceive((uint8_t*)readBuffer, count1);
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 20:06:32 +00:00
|
|
|
|
|
|
|
void MacSerialStream::setBaud(int baud)
|
|
|
|
{
|
2018-04-27 00:06:54 +00:00
|
|
|
curBaud = baud;
|
2018-04-24 20:06:32 +00:00
|
|
|
short baudval = 0;
|
|
|
|
switch(baud)
|
|
|
|
{
|
|
|
|
case 9600: baudval = baud9600; break;
|
|
|
|
case 14400: baudval = baud14400; break;
|
|
|
|
case 19200: baudval = baud19200; break;
|
|
|
|
case 28800: baudval = baud28800; break;
|
|
|
|
case 38400: baudval = baud38400; break;
|
|
|
|
case 57600: baudval = baud57600; break;
|
|
|
|
case 115200: baudval = 0; break;
|
|
|
|
case 230400: baudval = 0; break;
|
|
|
|
}
|
|
|
|
SerReset(outRefNum, baudval | data8 | noParity | stop10);
|
|
|
|
|
|
|
|
if(baud == 115200)
|
|
|
|
Control(outRefNum, kSERD115KBaud, nullptr);
|
|
|
|
else if(baud == 230400)
|
|
|
|
Control(outRefNum, kSERD230KBaud, nullptr);
|
|
|
|
}
|