CableCommunication.h - UnbDroid/sekksi2016 GitHub Wiki
inline void WaitForMessageToBeSent()
{
while(RS485SendingData())
Wait(MS_1);
}
void ConfigureRS485(){
// configure the S4 port as RS485
UseRS485();
// make sure the RS485 system is turned on
RS485Enable();
// initialize the UART to default values
// low level API function call (allows changing UART settings)
RS485Uart(HS_BAUD_DEFAULT, HS_MODE_DEFAULT);
Wait(MS_1); // make sure everything gets turned on okay
}
void SendCableMessage(string msg){
byte buffer[];
// send the # of bytes (5 bytes)
byte cnt = ArrayLen(msg);
SendRS485Number(cnt);
WaitForMessageToBeSent();
until(RS485DataAvailable());
RS485Read(buffer);
// now send the message
SendRS485String(msg);
WaitForMessageToBeSent();
// wait for ACK from recipient
until(RS485DataAvailable());
RS485Read(buffer);
}
string ReceiveCableMessage(){
byte mlen;
string buffer;
byte ACK[] = {1};
// wait for a message to arrive.
// read the number of bytes message
until(RS485DataAvailable() >= 5);
// read the number of bytes
RS485Read(buffer);
long cnt = 0;
UnflattenVar(buffer, cnt);
// send out ACK
RS485Write(ACK);
WaitForMessageToBeSent();
// now wait for the real message
until(RS485DataAvailable() >= cnt);
// now read the actual message
RS485ReadEx(buffer, cnt);
// RS485Read(buffer);
// send out ACK
RS485Write(ACK);
WaitForMessageToBeSent();
// display message
return buffer;
}