Shop OBEX P1 Docs P2 Docs Learn Events
Novice having problems with Serout — Parallax Forums

Novice having problems with Serout

SSchafferSSchaffer Posts: 12
edited 2008-08-23 01:24 in BASIC Stamp
Hello,
···· I'm currently working on a program in C# communicating back and forth with my Basic Stamp controller (Homework Board). I have a serial cable coming out of Com 3 leading to my Microcontroller, with the Red wire (Receive) connected to Pin 0, the·Purple wire (Transmit) connected to Pin 1, and the Brown wire connected to Vss. All 3 wires have 22 kOhm Resistors soldered to the ends before going into the Pins.

···· I have no problem transmitting from my computer to the Basic Stamp. On the Stamp, I am using the commands:
LOOP1:
· DO
··· SERIN 0, 19697, 60000, LOOP1, [noparse][[/noparse]reader]
··· SERIN 0, 19697, [noparse][[/noparse]facility1]
··· SERIN 0, 19697, [noparse][[/noparse]facility2]
··· SERIN 0, 19697, [noparse][[/noparse]badge1]
··· SERIN 0, 19697, [noparse][[/noparse]badge2]
··· SERIN 0, 19697, [noparse][[/noparse]issue]
··· SERIN 0, 19697, [noparse][[/noparse]dooro]
··· <Other Code>
· LOOP

···· I am using the utility Portmon to confirm that the data I'm sending is travelling at 300 Baud and is correct. However, when I try to transmit from the Basic Stamp to my computer, it's not working:

SEROUT 1, 19697, [noparse][[/noparse]1]

···· Portmon is telling me that the the information is being sent at 9600 Baud, even though I specified 300 Baud, and what is being sent is a 42 hex instead of a 1.

···· Anyone happen to have seen this before or have an idea of how I could proceed?

···· Thanks,
········· Scott

Comments

  • CannibalRoboticsCannibalRobotics Posts: 535
    edited 2008-08-22 22:09
    The RS-232 specified signal level is + 12 and -12, a 24 volt swing.
    I've seen some serial ports not be able to properly establish a zero crossing when the incomming signal has only a 5v swing. I'd try pulling the 22K resistor off of the output line from the stamp to the computer. It's unlikely the stamp is going to produce enough current to damage anything on the CPU. Where is the ground on the serial line tied?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    A wise man told me; "All electronics are made to work by magic smoke.

    Don't ever let it out as it's·very difficult·to get it back in."
  • metron9metron9 Posts: 1,100
    edited 2008-08-23 01:08
    Perhaps this will help
    http://weimenglee.blogspot.com/2006/09/build-low-cost-ttl-to-rs232-level.html

    And you can read the first 6 chapters online of this book

    http://www.smileymicros.com/index.php?module=pagemaster&PAGE_user_op=view_page&PAGE_id=50

    And the prebuilt ones

    http://www.sparkfun.com/commerce/product_info.php?products_id=449





    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!

    Post Edited (metron9) : 8/23/2008 1:21:44 AM GMT
  • SSchafferSSchaffer Posts: 12
    edited 2008-08-23 01:12
    Hi,
    Thanks for the reply. I hadn't considered that possibility. All the wires, including the Ground wire, all have 22 KOhm resistors. The Ground wire on the Basic Stamp side is tied to Vss. I'll try removing the resistor on the Transmit wire. I suppose that I can take it off the Ground wire as well, correct?

    Thanks again,
    Scott
  • SSchafferSSchaffer Posts: 12
    edited 2008-08-23 01:20
    Hi Metron9,
    Thank you very much for the links. It looks very helpful. I've downloaded the book chapters and will look them over.

    Scott
  • metron9metron9 Posts: 1,100
    edited 2008-08-23 01:24
    I would not transmit a -12 and +12 volt signal directly to the stamp, poof!

    You need to use a level shifter. The higher voltage signals get converted to lower 0 and +5 and your 0 and +5 get converted to -12 and +12 so the computer com port can read the signals. Some laptops now can read 0 and +5 but just use a level shifter so it works on any computer.

    Another cute little device , I just got one is from smileymicros.com called the usb thingee check that out as it allows USB connection and your C programs can talk to the virtual COM port through the USB and it has the proper level shifters built in.
    And for those who may want to try C but not get into the C# or c++ giant bemoths the lcc-win32 compiler is great. Google it and give it a try. I just found this code on one of their forums. Pretty raw but I tested it with the usbthinge from smileymicros and output the letter "U" and captured it on my scope with this code. I will have to rewrite the code so it is easier to use but for now I know it works.

     
    
    /*
     Posted: 2004-09-21 19:49:01    Post subject:
    --------------------------------------------------------------------------------
    Here is my serial port interface routine, not elegant at all but it works for me!
    All I do is put this function in my program and it does all the serial port interaction I want (open port with a specific baud rate, check for characters, send characters, close the port again). The desired baud rate is sent as a text string, ie
    serial_port(OPEN, "115200");
    I never use anything except 8 data bits, 1 stop bit, no parity.
    */
    #include <stdio.h>
    #include <win.h>
    
    #define CLOSE 0
    #define OPEN 1
    #define RECEIVE 2
    #define SEND 3
    
    unsigned long serial_port(int control, char *data)
    {
     COMMTIMEOUTS hCT;
     DCB Dcb;
     static HANDLE HdComm;
     unsigned long clox;
     if (control == CLOSE) // Close serial port
     {
      do
      {
       ReadFile( HdComm, data, 1, &clox, NULL ); //Receive a char from COM1 (clox is 0 if none available)
      }  while (clox > 0); // Flush channel
      CloseHandle( HdComm );
      return(0);
     }
    
     else if (control == RECEIVE) // Read
     {
      ReadFile( HdComm, data, 1, &clox, NULL ); //Receive a char from COM1 (clox is 0 if none available)
      return(clox);
     }
     
     else if (control == SEND) //Write to COM1
     {
     WriteFile( HdComm, data, 1, &clox, NULL );
     return(0);
     }
    HdComm = CreateFile( "COM6", GENERIC_READ | GENERIC_WRITE, 0,
    NULL, OPEN_EXISTING, 0, NULL ); // Open the COMM port.
    SetupComm( HdComm, 1024, 1024 ); // Initialize COMM port.
    GetCommState( HdComm, &Dcb );
    sscanf(data, "%d", &clox);
    Dcb.BaudRate = clox; // was baud_128k
    Dcb.Parity = NOPARITY;
    Dcb.ByteSize = 8;
    Dcb.StopBits = ONESTOPBIT;
    SetCommState( HdComm, &Dcb );
    GetCommTimeouts(HdComm, &hCT);
    hCT.ReadIntervalTimeout = MAXDWORD;
    SetCommTimeouts(HdComm, &hCT);
    do
    {
     ReadFile( HdComm, data, 1, &clox, NULL ); //Receive a char from COM1 (clox is 0 if none available)
     } while (clox > 0); // Flush channel
     return(0);
    }
     
    int main(void)
    {
     serial_port(OPEN,"19200");
     serial_port(SEND,"U");
     serial_port(CLOSE,"1");   //The way this guy has the code you need a second byte sent so I just used "1"
    
    }
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!

    Post Edited (metron9) : 8/23/2008 3:46:04 AM GMT
Sign In or Register to comment.