![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Final code for our 4180 Drawing Robot!
Dependencies: 4DGL-uLCD-SE gCodeParser mbed
Revision 2:ba15545a4ccf, committed 2014-04-30
- Comitter:
- jford38
- Date:
- Wed Apr 30 16:40:10 2014 +0000
- Parent:
- 1:ad895d72e9ed
- Commit message:
- Added Visual Studio Source files and README.; Alejandro.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/VStudio2013_Sources/README.txt Wed Apr 30 16:40:10 2014 +0000 @@ -0,0 +1,10 @@ +GCODE parser for the Mbed Controlled DrawBot +authors: Alejandro Jimenez, Jordan Ford +ECE 4180 Spring 2014 +In order to compile and run the program on the PC that parses and sends GCODE to the Mbed, follow the steps: + +1) Create a new Visual Studio 2013 C++ project. +2) Add the provided header and source files. +3) In Project Properties, enable Common Language Runtime Support option (/clr) +4) In the SerialIO.cpp source, line 51, change the path to point to the gcode file stored in your machine +5) Compile and run when the Mbed is ready to receive commands \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/VStudio2013_Sources/SerialIO.cpp Wed Apr 30 16:40:10 2014 +0000 @@ -0,0 +1,176 @@ +// SerialIO.cpp : Defines the entry point for the console application. + +#include "stdafx.h" +#include <windows.h> +#include <stdio.h> +#include "listCOM.h" +#include <iostream> +#include <fstream> +#include <string> +#include <ctype.h> + +using namespace std; + +#define FEED 10 +#define END_OF_TRANSMISSION 23 + +BOOL ModifyCommSettings(HANDLE hComPort); +HANDLE hSerial; + +int openConn() +{ + DWORD cBytes_out, cBytes_in; + DWORD dwMask; + + char cBuffer_in[2]; + cBuffer_in[1] = 0; + // Display message on console + printf("\nOpening Serial Port \n\r"); + + // Open Serial Port COMx: for Read and Write + hSerial = CreateFile((LPCWSTR)my_port, GENERIC_READ | GENERIC_WRITE, 0, NULL, + OPEN_EXISTING, 0, NULL); + // Check for file open errors + if (hSerial == INVALID_HANDLE_VALUE){ + printf("file open errors\n"); + Sleep(4000); + return 0; + } + // Modify Com Port settings (i.e. Baud Rate, #bits, parity etc) + if (!ModifyCommSettings(hSerial)){ + printf("com port settings errors\n"); + Sleep(4000); + return 0; + } + printf("\n Opened Serial Connection with mbed \n\r Ctrl C to exit\n\r"); + + // Set Communication event mask for WaitCommEvent for rxchar (receive character) in buffer + SetCommMask(hSerial, EV_RXCHAR | EV_ERR); + + // Setting up the reading of the text file + ifstream iFile("C:\\ECE4180\\hellombed.gcode"); + if (iFile) printf("\n Gcode file opened successfully\r\n"); + + string line; + // creating a pointer to char to store the C version of the line string + char* c_line; + + cBuffer_in[0] = 0; + // reading the gcode file line by line + while (getline(iFile, line)){ + + // finding the length of the message + cBytes_out = line.length() + 1; + // allocating space for C representation of string to be sent over serial + c_line = new char[cBytes_out]; + + //coyping data to C string c_line + strcpy_s(c_line, cBytes_out, line.c_str()); + + cout << c_line << "\r\n"; + + // checking that the line contains a valid command. + // TODO: only checking for starting command being a G#*, might have invalid args that are not checked + if (c_line[0] == 'G' && isdigit(c_line[1])){ + // sending the line over the serial link + if (!WriteFile(hSerial, c_line, cBytes_out, &cBytes_out, NULL)){ + printf("\rFile write errors\n"); + Sleep(4000); + return 0; + } + + // deleting the dynamic buffer after sending + delete[] c_line; + } + // if the line does not have a command continue to the next line + else{ + // deleting the dynamic buffer after sending + delete[] c_line; + continue; + } + + // Wait for character to be sent back to UARTs input buffer - events are more efficient than looping + WaitCommEvent(hSerial, &dwMask, 0); + + cBuffer_in[0] = 0; + // Loop until receiving the FEED signal + while (cBuffer_in[0] != FEED){ + // Read constantly waiting for FEED character + ReadFile(hSerial, cBuffer_in, 1, &cBytes_in, NULL); + + /* + // Read any serial data and display + if (ReadFile(hSerial, cBuffer_in, 1, &cBytes_in, NULL)){ + + cout << cBuffer_in << "\r\n"; + cout << cBytes_in << " bytes received \r\n"; + } + */ + } //while cBuffer_in[0] != FEED + } // while getline + + // after the last line has been sent, sent the end of transmission message + c_line[0] = END_OF_TRANSMISSION; + c_line[1] = 0; // NULL terminate the string + cBytes_out = 2; + + // attempt until message is sent + while (!WriteFile(hSerial, c_line, cBytes_out, &cBytes_out, NULL)); + + string input; + cout << "Press any key followed by ENTER to exit...\n>"; + cin >> input; + + // Close Files + iFile.close(); + CloseHandle(hSerial); + return 0; +} + +BOOL ModifyCommSettings(HANDLE hComPort) +{ + COMMTIMEOUTS ctos; + DCB PortDCB; + // Initialize the DCBlength member. + PortDCB.DCBlength = sizeof (DCB); + // Get the default serial port settings DCB information. + GetCommState(hSerial, &PortDCB); + // Change the common DCB structure settings to modify serial port settings. + PortDCB.BaudRate = 921600; // Current baud + PortDCB.fBinary = TRUE; // Binary mode; no EOF check + PortDCB.fParity = TRUE; // Enable parity checking + PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control + PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control + PortDCB.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type + PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity + PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx + PortDCB.fOutX = FALSE; // No XON/XOFF out flow control + PortDCB.fInX = FALSE; // No XON/XOFF in flow control + PortDCB.fErrorChar = FALSE; // Disable error replacement + PortDCB.fNull = FALSE; // Disable null stripping + PortDCB.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control + PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on error + PortDCB.ByteSize = 8; // Number of bits/byte, 4-8 + PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space + PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2 + // Configure the port settings according to the new specifications + // of the DCB structure. + if (!SetCommState(hSerial, &PortDCB)){ + printf("Unable to configure the serial port"); + Sleep(4000); + return false; + } + // Set read time outs + ctos.ReadIntervalTimeout = MAXDWORD; + ctos.ReadTotalTimeoutMultiplier = MAXDWORD; + ctos.ReadTotalTimeoutConstant = 1; + ctos.WriteTotalTimeoutMultiplier = 0; + ctos.WriteTotalTimeoutConstant = 0; + if (!SetCommTimeouts(hSerial, &ctos)){ + printf("Unable to configure the serial port"); + Sleep(4000); + return false; + } + return true; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/VStudio2013_Sources/listCOM.cpp Wed Apr 30 16:40:10 2014 +0000 @@ -0,0 +1,40 @@ +#include "stdafx.h" +#include "listCOM.h" +#include "string.h" + +#using <System.dll> + +using namespace System; +using namespace System::IO::Ports; +using namespace System::ComponentModel; + +_TCHAR my_port[NAME_SIZE]; + +_TCHAR* listCOM() +{ + wcscpy(my_port, L"\\\\.\\"); + _TCHAR temp[10]; + + array<String^>^ serialPorts = nullptr; + try + { + // Get a list of serial port names. + serialPorts = SerialPort::GetPortNames(); + } + catch (Win32Exception^ ex) + { + Console::WriteLine(ex->Message); + } + + Console::WriteLine("The following serial ports were found:"); + + // Display each port name to the console. + for each(String^ port in serialPorts) + { + Console::WriteLine(port); + _snwprintf(temp, NAME_SIZE, L"%hs", port); + wcscat_s(my_port, NAME_SIZE, temp); + + } + return my_port; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/VStudio2013_Sources/listCOM.h Wed Apr 30 16:40:10 2014 +0000 @@ -0,0 +1,10 @@ + +#ifndef LISTCOM_H +#define LISTCOM_H + +#define NAME_SIZE 40 +extern _TCHAR my_port[NAME_SIZE]; + +_TCHAR* listCOM(void); + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/VStudio2013_Sources/talk_to_mbed.cpp Wed Apr 30 16:40:10 2014 +0000 @@ -0,0 +1,17 @@ +// talk_to_mbed.cpp : Defines the entry point for the console application. +// + +#include "stdafx.h" +#include "listCOM.h" +#include <cstdlib> + +//forward declaration of function defined in the SerialIO.cpp file +int openConn(); + +int _tmain(int argc, _TCHAR* argv[]) +{ + listCOM(); + wprintf(L"My port is %s\n", my_port); + openConn(); + return 0; +}