DOC PREVIEW
UCSD PHYS 121 - Serial C Programming

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 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 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Serial C Programming 03/11/2008 Lecture 15 1 Winter 2012 UCSD: Physics 121; 2012 2 Winter 2012 UCSD: Physics 121; 2012 3 #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 2012 UCSD: Physics 121; 2012 4 #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 CrevelingSerial C Programming 03/11/2008 Lecture 15 2 Winter 2012 UCSD: Physics 121; 2012 5 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 2012 UCSD: Physics 121; 2012 6 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 2012 UCSD: Physics 121; 2012 7 Winter 2012 UCSD: Physics 121; 2012 8 struct {! int student_id;! char name[80];! char major[8];! double gpa;!} person1, person2={0578829,”Mot Turphy”,”PHYS”,1.324};!Serial C Programming 03/11/2008 Lecture 15 3 Winter 2012 UCSD: Physics 121; 2012 9 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 2012 UCSD: Physics 121; 2012 10 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: 00111111 Winter 2012 UCSD: Physics 121; 2012 11 #include <stdio.h>! unsigned int code;! unsigned char inbyte;! char sInBuff[51] = {0};! DWORD dwBytesRead = 0;! printf("Hit any key when finished\n");! while (!kbhit())! {! ReadFile(hSerial, sInBuff, 1, &dwBytesRead, NULL);! if (dwBytesRead > 0)! { // if any bytes! inbyte = sInBuff[0] & 0xFF; // mask to 8 bits! code = inbyte & mask; // mask to relevant data!Winter 2012 UCSD: Physics 121; 2012 12Serial C Programming 03/11/2008 Lecture 15 4 Winter 2012 UCSD: Physics 121; 2012 13 unsigned int i,parity;! // within loop…! parity = 0;! for (i=0; i<XX; i++)! {! parity += (inbyte >> i) & 0x01; // count the number of ones! }!Winter 2012 UCSD: Physics 121; 2012 14 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! }!Winter 2012 UCSD: Physics 121; 2012 15 #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",! inbyte,code,out_char_arr,parity);!Winter 2012 UCSD: Physics 121; 2012 16Serial C Programming 03/11/2008 Lecture 15 5 Winter 2012 UCSD: Physics 121; 2012 17 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);!Winter 2012 UCSD: Physics 121; 2012 18 Hit <Enter> to stop stream and exit!Got 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 =


View Full Document

UCSD PHYS 121 - Serial C Programming

Download Serial C Programming
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 C Programming 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 C Programming 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?