Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <windows.h>
- #include <stdio.h>
- #include <conio.h>
- #include <bits/stdc++.h>
- #include <stdlib.h> /* srand, rand */
- #include <time.h>
- #include "neuron class.h"
- #include "adv_neuron.h"
- using namespace std;
- int hextodec(char c)
- {
- switch(c)
- {
- case '0':
- {
- return 0;
- }
- case '1':
- {
- return 1;
- }
- case '2':
- {
- return 2;
- }
- case '3':
- {
- return 3;
- }
- case '4':
- {
- return 4;
- }
- case '5':
- {
- return 5;
- }
- case '6':
- {
- return 6;
- }
- case '7':
- {
- return 7;
- }
- case '8':
- {
- return 8;
- }
- case '9':
- {
- return 9;
- }
- case 'A':
- {
- return 10;
- }
- case 'B':
- {
- return 11;
- }
- case 'C':
- {
- return 12;
- }
- case 'D':
- {
- return 13;
- }
- case 'E':
- {
- return 14;
- }
- case 'F':
- {
- return 15;
- }
- }
- }
- int main(int argc, char *argv[])
- {
- int n=0;
- DCB dcb= {0};
- HANDLE hCom;
- BOOL fSuccess;
- char *pcCommPort = "COM3";
- char szBuff[40]= {0};
- char otherBuff[40]= {"300<CR>"};
- DWORD dwBytesRead=0;
- DWORD dwBytesWrite=0;
- /***************************************CommTimeouts******************************************/
- COMMTIMEOUTS timeouts= {0};
- timeouts.ReadIntervalTimeout=50;
- timeouts.ReadTotalTimeoutConstant=50;
- timeouts.ReadTotalTimeoutMultiplier=10;
- timeouts.WriteTotalTimeoutConstant=50;
- timeouts.WriteTotalTimeoutMultiplier=10;
- /*******************************************Handle*******************************************/
- hCom = CreateFile( pcCommPort,
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ, // must be opened with exclusive-access
- NULL, // no security attributes
- OPEN_EXISTING, // must use OPEN_EXISTING
- FILE_ATTRIBUTE_NORMAL, // not overlapped I/O
- NULL // hTemplate must be NULL for comm devices
- );
- /***************************************SET*UP*COM*PORT**************************************/
- if (hCom == INVALID_HANDLE_VALUE)
- {
- printf ("CreateFile failed with error %d.\n", GetLastError());
- return (1);
- }
- if(!SetCommTimeouts(hCom, &timeouts))
- {
- /*Well, then an error occurred*/
- }
- fSuccess = GetCommState(hCom, &dcb);
- if (!fSuccess)
- {
- /*More Error Handling*/
- printf ("GetCommState failed with error %d.\n", GetLastError());
- return (2);
- }
- dcb.BaudRate = 115200; // set the baud rate
- dcb.ByteSize = 8; // data size, xmit, and rcv
- dcb.Parity = NOPARITY; // no parity bit
- dcb.StopBits = ONESTOPBIT; // one stop bit
- fSuccess = SetCommState(hCom, &dcb);
- if (!fSuccess)
- {
- printf ("SetCommState failed. Error: %d.\n", GetLastError());
- return (3);
- }
- printf ("Serial port %s successfully configured.\n", pcCommPort);
- // return (0);
- /*************************************Reading************************************************/
- srand (time(NULL));
- int a[10],k=0,p,p2,c[10],zg[10],mx1,j1,pp[4]={0},i1,p3,r,p4,p5,p6,i,j,s[10],mx;
- float w[6][3]={0};
- advanced_neuron advn[9];
- queue<int> buf[2];
- int neuron_number=8,input_number=9;
- p=0;
- char cc;
- neuron neurons[10];
- // for(i=0; i<neuron_number; i++)
- // for(j=0; j<20; j++)buf[i].push(0);cout<<"debilo";
- for(i=0; i<neuron_number; i++)
- {
- for(j=0; j<input_number; j++)
- neurons[i].init_inp(&a[j]);
- for(j=0; j<neuron_number; j++)
- if(i!=j)neurons[i].init_con(&neurons[j].ss,&neurons[j].c,neurons[j].w);
- neurons[i].init();
- }
- for(i=0; i<2; i++)
- {
- for(j=0; j<4; j++)
- advn[i].init_inp(&neurons[j].p);
- for(j=0; j<2; j++)
- if(i!=j)advn[i].init_lat(&advn[j].ss);
- advn[i].init();
- }
- while(1)
- {
- if(GetAsyncKeyState(VK_UP))
- {
- for(i=0; i<9; i++)
- {
- if(neurons[i].e==0) neurons[i].e=1; else neurons[i].e=0;
- }
- }
- if(ReadFile(hCom, szBuff, 39, &dwBytesRead, NULL))
- {
- int j=0;
- for(j=0; j<dwBytesRead; j++)
- {
- if(k==16)
- {
- if(kbhit())
- {
- cc=getch();
- if(cc=='1')
- advn[0].learn();
- if(cc=='2')
- advn[1].learn();
- if(cc=='3')
- advn[2].learn();
- if(cc=='4')
- advn[3].learn();
- if(cc=='5')
- advn[4].learn();
- if(cc=='6')
- advn[5].learn();
- if(cc=='7')
- advn[6].learn();
- if(cc=='8')
- advn[7].learn();
- if(cc=='9')
- advn[8].learn();
- }
- system("cls");
- p++;
- cout<<p<<endl;
- a[0]=a[0];
- a[1]=a[1];
- a[2]=a[2];
- a[3]=max(0,a[0]-a[1]);
- a[4]=max(0,a[0]-a[2]);
- a[5]=max(0,a[1]-a[0]);
- a[6]=max(0,a[1]-a[2]);
- a[7]=max(0,a[2]-a[0]);
- a[8]=max(0,a[2]-a[1]);
- for(i=0; i<input_number; i++)
- cout<<a[i]<<" ";
- cout<<endl;
- for(i=0; i<neuron_number; i++)
- neurons[i].work_int();
- for(i=0; i<neuron_number; i++)
- neurons[i].work_out();
- for(i=0; i<2; i++)
- advn[i].work_int();
- for(i=0; i<2; i++)
- advn[i].work_out();
- for(int i=0; i<neuron_number; i++)
- {
- cout<<neurons[i].n<<" ";
- for(int j1=0; j1<neurons[i].n; j1++)
- printf("%f\t",neurons[i].w[j1]);
- int t=neurons[i].zg;
- printf("%d \t %d \t %d \t %d \t %d \n\r",neurons[i].zg,neurons[i].c,neurons[i].s,neurons[i].ss,neurons[i].p);
- }
- cout<<endl;
- for(int i=0; i<2; i++)
- {
- cout<<advn[i].n<<" ";
- for(int j1=0; j1<advn[i].n; j1++)
- printf("%f\t",advn[i].w[j1]);
- int t=advn[i].zg;
- printf("%d \t %d \t %d \t %d \t %d \n\r",advn[i].zg,advn[i].c,advn[i].s,advn[i].ss,advn[i].p);
- }
- }
- if(szBuff[j]=='#')
- {
- a[0]=0;
- a[1]=0;
- a[2]=0;
- a[3]=0;
- k=1;
- continue;
- }
- else if(k==0)
- continue;
- else if(k%4==1)
- a[k/4]+=hextodec(szBuff[j])*16*16;
- else if(k%4==2)
- a[k/4]+=hextodec(szBuff[j])*16;
- else if(k%4==3)
- a[k/4]+=hextodec(szBuff[j]);
- k++;
- }
- }
- }
- //printf("The size of dwBytesWrite is: %c", &otherBuff);
- //printf("I wrote to the device\n");
- //printf("I");
- //printf("Heres szBuff %d\n", szBuff);
- /********************************************************************************************/
- CloseHandle(hCom);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement