DOC PREVIEW
UCSD PHYS 121 - Serial Interface in C

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Serial Interface in CLecture ContentMagswipe Handling Program OutlineSerial Port Access in Windows (in 3 pieces)Slide 5Slide 6The funky stuffStructuresTypdeffing structures, yo5 or 7 bits?Apply MasksBitwise operators in CChecking parity for each byteKeeping track of the LRCString FormattingNotes on String FormattingFinal outputPutting it togetherSerial Interface in CSerial Interface in CApplication to MagswipeApplication to MagswipeWinter 2012UCSD: Physics 121; 20122Lecture ContentLecture Content•In this lecture, we have a broken-up a C-code that In this lecture, we have a broken-up a C-code that reads magswipe data into its composite chunksreads magswipe data into its composite chunks•We will go through these chunks, describing their We will go through these chunks, describing their functions and bits of new stufffunctions and bits of new stuff•Your job will be to put the chunks together and get it Your job will be to put the chunks together and get it all to workall to work•We start with a template of where things go…We start with a template of where things go…Winter 2012UCSD: Physics 121; 20123Magswipe Handling Program OutlineMagswipe 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 results CloseHandle(hSerial); return 0;}Winter 2012UCSD: Physics 121; 20124Serial Port Access in Windows (in 3 pieces)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 CrevelingWinter 2012UCSD: Physics 121; 20125 if(hSerial==INVALID_HANDLE_VALUE) { if(GetLastError()==ERROR_FILE_NOT_FOUND) printf("File Not Found.\n"); else printf("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;Winter 2012UCSD: Physics 121; 20126 if(!SetCommState(hSerial, &dcbSerialParams)) { printf("Error setting State.\n"); CloseHandle(hSerial); exit(-1); } COMMTIMEOUTS timeouts = {0}; 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////////////////////////Winter 2012UCSD: Physics 121; 20127The funky stuffThe funky stuff•Lots of weirdness accompanied that last bitLots 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•TypedefsTypedefs–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;Winter 2012UCSD: Physics 121; 20128StructuresStructures•Sometimes want to lump data together under Sometimes want to lump data together under common variablecommon variable–now person2.gpa  1.324, person2.name[0]  ‘M’–can assign person1.student_id = 0498213, etc.–in above, initialized one in declaration, but not both•can do anything you want•not restricted to two, for that matterstruct { int student_id; char name[80]; char major[8]; double gpa;} person1, person2={0578829,”Mot Turphy”,”PHYS”,1.324};Winter 2012UCSD: Physics 121; 20129Typdeffing structures, yoTypdeffing structures, yo•If we’re going to use the same structure a lot:If we’re going to use the same structure a lot:–Student is a new variable type, which happens to be a structure–now if we want to create a student, we declare as such:•Student stud1;•Student stud2={05788829,”Mot Turphy”,”PHYS”,1.324};–example from winbase.h (included from windows.h)typedef struct{ int student_id; char name[80]; char major[8]; double gps;} Student;typedef struct _COMMTIMEOUTS { DWORD ReadIntervalTimeout; // Max time between read chars. DWORD ReadTotalTimeoutMultiplier; // Multiplier of characters. DWORD ReadTotalTimeoutConstant; // Constant in milliseconds. DWORD WriteTotalTimeoutMultiplier; // Multiplier of characters. DWORD WriteTotalTimeoutConstant; // Constant in milliseconds.} COMMTIMEOUTS,*LPCOMMTIMEOUTS;Winter 2012UCSD: Physics 121; 2012105 or 7 bits?5 or 7 bits?•We need to tell program how to interpret the dataWe need to tell program how to interpret the data–as 5-bit (track 2) or 7-bit (tracks 1 and 3)•Use command line argument to setUse command line argument to set•maskmask determines which bits we pay attention to determines which bits we pay attention to unsigned int n_bits; unsigned char mask; n_bits = 5; // default is 5 bits per word if (argc > XX) { sscanf(argv[XX],"%d",&n_bits); } if (n_bits == 5) mask = 0x0f; // want 4 LSB: 00001111 if (n_bits == 7) mask = 0xXX; // want 6 LSB: 00111111Winter 2012UCSD: Physics 121; 201211Apply MasksApply Masks•Once we read the input byte, we apply masks to Once we read the input byte, we apply masks to concentrate on the parts we wantconcentrate on the parts we want#include <stdio.h> unsigned int code; unsigned char inbyte; char sInBuff[51] = {0}; DWORD dwBytesRead = 0; printf("Hit any key when


View Full Document

UCSD PHYS 121 - Serial Interface in C

Download Serial Interface in C
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Serial Interface in C and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Serial Interface in C 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?