interfacing to the pc serial port


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2007
    Posts
    5

    Default interfacing to the pc serial port

    Hello all:

    I'm a long time computer programmer, so so electronics background and new to pics.

    My immediate goals:
    1. control my laptop through the pic using pulsin generated by infrared.
    2. control my pic from my laptop.

    My longer term goals:
    1. continuous pic to laptop communication with no human intervention
    2. accomplish the above on a severe shoestring

    I have had proof-of-concept success in my immediate goals but with inconsistencies and anomalies.

    A little hardware and software background:
    1. My pic setup is breadboarded
    2. I am using picbasic demo
    3. I am using a straight through 9 pin d type female serial cable with 20 gauge wires stuck in the appropriate holes secured with a rubber band and alligator leads going to the breadboard.
    4. I was using hyperterminal to send and receive data through the serial port but it isn't gui capable. I am now using PHP since I know it quite well and have apache/php already set up on my laptop.
    5. Serial input and output electronics and software are configured as per the pbp manual.

    Once I secured the wires to the d connector and became more aware of not stepping on cables, not letting things fall on the floor and not stressing temporary connections, the hardware setup became stable.

    Now for the inconsistencies and anomalies.
    1. As I stated above I was using hyperterminal to communicate with the serial port. In my PHP input program, whether at the command line or on the browser, the program will hang unless I log into hyperterminal and log out with the correct settings. That action seems to set something in the background beyond what diagnostics I can display, but I can't tell what.

    Using PHP as output to the serial port requires no hyperterminal intervention.

    2.In the output mode, characters sent to the serial port and the pic through either hyperterminal or PHP result in a successful completion about 8 out of 10 times. I wasn't sure if it was a sticky keyboard problem with hyperterminal because the characters don't appear on the screen. With PHP the characters do appear on the screen and still the successful receive rate is 8 out of 10 times. Is there a timing thing going on here? Would "define char_spacing" help?

    ***

    One more question, unrelated to the above: Somehow I got all zereos on a 16f628 and it appears useless. I am using a pic programmer. Is there any recovery procedure available? What causes this?

    I have included the pic programs and the PHP programs below.

    Sorry for the length of this message. Thanks in advance to whoever takes the time to read it and reply.

    - K -

    *****************************************
    php code - to the pic ...
    - - - - - - - - - - - - -

    <?php
    $submit='';
    $word='';
    if (isset($_POST) && !empty($_POST)) {
    if (isset($_POST['word'])) {
    $word=$_POST['word'];
    echo "word=$word";
    }
    }
    if ($word) {
    `mode com1: baud=2400 parity=n data=8 stop=1 to=on xon=off octs=off odsr=off dtr=on rts=on

    idsr=off`;
    $fp = fopen ("com1:", "w+");
    if (!$fp) {
    echo "can't open port: com1";
    }
    else {
    if (fwrite($fp, $word) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
    }

    fclose($fp);
    }

    }


    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <?php
    echo "<input type=text size=2 maxlength=2 name=word><br>";
    echo "<input type=submit value=submit name=submit></form>";

    ?>

    ************************************************** **
    php code - from the pic ...
    - - - - - - - - - - - - - - -

    <?php

    `mode com1: baud=9600 parity=n data=8 stop=1 to=on xon=off octs=off odsr=off dtr=on rts=on idsr=off`;
    $char='';
    $fp = fopen ("com1:", "r");
    if (!$fp) {
    echo "can't open port: com1";
    }
    else {
    //while (!feof($fp)) {
    // $buffer = fgets($fp);
    // echo ord($buffer);
    //}
    // while (false !== ($char = fgetc($fp))) {
    for ($i=0;$i<=34;$i++) {
    $char = $char.fgetc($fp);
    }
    echo substr($char,19,16);
    //}
    echo "<br>finish 1<br>";
    fclose($fp);
    }

    ?>

    ************************************************** **
    serin pic code (16f628)
    - - - - - - - - - - - - - -
    SI var portb.1 ' Define serial in pin
    N2400 con 4 ' Set serial mode
    byteIn var byte
    low portb.4
    low portb.5
    low portb.7
    loop:
    Serin SI,N2400,["x"],Bytein
    if bytein = "6" then
    low portb.4
    low portb.5
    low portb.7
    goto loop
    endif
    if (bytein = "4") or (bytein = "5") then
    goto show4or5
    else
    high portb.7
    low portb.4
    low portb.5
    endif
    show4or5:
    If bytein = "4" Then ' 4 = input character
    low portb.5
    low portb.7
    high portb.4
    endif
    If bytein = "5" Then ' 5 = input character
    low portb.4
    low portb.7
    high portb.5
    endif
    goto loop

    *********************************************
    serout code ...
    - - - - - - - - - - -
    w0 var word
    so var portb.0
    N9600 con 6
    j var byte
    k var word
    pulseLoop:
    PULSIN PORTb.2,1,w0
    if w0 < 1 then
    goto pulseLoop
    else
    Serout SO,N9600,[" ",10]
    Serout SO,N9600,[#w0]
    Serout SO,N9600,["-w0 value"]
    endif
    ;low portb.0
    Serout SO,N9600,[" "]
    Serout SO,N9600,[#w0]
    Serout SO,N9600,["-"]
    k = w0
    for j = 0 to 15
    ;low j
    if k.15 then
    Serout SO,N9600,["1"]
    else
    Serout SO,N9600,["0"]
    endif
    k = w0 << (j + 1)
    next j
    pause 1500
    goto pulseloop
    ************

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    I am not very good with PHP but you may want to slow the PIC to PHP baud down to 2400 to test things.

    What type of oscillator are you using?

    A line feed and carriage return might help at the PHP receive end (10, 13) at the end of every transmission.

    One more question, unrelated to the above: Somehow I got all zereos on a 16f628 and it appears useless. I am using a pic programmer. Is there any recovery procedure available? What causes this?
    Have you tried a complete chip erase? what programmer and software?
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Jul 2007
    Posts
    5


    Did you find this post helpful? Yes | No

    Default interface to the serial port

    Hello Dave -

    Thanks for the timely reply.

    First of all I am using the Melabs U2 Programmer. I have only seen one erase feature. Is that the complete erase you refer to?

    As for the programming side I've returned to the PBP manual. Now I can't see where I got 9600 baud for serout so I will slow it down and see what happens.

    I think you mean I should have a carriage control and line feed for every serout command, not just the initial one. I'll give that a try and see what happens also.

    K

  4. #4
    Join Date
    Jul 2007
    Posts
    5


    Did you find this post helpful? Yes | No

    Default interface to serial port

    Left out one piece of the reply:

    I am using a 4mhz crystal oscillator with the markings ECSH4.0EX on top. Hope that's not an issue.

    K

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by kelangfei View Post
    Left out one piece of the reply:

    I am using a 4mhz crystal oscillator with the markings ECSH4.0EX on top. Hope that's not an issue.

    K
    Could be a little slow for 9600. 2400 will be fine.

    I think you mean I should have a carriage control and line feed for every serout command, not just the initial one. I'll give that a try and see what happens also.
    Yes, That is what I was trying to say.

    As far as your programmer, I do not use that one but being from Melabs I am sure it is good. There should be some place to erase the config, program, and eeprom areas, maybe it does it all at once.

    For the dead PIC see if you can be sure the config area is being erased and do a blank check. You may have to use MPLAB for this?
    Dave
    Always wear safety glasses while programming.

Similar Threads

  1. Send binary file to pic over pc serial port
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 3rd May 2011, 13:47
  2. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  3. PC serial port funny
    By nicjo in forum Serial
    Replies: 13
    Last Post: - 6th February 2007, 05:34
  4. Replies: 2
    Last Post: - 28th April 2006, 12:10
  5. PC Serial Port Connections
    By BigH in forum Serial
    Replies: 1
    Last Post: - 20th January 2006, 08:24

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts