From Honsinger@studentmail.wsd.wednet.edu Mon Jan 3 15:21:12 2000 Received: from mxu4.u.washington.edu (mxu4.u.washington.edu [140.142.33.8]) by lists.u.washington.edu (8.9.3+UW99.09/8.9.3+UW99.09) with ESMTP id PAA30192 for ; Mon, 3 Jan 2000 15:21:11 -0800 Received: from studentmail.wsd.wednet.edu ([168.99.106.249]) by mxu4.u.washington.edu (8.9.3+UW99.09/8.9.3+UW99.09) with ESMTP id PAA23225 for ; Mon, 3 Jan 2000 15:21:11 -0800 Received: from mandrake.internet.wsd.wednet.edu (168.99.104.147) by studentmail.wsd.wednet.edu with SMTP (Apple Internet Mail Server 1.1); Mon, 3 Jan 2000 16:26:39 +0000 From: Benjamin Honsinger To: linux@u.washington.edu Subject: Trouble with serial port Date: Mon, 3 Jan 2000 15:09:49 -0800 X-Mailer: KMail [version 1.0.28] Content-Type: text/plain MIME-Version: 1.0 Message-Id: <00010315185102.24285@mandrake.internet.wsd.wednet.edu> Content-Transfer-Encoding: 8bit Hi, I have a program that I've written in C. It compiles correctly, but gives an error when it tries to use the serial port. Specifically, at line 239 of this message it gives the error "Didn't put LOOP!". Any help as to what is wrong (and maybe how to fix it) would be greatly appreciated, as I am fast nearing a deadline for this project to be completed. Please don't tell me to read the serial programming howto, I already have and it hasn't helped me. Thanks again for any help! Also, if anyone knows of a linux programming list I'd be glad to know of it, since that is a more appropiate place to post this. I don't have access to newsgroups though, so it has to be mail. Benjamin /* Program Testing.c - Program to communicate with the Weather Station. This is a working code file that is NOT the final product. This file includes error-checking, some that will be removed for the final version. */ #include #include "Ccitt.h" #define SUCCEEDED 1 #define FAILED 0 #define ACK 0x06 #define SERIAL_PORT FILE* ttyS0 // should I use this? void foo(FILE* serial_port); /* Open serial port for reading and writing */ int main(void) { FILE *serial_port; serial_port = fopen("/dev/ttyS0", "r+"); if (serial_port != NULL) { foo(serial_port); } else {printf("Failed to open serial port correctly\n");} } /* Write a character to the serial port Return value: SUCCEEDED if write succeeded, FAILED if write failed */ int put_serial_char(unsigned char c, FILE* ttyS0) { int success = SUCCEEDED; //Assume we'll succeed int i = putc(c, ttyS0); if(EOF == i) { success = FAILED; // i equals eof - we failed } return success; } /* Get a character from the serial port Return value: SUCCEEDED if write succeeded, FAILED if write failed */ int get_serial_char(unsigned char* c, FILE* ttyS0) { int success = SUCCEEDED; //Assume we'll succeed int r = getc(ttyS0); // int r is assigned the value from the serial port if(EOF == r) // if eof is equal to r, success = FAILED; // set success to FAILED else { *c = (unsigned char) r; // *c is assigned the char part of int r } return success; } /* Output a string to the current serial port Return value: SUCCEEDED if write succeeds, FAILED if write fails. */ int put_serial_string(char* s, FILE* ttyS0) { int success = SUCCEEDED; int i; for(i = 0; s [i] != '\0' && success == SUCCEEDED; i++) put_serial_char(s [i], ttyS0); return success; } /* Output an unsigned integer (16 bits) to the serial port */ int send_unsigned(unsigned int d, FILE* ttyS0) { unsigned char* cp; cp = (unsigned char *) &d; put_serial_char(*cp, ttyS0); put_serial_char(*(cp+1), ttyS0); } /* Anticipate and ACK to verify that the link has understood the command it has just received. If the character received is not ACK, or there has been an error, return FAILED, otherwise return SUCCEEDED */ int get_acknowledge(FILE* ttyS0) { int success = SUCCEEDED; //Assume we'll succeed char serial_char; //Character from serial port using get_serial_char char* x; int return_value; x = &serial_char; return_value = get_serial_char(x, ttyS0); if ( SUCCEEDED == return_value ) { // We successfully got a character, now compare it to ACK if ( ACK == serial_char ) { // We got an ACK, everything is successful!! success = SUCCEEDED; } else { // We didn't get an ACK, we lose success = FAILED; } } else { // get_serial_char didn't succeed! success = FAILED; } return success; } /* Read a series of bytes from the serial port and store them in a receive buffer */ int fill_buffer( char* buffer, // Pointer to the start of character buffer int buffer_size, // Number of characters to store in buffer FILE* ttyS0 // Stream to read characters from ) { int i; // Number of characters read so far char c; // Character (or EOF) most recently read from serial port int success; // Success of last character read for (i = 0; i < buffer_size; i++) // Read buffer_size chars into buffer { success = get_serial_char(&c, ttyS0); // Read in one character if (success == SUCCEEDED) // if we read a character successfully, buffer[i] = c; // Put the character read into the buffer else return FAILED; // Return an error } return SUCCEEDED; // Everything is fine } /* Read a series of bytes from the serial port, perform CRC check on data, and store the data in the receive buffer */ int fill_crc_buffer( char* buffer, // Pointer to the start of character buffer int buffer_size, // Number of characters to store in buffer FILE* ttyS0 // Stream to read characters from ) { int i; // Number of characters read so far char c; // Character (or EOF) most recently read from serial port int success; // Success of last character read int crc = 0; for(i = 0; i < buffer_size; i++) // Read buffer_size chars into buffer { success = get_serial_char(&c, ttyS0); // Read in one character if (success == SUCCEEDED) // if we read a character successfully, { buffer[i] = c; // Put the character read into the buffer crc = crc_table [(crc >> 8) ^ c] ^ (crc << 8); // crc check } else return FAILED; //error } if (crc == 0) return SUCCEEDED; // Everything is fine else return FAILED; // CRC_ERROR } /* Start of data extraction from the weatherstation */ void foo(FILE* serial_port) { char inside_temperature; char outside_temperature; char wind_speed; char wind_direction; char barometer; char inside_humidity; char outside_humidity; char rain; char c; char receive_buffer[17]; int header; int CRC_Error; //send loop command if(SUCCEEDED != put_serial_string ("LOOP", serial_port)); { printf("Didn't put LOOP!"); return; } //request 1 loop packet if(SUCCEEDED != send_unsigned ((unsigned) (65535), serial_port)); { printf("Didn't send 65535!\n"); return; } if(SUCCEEDED != put_serial_char (0x0d, serial_port)); {printf("Didn't put 0x0d!\n"); return;} //verify the command was received and got ACK if(SUCCEEDED != get_acknowledge (serial_port)); {printf("Didn't get ACK!\n"); return;} //should be the block header byte header = get_serial_char(&c, serial_port); if(header =! SUCCEEDED) {printf("Didn't read block header byte!\n"); return;} CRC_Error = fill_crc_buffer(receive_buffer, 17, serial_port); if(SUCCEEDED == CRC_Error) { inside_temperature = *((int *) (receive_buffer+0)); outside_temperature = *((int *) (receive_buffer+2)); wind_speed = *((unsigned char *) (receive_buffer+4)); wind_direction = *((int *) (receive_buffer+5)); barometer = *((int *) (receive_buffer+7)); inside_humidity = *((unsigned char *) (receive_buffer+9)); outside_humidity = *((unsigned char*) (receive_buffer+10)); rain = *((unsigned *) (receive_buffer+11)); printf("Inside temp: %d, Outside temp: %d\n", inside_temperature, outside_temperature); printf("Wind Speed: %d, Wind direction: %d\n", wind_speed, wind_direction); printf("Barometer: %d, Rain: %d\n", barometer, rain); printf("Inside humidity: %d, Outside humidity: %d\n", inside_humidity, outside_humidity); } else { printf("Error reading buffer from weather station!\n"); } } .