// std_c_and_functions // This source code shows use of standard c code. // Structs, Arrays, pointers, abs(), string functions. // Ctrl A Transmits Extended Can Data Frame. // Ctrl B Transmits Standard Can Data Frame. // Ctrl D shows absolute value and string to long // Ctrl J calls functions user defined structure in definestructureanddisplay() // and displays values in Information View. // Ctrl M calls function2 which calls my function and printfunction and then displays values. // Ctrl P Starts a timer. // Ctrl K kills a timer and displays message in Information View. // Ctrl T transmits frame and displays message in Information View. #include int a; int g; char string1[50]; char * string2; DataFrame *x; DataFrame *y; DataFrame *t; DataFrame *v; void myfunction(void) { char temp[30]; int value = 10; sprintf(temp, "value = %d", value); WriteInfo(temp); } void function2(void) { /* Shows standard C code showing a function calling a function*/ myfunction(); printfunction(); } void definestructanddisplay(void) { /* This function just shows standard C code within PBEC. Creates a struct and displays the data within the struct */ char temp1[75]; struct cars { char name[25]; int numvehicles; int nummodules; } gem, vow; int y; int x; vow.nummodules = 10; vow.numvehicles = 12; strcpy (vow.name, "VOW Vehicles"); gem.nummodules = 15; gem.numvehicles = 5; strcpy(gem.name, "GEM Vehicles"); sprintf(temp1, "Name is %s", gem.name); WriteInfo(temp1); sprintf(temp1, "Number of vehicles is %i in the %s cars", gem.numvehicles, gem.name); WriteInfo(temp1); sprintf(temp1, "Number of modules is %i ", gem.nummodules ,"in the %s", gem.name, " cars"); WriteInfo(temp1); sprintf (temp1, "Name is %s", vow.name); WriteInfo(temp1); sprintf(temp1, "Number of vehicles is %i in the %s cars", vow.numvehicles, vow.name); WriteInfo(temp1); sprintf(temp1, "Number of modules %i in the %s cars", vow.nummodules, vow.name); WriteInfo(temp1); sprintf(temp1, "Number of modules is %i in the %s cars", vow.nummodules, vow.name); } void printfunction(void) { /* Function prints an interger value*/ char temp[30]; int b = 78; sprintf (temp, "Print integer %i", b); WriteInfo(temp); } void OnError(uchar channel, uchar error_type) { /*Display message if an Error occurs*/ WriteInfo("On Error Event"); } void OnFrame(DataFrame *frame, uchar channel) { char temp[50]; int length; if (frame->mode & MODE_TX) { WriteInfo("OnFrame Event - Transmission"); } else if (frame->mode & MODE_RX) { WriteInfo("OnFrame Event - Receive"); } sprintf(temp,"channel is %i", channel); WriteInfo(temp); if (channel == 1) { WriteInfo("High Speed"); } else WriteInfo("Speed unknown"); length = frame->Idlen; sprintf(temp, "Length of Frame passed is %d", length); WriteInfo(temp); return; } void OnKey(char character) { int i=0; double test; int abso; int p1; long z1; char temp[50]; p1 = 0; WriteInfo("In the OnKey event"); x = CreateDataFrame(29,8,0); /*Create a data frame extended CAN with frame header of 0x00234523, data of x66, x77 */ x->Idlen = 29; x->datalen = 2; x->extralen = 0; ID(x)[0] = 0x00; ID(x)[1] = 0x23; ID(x)[2] = 0x45; ID(x)[3] = 0x23; DATA(x)[0] = 0x66; DATA(x)[1] = 0x77; /*Create a data frame extended CAN with frame header of 0x0022, */ /* data of xAA, xBB xCC, xDD, XEE */ y = CreateDataFrame(11,8,0); y->Idlen = 11; y->datalen = 5; y->extralen = 0; ID(y)[0] = 0x00; ID(y)[1] = 0x22; DATA(y)[0] = 0xAA; DATA(y)[1 ]= 0xBB; DATA(y)[2]= 0xCC; DATA(y)[3] = 0xDD; DATA(y)[4] = 0xEE; t = CreateDataFrame(11,8,0); /*Create a data frame extended CAN with frame header of 0x100, data of x66, x77 */ t->Idlen = 11; t->datalen = 2; t->extralen = 0; ID(t)[0] = 0x01; ID(t)[1] = 0x00; DATA(t)[0] = 0x66; DATA(t)[1] = 0x77; if (character == 'A') { i = TransmitDataFrame(x ,1); if (i == 1) { WriteInfo("Transmission Failed"); } } else if (character == 'B') { i = TransmitDataFrame(y,1); if (i == 1) { WriteInfo("Transmission Failed"); } } else if (character == 'D') { abso = abs(-5); sprintf(temp, "ABSOLUTE VALUE %i ", abso); WriteInfo(temp); z1 = atol("200"); sprintf(temp, "Convert string to long %d", z1); WriteInfo(temp); } else if (character == 'J') { definestructanddisplay(); } else if (character == 'P') { /*To start timer press control P */ WriteInfo("Starting Timer "); StartTimer(0x01, 200, OnTimer); } else if (character == 'K') { /*To kill timer press control K */ StopTimer(0x01); } else if (character == 'S') { /*To kill ONSTART timer press control S */ WriteInfo("Stopped On Start Timer"); StopTimer(0x02); } else if (character == 'M') { function2(); } else if (character == 'T') { /*Send Frame*/ printfunction(); i = TransmitDataFrame(t,1); v= CreateDataFrame(29,8,0); /*Create a data frame extended CAN with frame header of 0x00234523, data of x66, x77 */ v->Idlen = 29; v->datalen = 2; v->extralen = 0; ID(v)[0] = 0x00; ID(v)[1] = 0x23; ID(v)[2] = 0x45; ID(v)[3] = 0x23; DATA(v)[0] = 0x66; DATA(v)[1] = 0x77; i = TransmitDataFrame(v,1); } else { sprintf(temp, "Logged character %d", character); WriteInfo(temp); } } void OnStart(long time) { int s; char temp[30]; s = -2; s =abs(s); g = 15; StartTimer (0x02, 100, OnTimer); WriteInfo("The Timer 2 will continue displaying messages until stopped"); sprintf(temp, "value of g is %d", g); WriteInfo(temp); sprintf(temp, "absolute value of s is %i", s); WriteInfo(temp); strcpy(string1, "string copy to string1"); if (string2 == NULL) { WriteInfo("This pointer string2 is NULL"); } else { WriteInfo("The pointer string2 is NOT NULL"); } WriteInfo("OnStart Event occurrence and set Timer"); WriteInfo("Press Ctrl S to stop Timer 2"); WriteInfo ("Ctrl A transmits EXT CAN frame on channel 1"); WriteInfo ("Ctrl B Transmits STD CAN frame on channel1 1"); WriteInfo ("Ctrl D shows absolute value and coverts string to long value"); WriteInfo ("Ctrl J calls function that has user defined struct and displays info"); WriteInfo ("Ctrl M calls function and displays value"); WriteInfo("Ctrl P starts a timer"); WriteInfo("Ctrl K kills a timer"); WriteInfo("Ctrl T transmits two frames"); return; } void OnStop(long time) { if (FreeDataFrame(x) ==0) WriteInfo ("Memory Freed"); if (FreeDataFrame(y) == 0) WriteInfo("Memory Freed"); if (FreeDataFrame(t) == 0) WriteInfo("Memory Freed"); WriteInfo("Script Stopped"); return; } void OnTimer(uint timerid) { char temp[50]; sprintf(temp, "On Timer Even Occurrence at %d", timerid); WriteInfo(temp); return; } void OnTrigger(long time) { char temp[50]; int length; sprintf(temp, "123456789"); length = strlen(temp) - 1; sprintf(temp, "The length of text string temp \"12345678\" is %d", length ); WriteInfo(temp); sprintf(temp, "OnTrigger Event Occurence time %d", time); WriteInfo(temp); length = strlen(temp) -1; sprintf(temp, "The length of the the text string previously displayed is %d", length); WriteInfo(temp); return; } void OnSerial(const void * data, unsigned short datacount) { WriteInfo("OnSerial Event Occurrence"); return; } void OnMsg(char * string) { }