Serial Interface in C: Reading Magswipe Data, Slides of Electrical Engineering

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

2012/2013

Uploaded on 07/24/2013

bulla.baba
bulla.baba 🇮🇳

5

(7)

87 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Serial C Programming 03/11/2008
Lecture 15 1
Serial Interface in C
Application to Magswipe
Lecture Content
In this lecture, we have a broken-up a C-code that
reads magswipe data into its composite chunks
We will go through these chunks, describing their
functions and bits of new stuff
Your job will be to put the chunks together and get it
all to work
We start with a template of where things go…
Magswipe Handling Program Outline
#include <various things, including coni o.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 results
CloseHandle(hSerial);
return 0;
}
Serial Port Access in Windows (in 3 pieces)
#include <fcntl.h>
#include <errno.h>
#include <windows.h>
////////////////////////
// Open COMXX (COM1 if 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
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Serial Interface in C: Reading Magswipe Data and more Slides Electrical Engineering in PDF only on Docsity!

Serial Interface in CApplication to Magswipe

Lecture Content• In this lecture, we have a broken-up a C-code thatreads magswipe data into its composite chunks• We will go through these chunks, describing theirfunctions and bits of new stuff• Your job will be to put the chunks together and get itall to work• We start with a template of where things go…

Magswipe Handling Program Outline #include^ <various^ things,^ including

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;}

Serial Port Access in Windows (in 3 pieces) #include^ <fcntl.h>#include^ <errno.h>#include^ <windows.h>//////////////////////////^ Open^ COMXX^ (COM1^ if^

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

Docsity.com

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

=^ CBR_9600;

dcbSerialParams.ByteSize

=^ 8;

dcbSerialParams.StopBits

=^ ONESTOPBIT;

dcbSerialParams.Parity^

=^ NOPARITY;

if(!SetCommState(hSerial,

&dcbSerialParams)) {printf("Error^ setting

State.\n");CloseHandle(hSerial);exit(-1); } COMMTIMEOUTS^ timeouts^ =

timeouts.ReadIntervalTimeout

=^ 50;

timeouts.ReadTotalTimeoutConstant

=^ 50;

timeouts.ReadTotalTimeoutMultiplier

=^ 10;

timeouts.WriteTotalTimeoutConstant

=^ 50;

timeouts.WriteTotalTimeoutMultiplier

=^ 10;

if(!SetCommTimeouts(hSerial,

&timeouts)) {printf("Error^ setting

timeouts.\n");CloseHandle(hSerial);exit(-1); } // END^ Open^ COM1////////////////////////

The funky stuff

•^ Lots of weirdness accompanied that last bit– much of it derived from

windows.h • http://source.winehq.org/source/include/windows.h

  • in particular^ winbase.h

(included within^ windows.h

•^ http://source.winehq.org/source/include/winbase.h• Typedefs– new variable types may be defined to augment the standard ones– example:^ typdef unsigned char

int8;

-^ now can use:^ int8 my_variable;

in declaration

  • example from^ windef.h

(included from^ windows.h

  • typedef unsigned long

DWORD;

  • typedef int^

BOOL;

  • typedef unsigned char

BYTE;

Structures

•^ Sometimes want to lump data together undercommon variable– now^ person2.gpa

^ 1.324,^ person2.name[0]

^ ‘M’

  • can assign^ person1.student_id = 0498213

, etc.

struct^ {int^ student_id;char^ name[80];char^ major[8];double^ gpa;}^ person1,^ person2={0578829,”Mot– in above, initialized one in declaration, but not both•^ can do anything you want•^ not restricted to two, for that matter

Turphy”,”PHYS”,1.324};

Docsity.com

unsigned^ int^ i,parity;//^ within^ loop…parity^ =^ 0;for^ (i=0;^ i<XX;^ i++){parity^ +=^ (inbyte

^ i)^ &^ 0x01;^ //^

count^ the^ number^ of^ ones

Checking parity for each byte• The magswipe stream should always obey odd parity– odd number of ones in packet– error checking scheme• The following within the byte-processing loop countsthe ones: }

Keeping track of the LRC unsigned short^ LRC=0,LRCflag=1;// within loop…if(LRCflag){LRC ^= inbyte;^ //^ Calculate^ Longitudinal

Redundancy^ Check }if(inbyte^ ==^ 0xXX){LRCflag^ =^ XX;^ //^ Stop

calculating^ LRC^ after^

End^ Sentinel

•^ The Longitudinal Redundancy Check is a final checkon data integrity– in case a two-bit error satisfied parity by accident•^ A common method is XOR– XOR all data within stream, byte-by-byte to arrive at finalLRC }

#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",

String Formatting inbyte,code,out_char_arr,parity);

Notes on String Formatting• Strings are ugly in C– for out_char_arr[2], initialize as

“x”

-^ effectively the same as: • out_char_arr[0]^ =^ ‘x’ • out_char_arr[1]^ =^ ‘\0’ – drop-in replacement of^ out_char_arr[0]

in program maintains

the necessary^ ‘\0’^ at the end of the string– concatenate entries onto out_string via:^ • strcat(out_string,out_char_arr); – place values into^ par_char_arr[]

via^ sprintf()

  • sprintf()^ takes care of the

‘\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

Docsity.com

char^ LRCchar;//^ after^ loop^ is^ done…printf("%s\n",out_string);

//^ print

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);

Final output^

Putting it together• The program snippets preceding should be enough toget the magswipe working– but for the love of all that is good, please place declarationsand initialization stuff together, not piecewise as in lecture• What comes out looks like: Hit ^ to^ stop^ stream^ and^ exitGot inbyte^ 0b;^ code^ 11;^ char^ ;^ with

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

Docsity.com