We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi!
I have already created the firmware for Lpc1114 but an issue I am running into is how to communicate with the mcu through a GUI. I know how to create gui's but I don't know how to send binary that then gets converted to a USB packet and then to the UART port of the mcu. Any suggestions would be helpful.
Here is some code I wrote not complete but may give you an Idea.
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "SER_TST_JUN4.h" #include "math.h" #include "conio.h" #include "iostream.h" #include <iomanip> #include <fstream> HANDLE m_hCom; char m_sComPort[10]; BOOL m_bPortReady; DCB m_dcb; COMMTIMEOUTS m_CommTimeouts; BOOL bWriteRC; BOOL bReadRC; int indx_list[20]; int length_list[20]; unsigned char str2[4000]; char STRING_OUT[4000]; int i, j; DWORD iBytesWritten; // READ STUFF DWORD iBytesRead; unsigned char sBuffer[10000]; char message[4000]; int CMD_NUM = 1; //int complete_num; void open_port(); void close_port(); void send_cmd(int index, int length); void receive_cmd(int length); void load_array(); void INIT_LONG_ARRAY(); void send_long_cmd(int index, int length); void receive_long_cmd(int length); unsigned char A_arr[] = { 0x11, 0x22, 0x3C, 0x1E, 0x10, 0x22, 0x33, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11 }; unsigned char * ptr; //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- void __fastcall TForm1::send_buttonClick(TObject *Sender) { length_list[0] = 10; indx_list[0] = 0; memset(STRING_OUT, 0x00, 100); memset(str2, 0x00, 200); load_array(); open_port(); send_cmd(indx_list[0], length_list[0]); receive_cmd(length_list[0]); close_port(); } //--------------------------------------------------------------------------- void send_cmd(int index, int length) { bWriteRC = WriteFile(m_hCom, &str2[index], length, &iBytesWritten, NULL); return; } void load_array() { ptr = A_arr; //ptr = B_arr; //ptr = C_arr; //ptr = D_arr; //ptr = E_arr; indx_list[0] = 0; //begining index of command length_list[0] = 23; // length of command str2[0] = ptr[0]; str2[1] = ptr[1]; str2[2] = ptr[2]; str2[3] = ptr[3]; str2[4] = ptr[4]; str2[5] = ptr[5]; str2[6] = ptr[6]; str2[7] = ptr[7]; str2[8] = ptr[8]; str2[9] = ptr[9]; str2[10] = ptr[10]; str2[11] = ptr[11]; str2[12] = ptr[12]; str2[13] = ptr[13]; str2[14] = ptr[14]; str2[15] = ptr[15]; str2[16] = ptr[16]; str2[17] = ptr[17]; str2[18] = ptr[18]; str2[19] = ptr[19]; str2[20] = ptr[20]; str2[21] = ptr[21]; str2[22] = ptr[22]; str2[23] = ptr[23]; ///--------------------------------- return; } void open_port() { strcpy(&m_sComPort[0], "Com1"); m_hCom = CreateFileA(m_sComPort, GENERIC_READ | GENERIC_WRITE, 0, // exclusive access NULL, // no security OPEN_EXISTING, 0, // no overlapped I/O NULL); // null template m_bPortReady = SetupComm(m_hCom, 1000, 1000); // set buffer sizes // Make a call to GetCommState() first in order to fill // the comSettings structure with all the necessary values. // Then change the ones you want and call SetCommState(). m_bPortReady = GetCommState(m_hCom, &m_dcb); m_dcb.BaudRate = 115200; //9600; //921600; //9600; m_dcb.ByteSize = 8; m_dcb.Parity = NOPARITY; m_dcb.StopBits = TWOSTOPBITS; m_dcb.fAbortOnError = TRUE; m_bPortReady = SetCommState(m_hCom, &m_dcb); m_bPortReady = GetCommTimeouts (m_hCom, &m_CommTimeouts); m_CommTimeouts.ReadIntervalTimeout = 50; m_CommTimeouts.ReadTotalTimeoutConstant = 50; m_CommTimeouts.ReadTotalTimeoutMultiplier = 10; m_CommTimeouts.WriteTotalTimeoutConstant = 50; m_CommTimeouts.WriteTotalTimeoutMultiplier = 10; m_bPortReady = SetCommTimeouts (m_hCom, &m_CommTimeouts); return; } void receive_cmd(int length) { memset(sBuffer, 0x00, 10000); bReadRC = ReadFile(m_hCom, &sBuffer, length, &iBytesRead, NULL); memset(STRING_OUT, 0x00, 100); memset(message, 0x00, 40000); sprintf(message,"%d ", CMD_NUM ); strcat(STRING_OUT, message); CMD_NUM = CMD_NUM + 1; for(j = 0; j <= length-1; j++) { sprintf( message,"%x ", sBuffer[j] ); strcat(STRING_OUT, message); //ShowMessage(STRING_OUT); } Form1->ListBox1->Items->Add(STRING_OUT); return; } void close_port() { CloseHandle(m_hCom); return; } //--------------------------------------------------------------------------- void __fastcall TForm1::rcv_buttonClick(TObject *Sender) { length_list[0] = 10; indx_list[0] = 0; memset(STRING_OUT, 0x00, 100); memset(str2, 0x00, 200); //load_array(); open_port(); //send_cmd(indx_list[0], length_list[0]); receive_cmd(length_list[0]); close_port(); } //--------------------------------------------------------------------------- void __fastcall TForm1::sendchar_buttonClick(TObject *Sender) { length_list[0] = 1; indx_list[0] = 0; memset(STRING_OUT, 0x00, 100); memset(str2, 0x00, 200); str2[0] = 'a'; open_port(); send_cmd(indx_list[0], length_list[0]); close_port(); } //--------------------------------------------------------------------------- void send_long_cmd(int index, int length) { char messge[100]; messge[0] = NULL; bWriteRC = WriteFile(m_hCom, &STRING_OUT[index], length, &iBytesWritten, NULL); sprintf(messge,"written = %d ", iBytesWritten); ShowMessage(messge); return; } void receive_long_cmd(int length) { char messge[100]; messge[0] = NULL; memset(sBuffer, 0x00, 4000); bReadRC = ReadFile(m_hCom, &sBuffer, length, &iBytesRead, NULL); memset(STRING_OUT, 0x00, 4000); memset(message, 0x00, 4000); sprintf(message,"%d %d", CMD_NUM, iBytesRead ); strcat(STRING_OUT, message); CMD_NUM = CMD_NUM + 1; for(j = 0; j <= length-1; j++) { sprintf( message,"%x ", sBuffer[j] ); strcat(STRING_OUT, message); //ShowMessage(STRING_OUT); } Form1->ListBox1->Items->Add(STRING_OUT); fstream outfile("out.txt", ios::out); outfile << STRING_OUT << endl; outfile.close(); sprintf(messge,"j = %d \t %d", j, length); ShowMessage(messge); return; }
Wow ok thanks a lot for all the help this is more help than I thought I would receive, I know what I need to do now. Thanks again!