// Greg Stitt, Ann Gordon-Ross // SendText.cpp : implementation file // #include "stdafx.h" #include "vahid.h" #include "SendText.h" #include "special.h" #include "port.h" #include #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CSendText dialog CSendText::CSendText(CWnd* pParent /*=NULL*/) : CDialog(CSendText::IDD, pParent) { //{{AFX_DATA_INIT(CSendText) m_strLabel = _T(""); //}}AFX_DATA_INIT m_nProgress = 0; m_bNewDay = FALSE; } void CSendText::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSendText) DDX_Text(pDX, IDC_MESSAGE, m_strLabel); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSendText, CDialog) //{{AFX_MSG_MAP(CSendText) ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSendText message handlers BOOL CSendText::OnInitDialog() { // initialize progress bar CProgressCtrl* pProg = (CProgressCtrl*) GetDlgItem(IDC_PROGRESS1); pProg->SetRange(0,NUM_TIMES * m_nSends); pProg->SetPos(m_nProgress); // initialize m_strLabel to correct room switch(m_nRoom) { case 0: m_strLabel = "Sending Text to room A265"; break; case 1: m_strLabel = "Sending Text to room A275"; break; case 2: m_strLabel = "Sending Text to room A277"; break; } CDialog::OnInitDialog(); SetTimer(1, 100, NULL); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } // sends text to appropriate room void CSendText::SendText() { KillTimer(1); Port serial(m_nCom, m_nBaud); char begin = (char)BEGIN_TRANSMISSION; char end = (char)END_TRANSMISSION; CString room; // sets room to the name of the current room switch(m_nRoom) { case 0: room.Format("A265"); break; case 1: room.Format("A275"); break; case 2: room.Format("A277"); break; } // retrieves data for current room if (!GetText(room)) { // error is returned so that error message is displayed EndDialog(ID_ERRORFILE); return; } CProgressCtrl* pProg = (CProgressCtrl*) GetDlgItem(IDC_PROGRESS1); TRACE("Numsends from sendtext = %d\n", m_nSends); if (serial.IsGood()) { // sends data m_nSends times for (int num_send = 0; num_send < m_nSends; num_send ++) { // sends each time for current day for (int index = 0; index < NUM_TIMES; index ++) { // if the data has changed or if it is a new day // send BEGIN_TRANSMISSION, else send SAME if ((m_text[index] != m_oldtext[index] && num_send == 0) || (m_bNewDay == TRUE)) { serial.SendUart(BEGIN_TRANSMISSION + m_nRoom); TRACE("BEGIN = %d\n", BEGIN_TRANSMISSION + m_nRoom); } else { serial.SendUart(SAME_TRANSMISSION + m_nRoom); TRACE("SAME = %d\n", SAME_TRANSMISSION + m_nRoom); } m_bNewDay = FALSE; // delay and send data Sleep(500); serial.SendStringUart(m_text[index]); TRACE("text = %s\n", m_text[index]); // delay and send END_INDEX Sleep(40); serial.SendUart(END_INDEX); // update progress bar m_nProgress ++; pProg->SetPos(m_nProgress); Sleep(25); } // end the transmission serial.SendUart(END_TRANSMISSION); } // update progress bar and label m_nProgress = NUM_TIMES * m_nSends; pProg->SetPos(m_nProgress); m_strLabel = "Done Sending"; UpdateData(FALSE); } else { // print error message is com port is working MessageBox("The port is not functioning.", "Error"); EndDialog(IDCANCEL); return; } // delay so user can read label then exit with IDOK Sleep(300); OnOK(); } // causes the progress bar to start void CSendText::OnTimer(UINT nIDEvent) { SendText(); } // reads in data from appropriate file int CSendText::GetText(CString room) { CTime time = CTime::GetCurrentTime(); ifstream infile; char temp[MAX_LINE_CHAR]; int line_count; CString test; test = m_strPath + "\\" + room + "\\" + time.Format("%B.txt"); infile.open(m_strPath + "\\" + room + "\\" + time.Format("%B.txt"), ios::nocreate); if (!infile) { CString error; error.Format("Error Opening File: %s", room + "\\" + time.Format("%B.txt")); //if (m_bSending == FALSE) // AfxMessageBox(error); infile.close(); return 0; } // used for sequential access -> old version /* for (line_count = 0; line_count < (time.GetDay() - 1) * NUM_TIMES && !infile.eof(); line_count ++) { infile.getline(temp, MAX_LINE_CHAR + 1, '\n'); infile.read((char *) temp, 42); } */ // used for random access new version // each line contains MAX_LINE_CHAR + 2 bytes // each day starts after NUM_TIMES lines infile.seekg((time.GetDay() - 1) * NUM_TIMES * (MAX_LINE_CHAR + 2)); // copies data from file into CString array for (line_count = 0; line_count < NUM_TIMES && !infile.eof(); line_count ++) { //must be MAX_LINE_CHAR + 1 infile.read((char *) temp, MAX_LINE_CHAR + 1); m_text[line_count].Format("%s", temp); TRACE("DATA = %s", m_text[line_count]); } infile.close(); return 1; }