element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Pic Microcontrollers Forum PIC 16f887 UART to PC
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 2 replies
  • Subscribers 190 subscribers
  • Views 407 views
  • Users 0 members are here
  • uart
  • rs232
  • serial
  • pic
Related

PIC 16f887 UART to PC

Former Member
Former Member over 10 years ago

Hello,

 

I am trying to send what I am calling a "device ID" to my PIC and have the PIC store it. The other half of this issue is that I am also writing a program in C that runs on on my PC that will accomplish this transaction. My expected results are I send an ID from the PC software and the PIC replies with the ID that it has received ("ID set to: 77). It does work perfectly the first time, however if I try to send a new ID, the PIC does not reply with ID only with "ID set to:". If I reset the PIC I can send in a new ID again, but only once.

 

Using the terminal application in Mikro C, I can send the ID, and resend the ID as much as I want without resetting the PIC.

 

My question for this discussion is should I be looking for problems in the code on the PC side or PIC side? Both are written in C, and my gut feeling is that its on the PC side since the terminal works, but why would resetting the PIC allow me to send a new ID Properly?

 

I can provide code if you like, but wont paste it now since this is more of a "where should I start" type of question.

 

I appreciate any thoughts and constructive advice.

 

Thanks,

George

  • Sign in to reply
  • Cancel

Top Replies

  • Former Member
    Former Member over 10 years ago in reply to Problemchild +1
    I have tried using Putty, and It worked fine as well. Here is the PIC C Code: char myidd[10]; void newline(){ UART1_write(13); UART1_write(10); } void main() { UART1_init(9600); ANSEL = ANSELH = 0; TRISA…
  • Problemchild
    0 Problemchild over 10 years ago

    I think most folk would be as confused as you as the answer is yeah both...ish probably!

    Obviously that's not the answer you want image !

     

    Please can you post the code or at least the section in question so that those who have the skills to help can take a look and offer some advice.

     

    BTW have you tried any other terminal software ?

    Any pictures or diagrams would be good as well.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to Problemchild

    I have tried using Putty, and It worked fine as well.

    Here is the PIC C Code:

     

    char myidd[10];

      

    void newline(){

      UART1_write(13);

      UART1_write(10);

    }

     

     

    void main() {

      UART1_init(9600);

      ANSEL = ANSELH = 0;

      TRISA = 255;

      TRISB = 255;

      TRISD = 0;

      PORTD = 0;

      for(;;){

      if (rb7_bit == 1){

          PORTD = 0b00000000;

        }

        if (rb0_bit == 1) {

          PORTD = 0b00000111;

          //delay_ms(3);

        

          if (UART1_data_ready){

            memset(myidd, 0, strlen(myidd));

            UART1_read_text(myidd, "id", 10);

            UART1_write_text("ID set to: ");

            UART1_write_text(myidd);

            newline();

          }

        }

      }

    }

     

     

    Here is the section of my program that should be relevant:

     

    void pgm_dev(){

        char com_port[7] = "COM";

      

        char dev_id[12];

        memset(dev_id, 0, sizeof(dev_id));

      

        char pic_input[256];

        memset(pic_input, 0, sizeof(pic_input));

      

        char dumberr[256];

        memset(dumberr, 0, sizeof(dumberr));

     

        system("cls");

        printf("Please enter the number of your Com Port: ");

        fgets (com_portpick, 4, stdin);

        strcat(com_port, com_portpick);

        str_length = strlen(com_port);

      

        if (com_port[str_length - 1] == '\n'){

            com_port[str_length - 1] = NULL;

        }

     

        DWORD  accessdirection =GENERIC_READ | GENERIC_WRITE;

        HANDLE hSerial = CreateFile(com_port, accessdirection, 0, 0, OPEN_EXISTING, 0, 0);

     

        if (hSerial == INVALID_HANDLE_VALUE) {

            FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), dumberr, 256, NULL);

                printf("Could not open selected COM port! (%s)\n", com_port);

                strcpy(com_port, "COM");

                printf("Press Enter to continue");

                getch();

                pgm_dev();

        }

     

        DCB dcbSerialParams = {0};

        dcbSerialParams.DCBlength=sizeof(dcbSerialParams);

     

        if (!GetCommState(hSerial, &dcbSerialParams)) {

               FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), dumberr, 256, NULL);

                printf("%s", dumberr);

        }

     

        dcbSerialParams.BaudRate=9600;

        dcbSerialParams.ByteSize=8;

        dcbSerialParams.StopBits=ONESTOPBIT;

        dcbSerialParams.Parity=NOPARITY;

     

        if(!SetCommState(hSerial, &dcbSerialParams)){

            FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), dumberr, 256, NULL);

                printf("%s", dumberr);

        }

     

        COMMTIMEOUTS timeouts={0};

        timeouts.ReadIntervalTimeout=50;

        timeouts.ReadTotalTimeoutConstant=50;

        timeouts.ReadTotalTimeoutMultiplier=10;

        timeouts.WriteTotalTimeoutConstant=50;

        timeouts.WriteTotalTimeoutMultiplier=10;

     

        if(!SetCommTimeouts(hSerial, &timeouts)){

            FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), dumberr, 256, NULL);

               printf("%s", dumberr);

        }

     

        printf("You have selected %s press Enter\n", com_port);

        getch();

        system("cls");

        printf("Please connect the device and press the PROGRAM button.\n");

        printf("Press ENTER when ready.\n");

        getch();

        system("cls");

        devid:

        printf("Enter desired device ID Up to 8 letters and numbers no special characters followed by \"id\": ");

        fgets (dev_id, 12, stdin);

      

        str_length = strlen(dev_id);

        if (dev_id[str_length - 3] != 'i' || dev_id[str_length - 2] != 'd'){

            if(dev_id[str_length -3] == 'I' || dev_id[str_length] == 'D'){

                printf("Must be lowercase!\n");

                printf("Press any key to try again\n");

                getch();

                system("cls");

                goto devid;

            }

            printf("You must folow with \"id\"\n");

            printf("Press any key to try again\n");

            getch();

            system("cls");

            goto devid;

        }

        system("cls");

                  

        // Write device ID

        DWORD dwBytesWrote = 0;

        !WriteFile(hSerial, dev_id, 12, &dwBytesWrote, NULL);

          

     

     

        //if (!WriteFile(hSerial, dev_id, 12, &dwBytesWrote, NULL)){

        //    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), dumberr, 256, NULL);

           //    printf("%s", dumberr);  

        // }

          

        // Read device ID from Device

        DWORD dwBytesRead = 0;

          

        //    if(!ReadFile(hSerial, pic_input, 256, &dwBytesRead, NULL)){

        //        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), dumberr, 256, NULL);

        //        printf("%s", dumberr);

        //    }

        !ReadFile(hSerial, pic_input, 256, &dwBytesRead, NULL);      

        //pic_input[dwBytesRead] = '\0';

           printf("%s", pic_input);

         

        //!ReadFile(hSerial, pic_input, 256, &dwBytesRead, NULL);

        //pic_input[dwBytesRead] = '\0';

        //printf("%s", pic_input);

        //!ReadFile(hSerial, pic_input, 256, &dwBytesRead, NULL);

        //pic_input[dwBytesRead] = '\0';

        //printf("%s", pic_input);

         

        printf("Press the clear button on the device, and press ENTER when ready.");

        getch();

        start_232f = 0;

        CloseHandle(hSerial);

        menurtn();

      

    }

     

     

    Thanks,

    George

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube