



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A lecture note from a serial c programming course, focusing on reading magswipe data using c language. It covers opening a serial port, handling errors, and applying masks to concentrate on the desired data bits. The lecture also explains the concept of structures, typedefs, and bitwise operators. The goal is to help students understand how to read and process data from a magswipe device using c programming.
Typology: Slides
1 / 5
This page cannot be seen from the preview
Don't miss anything!




conio.h> int^ main(int^ argc,^ char*
argv[]) {//^ type^ definitions//^ open^ serial^ port^ as
COMXX^ (COM1^ if^ built-in)// establish whether 5 or^7 bits^ through^ argv[1],
and^ pick^ mask while^ (!kbhit()){ReadFile(hSerial,^ sInBuff,
1,^ &dwBytesRead,^ NULL); if^ (dwBytesRead^ >^ 0){//^ apply^ masks//^ parity^ check//^ LRC^ calculation//^ string^ formatting}}// print^ resultsCloseHandle(hSerial);return^ 0;}
built-in)HANDLE hSerial;hSerial = CreateFile("COMXX",GENERIC_READ^ |^ GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); Windows serial code worked out by Dan Creveling
if(hSerial==INVALID_HANDLE_VALUE){if(GetLastError()==ERROR_FILE_NOT_FOUND)printf("File^ Not^
Found.\n");elseprintf("Generic Error.\n");exit(-1); } DCB^ dcbSerialParams^ =^ {0};dcbSerialParams.DCBlength=sizeof(dcbSerialParams);if(!GetCommState(hSerial,
&dcbSerialParams)) {printf("Error^ Getting
State.\n");CloseHandle(hSerial);exit(-1); } dcbSerialParams.BaudRate
dcbSerialParams.ByteSize
dcbSerialParams.StopBits
dcbSerialParams.Parity^
if(!SetCommState(hSerial,
&dcbSerialParams)) {printf("Error^ setting
State.\n");CloseHandle(hSerial);exit(-1); } COMMTIMEOUTS^ timeouts^ =
timeouts.ReadIntervalTimeout
timeouts.ReadTotalTimeoutConstant
timeouts.ReadTotalTimeoutMultiplier
timeouts.WriteTotalTimeoutConstant
timeouts.WriteTotalTimeoutMultiplier
if(!SetCommTimeouts(hSerial,
&timeouts)) {printf("Error^ setting
timeouts.\n");CloseHandle(hSerial);exit(-1); } // END^ Open^ COM1////////////////////////
-^ now can use:^ int8 my_variable;
Turphy”,”PHYS”,1.324};
unsigned^ int^ i,parity;//^ within^ loop…parity^ =^ 0;for^ (i=0;^ i<XX;^ i++){parity^ +=^ (inbyte
^ i)^ &^ 0x01;^ //^
count^ the^ number^ of^ ones
Redundancy^ Check }if(inbyte^ ==^ 0xXX){LRCflag^ =^ XX;^ //^ Stop
calculating^ LRC^ after^
End^ Sentinel
#include^ <string.h>char^ out_string[80]="",out_char_arr[2]=“x”;char^ parity_string[80]="",par_char_arr[2];char^ charmap5[17]="0123456789:;<=>?";char^ charmap7[65]="^ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_";//^ within^ loop…if^ (n_bits^ ==^ XX)
out_char_arr[0]^ =^ charmap5[code];if (XX == XX) out_char_arr[XX]^ =^ XX[XX];strcat(out_string,out_char_arr);sprintf(par_char_arr,"%d",XX);^ //
write^ parity^ into^ string XX(parity_string,par_char_arr);
//^ append^ char^
to^ string XX("Got^ inbyte^ %02x;^ code
%2d;^ char^ %s^ with^ parity
=^ %XX\n",
-^ effectively the same as: • out_char_arr[0]^ =^ ‘x’ • out_char_arr[1]^ =^ ‘\0’ – drop-in replacement of^ out_char_arr[0]
‘\0’^ automatically
-^ could also say:^ par_char_arr[0]
=^ '0'^ +^ parity;
-^ adds the parity to the character code: relies on ASCII table’s order• The string.h^ library contains a number of usefulmanipulations for strings– but this doesn’t wholly make up for the deficit
char^ LRCchar;//^ after^ loop^ is^ done…printf("%s\n",out_string);
composite^ string printf("%XX\n",parity_string);
//^ print^
parity^ string printf(“LRC^ =^ %d\n”,LRC);if^ (n_bits^ ==^ 5)^ LRCchar
=^ charmap5[LRC^ &^ mask]; if^ (XX^ XX^ XX)^ XX^ =^ XX[XX];
//^ same^ deal^ for
7-bit printf(“LRC^ =^ %c\n”,LRCchar);
parity^ =^3 Got^ inbyte^ 04;^ code^ 4;
char^4 with^ parity^ =^1 Got^ inbyte^ 07;^ code^ 7;
char^7 with^ parity^ =^3 Got^ inbyte^ 01;^ code^ 1;
char^1 with^ parity^ =^1 Got^ inbyte^ 02;^ code^ 2;
char^2 with^ parity^ =^1 :Got^ inbyte^ 10;^ code^ 0;
char^0 with^ parity^ =^1 Got^ inbyte^ 10;^ code^ 0;
char^0 with^ parity^ =^1 Got^ inbyte^ 1f;^ code^ 15;
char^?^ with^ parity^ =^5 Got^ inbyte^ 16;^ code^ 6;
char^6 with^ parity^ =^3 Got^ inbyte^ 00;^ code^ 0;
char^0 with^ parity^ =^0 Got^ inbyte^ 00;^ code^ 0;
char^0 with^ parity^ =^0 : ;4712584314171608=081210130451458800000?600000 3131131131113131131111111311311311111115300000