Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-rtos mbed mbed_fota_fan_control
Fork of mbed_fota_lamp_control by
ext_fota/SerialManager.cpp
- Committer:
- dudnwjs
- Date:
- 2015-07-13
- Revision:
- 5:e11b23f9aacc
- Parent:
- 3:1e70387e1337
- Child:
- 6:8dd20294b2aa
File content as of revision 5:e11b23f9aacc:
#include "SerialManager.h"
namespace sevencore_fota{
SerialManager::SerialManager(Serial *_device)
{
print_flag = 0;
device = _device;
FE_MSG_PACKET_TYPE = 0x05;
MAX_PACKET_LENGTH = 350;
bReceiveState = 0;
wDataLength = 0;
wReceivePos = 0;
bHdrBytesRead = 0;
memset(bReceiveElementArr,0,512);
}
SerialManager::SerialManager(Serial *_device, Serial *_hostpc)
{
print_flag = 1;
device = _device;
hostpc = _hostpc;
FE_MSG_PACKET_TYPE = 0x05;
MAX_PACKET_LENGTH = 350;
bReceiveState = 0;
wDataLength = 0;
wReceivePos = 0;
bHdrBytesRead = 0;
memset(bReceiveElementArr,0,512);
PrintSerialManager();
}
SerialManager::~SerialManager(void)
{
}
void SerialManager::PrintSerialManager(void)
{
if(print_flag == 1)
hostpc->printf("\nSerialManager Start!!\n");
}
int SerialManager::SendToSerial(uint8_t *data,unsigned short size)
{
int cnt = 0;
if(print_flag == 1)
hostpc->printf("\n>>> send size = %hu\n",size);
for(int i=0; i < size; i++)
{
if( device->writeable())
{
if(print_flag == 1)
hostpc->printf("%02X ", data[i]);
device->putc(data[i]);
cnt++;
}
}
return cnt;
}
// return boolean : msg_size (receive complete) or -1 (receive not complete)
int SerialManager::ReceiveToSerial(unsigned char *receive_msg)
{
int ret = -1;
unsigned char tmp;
if(device->readable())
{
tmp = device->getc();
switch(bReceiveState)
{
case 0:
if( tmp == FE_MSG_PACKET_TYPE )
{
bReceiveState = 1;
wDataLength = 0;
wReceivePos = 0;
bHdrBytesRead = 0;
memset(bReceiveElementArr,0,512);
bReceiveElementArr[wReceivePos] = tmp;
wReceivePos++;
if(print_flag == 1)
hostpc->printf("\n[Receiver] Packet Type |: %02X \n", tmp);
}else
{
if(print_flag == 1)
hostpc->printf("\n[Receiver] Packet Type Error |: %02X \n", tmp);
}
break;
case 1:
if(print_flag == 1)
hostpc->printf("R-%02X ",tmp);
bHdrBytesRead++;
bReceiveElementArr[wReceivePos] = tmp;
wReceivePos++;
if( bHdrBytesRead == 6 )
bReceiveState = 2;
break;
case 2:
if(print_flag == 1)
hostpc->printf("R+%02X ",tmp);
wDataLength += tmp;
if( wDataLength > MAX_PACKET_LENGTH )
bReceiveState = 0;
else
{
bReceiveElementArr[wReceivePos] = tmp;
wReceivePos++;
bReceiveState = 3;
}
break;
case 3:
if( print_flag == 1 )
hostpc->printf("R=%02X ",tmp);
wDataLength += (unsigned short) (tmp*256);
if( wDataLength > MAX_PACKET_LENGTH )
{
if( print_flag == 1 )
hostpc->printf("\n[Receiver] Over SIZE: %d \n", wDataLength);
bReceiveState = 0;
}else if (wDataLength == 0)
{
if( print_flag == 1 )
hostpc->printf("\n[Receiver] Zero SIZE: %d \n", wDataLength);
memcpy(receive_msg,bReceiveElementArr,wReceivePos);
ret = wReceivePos;
bReceiveState = 0;
}else
{
bReceiveElementArr[wReceivePos] = tmp;
wReceivePos++;
bReceiveState = 4;
}
break;
case 4:
if( print_flag == 1 )
hostpc->printf("R~%02X ",tmp);
bReceiveElementArr[wReceivePos] = tmp;
wReceivePos++;
// 9 = 1(first byte = FE_MSG_PACKET_TYPE) + 2(type) +2(dstid) +2(srcid) +2(length size)
if(wReceivePos == wDataLength + 9)
{
memcpy(receive_msg,bReceiveElementArr,wReceivePos);
ret = wReceivePos;
bReceiveState = 0;
if( print_flag == 1 )
hostpc->printf("\n[Receiver] Rcv Data SIZE: %d \n", wDataLength);
}
break;
default:
if(print_flag == 1)
hostpc->printf("\nERROR STRAGE STATE R+%02X \n",tmp);
break;
}
}
return ret;
}
void SerialManager::ReceiveToSerialTest(void)
{
unsigned char tmp;
int pos = 0;
char buff[128];
memset(buff,'K',127);
buff[127] = '\0';
hostpc->printf("\n pos = %d FIRST <%s> \n",pos,buff);
tmp = device->getc();
tmp = device->getc();
tmp = device->getc();
tmp = device->getc();
tmp = device->getc();
while(1)
{
if(device->readable())
{
tmp = device->getc();
hostpc->printf("%d ==> 0x%X\n",pos,tmp);
buff[pos] = tmp;
pos++;
if( pos == 128 ){
pos = 0;
memset(buff,'Z',127);
buff[127] = '\0';
}
if( pos%16 == 0 ){
hostpc->printf("\n pos = %d ERROR <%s> \n",pos,buff);
if(device->writeable()){
hostpc->printf("writeable!!\n");
device->putc(0x80);
device->putc(0x44);
}
}
}
}
}
}//namespace
