Final code for our 4180 Drawing Robot!

Dependencies:   4DGL-uLCD-SE gCodeParser mbed

Committer:
jford38
Date:
Wed Apr 30 16:40:10 2014 +0000
Revision:
2:ba15545a4ccf
Added Visual Studio Source files and README.; Alejandro.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jford38 2:ba15545a4ccf 1 // SerialIO.cpp : Defines the entry point for the console application.
jford38 2:ba15545a4ccf 2
jford38 2:ba15545a4ccf 3 #include "stdafx.h"
jford38 2:ba15545a4ccf 4 #include <windows.h>
jford38 2:ba15545a4ccf 5 #include <stdio.h>
jford38 2:ba15545a4ccf 6 #include "listCOM.h"
jford38 2:ba15545a4ccf 7 #include <iostream>
jford38 2:ba15545a4ccf 8 #include <fstream>
jford38 2:ba15545a4ccf 9 #include <string>
jford38 2:ba15545a4ccf 10 #include <ctype.h>
jford38 2:ba15545a4ccf 11
jford38 2:ba15545a4ccf 12 using namespace std;
jford38 2:ba15545a4ccf 13
jford38 2:ba15545a4ccf 14 #define FEED 10
jford38 2:ba15545a4ccf 15 #define END_OF_TRANSMISSION 23
jford38 2:ba15545a4ccf 16
jford38 2:ba15545a4ccf 17 BOOL ModifyCommSettings(HANDLE hComPort);
jford38 2:ba15545a4ccf 18 HANDLE hSerial;
jford38 2:ba15545a4ccf 19
jford38 2:ba15545a4ccf 20 int openConn()
jford38 2:ba15545a4ccf 21 {
jford38 2:ba15545a4ccf 22 DWORD cBytes_out, cBytes_in;
jford38 2:ba15545a4ccf 23 DWORD dwMask;
jford38 2:ba15545a4ccf 24
jford38 2:ba15545a4ccf 25 char cBuffer_in[2];
jford38 2:ba15545a4ccf 26 cBuffer_in[1] = 0;
jford38 2:ba15545a4ccf 27 // Display message on console
jford38 2:ba15545a4ccf 28 printf("\nOpening Serial Port \n\r");
jford38 2:ba15545a4ccf 29
jford38 2:ba15545a4ccf 30 // Open Serial Port COMx: for Read and Write
jford38 2:ba15545a4ccf 31 hSerial = CreateFile((LPCWSTR)my_port, GENERIC_READ | GENERIC_WRITE, 0, NULL,
jford38 2:ba15545a4ccf 32 OPEN_EXISTING, 0, NULL);
jford38 2:ba15545a4ccf 33 // Check for file open errors
jford38 2:ba15545a4ccf 34 if (hSerial == INVALID_HANDLE_VALUE){
jford38 2:ba15545a4ccf 35 printf("file open errors\n");
jford38 2:ba15545a4ccf 36 Sleep(4000);
jford38 2:ba15545a4ccf 37 return 0;
jford38 2:ba15545a4ccf 38 }
jford38 2:ba15545a4ccf 39 // Modify Com Port settings (i.e. Baud Rate, #bits, parity etc)
jford38 2:ba15545a4ccf 40 if (!ModifyCommSettings(hSerial)){
jford38 2:ba15545a4ccf 41 printf("com port settings errors\n");
jford38 2:ba15545a4ccf 42 Sleep(4000);
jford38 2:ba15545a4ccf 43 return 0;
jford38 2:ba15545a4ccf 44 }
jford38 2:ba15545a4ccf 45 printf("\n Opened Serial Connection with mbed \n\r Ctrl C to exit\n\r");
jford38 2:ba15545a4ccf 46
jford38 2:ba15545a4ccf 47 // Set Communication event mask for WaitCommEvent for rxchar (receive character) in buffer
jford38 2:ba15545a4ccf 48 SetCommMask(hSerial, EV_RXCHAR | EV_ERR);
jford38 2:ba15545a4ccf 49
jford38 2:ba15545a4ccf 50 // Setting up the reading of the text file
jford38 2:ba15545a4ccf 51 ifstream iFile("C:\\ECE4180\\hellombed.gcode");
jford38 2:ba15545a4ccf 52 if (iFile) printf("\n Gcode file opened successfully\r\n");
jford38 2:ba15545a4ccf 53
jford38 2:ba15545a4ccf 54 string line;
jford38 2:ba15545a4ccf 55 // creating a pointer to char to store the C version of the line string
jford38 2:ba15545a4ccf 56 char* c_line;
jford38 2:ba15545a4ccf 57
jford38 2:ba15545a4ccf 58 cBuffer_in[0] = 0;
jford38 2:ba15545a4ccf 59 // reading the gcode file line by line
jford38 2:ba15545a4ccf 60 while (getline(iFile, line)){
jford38 2:ba15545a4ccf 61
jford38 2:ba15545a4ccf 62 // finding the length of the message
jford38 2:ba15545a4ccf 63 cBytes_out = line.length() + 1;
jford38 2:ba15545a4ccf 64 // allocating space for C representation of string to be sent over serial
jford38 2:ba15545a4ccf 65 c_line = new char[cBytes_out];
jford38 2:ba15545a4ccf 66
jford38 2:ba15545a4ccf 67 //coyping data to C string c_line
jford38 2:ba15545a4ccf 68 strcpy_s(c_line, cBytes_out, line.c_str());
jford38 2:ba15545a4ccf 69
jford38 2:ba15545a4ccf 70 cout << c_line << "\r\n";
jford38 2:ba15545a4ccf 71
jford38 2:ba15545a4ccf 72 // checking that the line contains a valid command.
jford38 2:ba15545a4ccf 73 // TODO: only checking for starting command being a G#*, might have invalid args that are not checked
jford38 2:ba15545a4ccf 74 if (c_line[0] == 'G' && isdigit(c_line[1])){
jford38 2:ba15545a4ccf 75 // sending the line over the serial link
jford38 2:ba15545a4ccf 76 if (!WriteFile(hSerial, c_line, cBytes_out, &cBytes_out, NULL)){
jford38 2:ba15545a4ccf 77 printf("\rFile write errors\n");
jford38 2:ba15545a4ccf 78 Sleep(4000);
jford38 2:ba15545a4ccf 79 return 0;
jford38 2:ba15545a4ccf 80 }
jford38 2:ba15545a4ccf 81
jford38 2:ba15545a4ccf 82 // deleting the dynamic buffer after sending
jford38 2:ba15545a4ccf 83 delete[] c_line;
jford38 2:ba15545a4ccf 84 }
jford38 2:ba15545a4ccf 85 // if the line does not have a command continue to the next line
jford38 2:ba15545a4ccf 86 else{
jford38 2:ba15545a4ccf 87 // deleting the dynamic buffer after sending
jford38 2:ba15545a4ccf 88 delete[] c_line;
jford38 2:ba15545a4ccf 89 continue;
jford38 2:ba15545a4ccf 90 }
jford38 2:ba15545a4ccf 91
jford38 2:ba15545a4ccf 92 // Wait for character to be sent back to UARTs input buffer - events are more efficient than looping
jford38 2:ba15545a4ccf 93 WaitCommEvent(hSerial, &dwMask, 0);
jford38 2:ba15545a4ccf 94
jford38 2:ba15545a4ccf 95 cBuffer_in[0] = 0;
jford38 2:ba15545a4ccf 96 // Loop until receiving the FEED signal
jford38 2:ba15545a4ccf 97 while (cBuffer_in[0] != FEED){
jford38 2:ba15545a4ccf 98 // Read constantly waiting for FEED character
jford38 2:ba15545a4ccf 99 ReadFile(hSerial, cBuffer_in, 1, &cBytes_in, NULL);
jford38 2:ba15545a4ccf 100
jford38 2:ba15545a4ccf 101 /*
jford38 2:ba15545a4ccf 102 // Read any serial data and display
jford38 2:ba15545a4ccf 103 if (ReadFile(hSerial, cBuffer_in, 1, &cBytes_in, NULL)){
jford38 2:ba15545a4ccf 104
jford38 2:ba15545a4ccf 105 cout << cBuffer_in << "\r\n";
jford38 2:ba15545a4ccf 106 cout << cBytes_in << " bytes received \r\n";
jford38 2:ba15545a4ccf 107 }
jford38 2:ba15545a4ccf 108 */
jford38 2:ba15545a4ccf 109 } //while cBuffer_in[0] != FEED
jford38 2:ba15545a4ccf 110 } // while getline
jford38 2:ba15545a4ccf 111
jford38 2:ba15545a4ccf 112 // after the last line has been sent, sent the end of transmission message
jford38 2:ba15545a4ccf 113 c_line[0] = END_OF_TRANSMISSION;
jford38 2:ba15545a4ccf 114 c_line[1] = 0; // NULL terminate the string
jford38 2:ba15545a4ccf 115 cBytes_out = 2;
jford38 2:ba15545a4ccf 116
jford38 2:ba15545a4ccf 117 // attempt until message is sent
jford38 2:ba15545a4ccf 118 while (!WriteFile(hSerial, c_line, cBytes_out, &cBytes_out, NULL));
jford38 2:ba15545a4ccf 119
jford38 2:ba15545a4ccf 120 string input;
jford38 2:ba15545a4ccf 121 cout << "Press any key followed by ENTER to exit...\n>";
jford38 2:ba15545a4ccf 122 cin >> input;
jford38 2:ba15545a4ccf 123
jford38 2:ba15545a4ccf 124 // Close Files
jford38 2:ba15545a4ccf 125 iFile.close();
jford38 2:ba15545a4ccf 126 CloseHandle(hSerial);
jford38 2:ba15545a4ccf 127 return 0;
jford38 2:ba15545a4ccf 128 }
jford38 2:ba15545a4ccf 129
jford38 2:ba15545a4ccf 130 BOOL ModifyCommSettings(HANDLE hComPort)
jford38 2:ba15545a4ccf 131 {
jford38 2:ba15545a4ccf 132 COMMTIMEOUTS ctos;
jford38 2:ba15545a4ccf 133 DCB PortDCB;
jford38 2:ba15545a4ccf 134 // Initialize the DCBlength member.
jford38 2:ba15545a4ccf 135 PortDCB.DCBlength = sizeof (DCB);
jford38 2:ba15545a4ccf 136 // Get the default serial port settings DCB information.
jford38 2:ba15545a4ccf 137 GetCommState(hSerial, &PortDCB);
jford38 2:ba15545a4ccf 138 // Change the common DCB structure settings to modify serial port settings.
jford38 2:ba15545a4ccf 139 PortDCB.BaudRate = 921600; // Current baud
jford38 2:ba15545a4ccf 140 PortDCB.fBinary = TRUE; // Binary mode; no EOF check
jford38 2:ba15545a4ccf 141 PortDCB.fParity = TRUE; // Enable parity checking
jford38 2:ba15545a4ccf 142 PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
jford38 2:ba15545a4ccf 143 PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
jford38 2:ba15545a4ccf 144 PortDCB.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type
jford38 2:ba15545a4ccf 145 PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
jford38 2:ba15545a4ccf 146 PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
jford38 2:ba15545a4ccf 147 PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
jford38 2:ba15545a4ccf 148 PortDCB.fInX = FALSE; // No XON/XOFF in flow control
jford38 2:ba15545a4ccf 149 PortDCB.fErrorChar = FALSE; // Disable error replacement
jford38 2:ba15545a4ccf 150 PortDCB.fNull = FALSE; // Disable null stripping
jford38 2:ba15545a4ccf 151 PortDCB.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control
jford38 2:ba15545a4ccf 152 PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on error
jford38 2:ba15545a4ccf 153 PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
jford38 2:ba15545a4ccf 154 PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
jford38 2:ba15545a4ccf 155 PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
jford38 2:ba15545a4ccf 156 // Configure the port settings according to the new specifications
jford38 2:ba15545a4ccf 157 // of the DCB structure.
jford38 2:ba15545a4ccf 158 if (!SetCommState(hSerial, &PortDCB)){
jford38 2:ba15545a4ccf 159 printf("Unable to configure the serial port");
jford38 2:ba15545a4ccf 160 Sleep(4000);
jford38 2:ba15545a4ccf 161 return false;
jford38 2:ba15545a4ccf 162 }
jford38 2:ba15545a4ccf 163 // Set read time outs
jford38 2:ba15545a4ccf 164 ctos.ReadIntervalTimeout = MAXDWORD;
jford38 2:ba15545a4ccf 165 ctos.ReadTotalTimeoutMultiplier = MAXDWORD;
jford38 2:ba15545a4ccf 166 ctos.ReadTotalTimeoutConstant = 1;
jford38 2:ba15545a4ccf 167 ctos.WriteTotalTimeoutMultiplier = 0;
jford38 2:ba15545a4ccf 168 ctos.WriteTotalTimeoutConstant = 0;
jford38 2:ba15545a4ccf 169 if (!SetCommTimeouts(hSerial, &ctos)){
jford38 2:ba15545a4ccf 170 printf("Unable to configure the serial port");
jford38 2:ba15545a4ccf 171 Sleep(4000);
jford38 2:ba15545a4ccf 172 return false;
jford38 2:ba15545a4ccf 173 }
jford38 2:ba15545a4ccf 174 return true;
jford38 2:ba15545a4ccf 175 }
jford38 2:ba15545a4ccf 176