Announcement

Collapse
No announcement yet.

Another Serial Communication Question

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Another Serial Communication Question

    1.) So I am send via serial the following:
    "$" " An Integer of 0-150" "%"

    2.) I have control of the message sent and can make the integer a constant length ie 001 vs. 1.

    3.) I can change the start and end characters in this message ($ and %). I can also get rid of them.

    4.) I need to read in each numerical value received to a variable. I also need to save the old variable when the new one gets updated. I would like to update the variable 10-14 times per second. My message sender is not the limiting factor on baud rate.

    5.) Eventually the code will need to have several states, a reading state, a "set state", and an error state.

    I'm wondering if 4Dsystemsfan (Howard) can chime in. I read through his previous thread on serial communications and state machines. I think that currently my main troubles are that I don't have a good grasp of pointers. Right now I'm very stuck on how to combine the series of integers that I receive ie (1,2,3) into the number (123) and put it into a variable.
    #platform "uLCD-32PT_GFX2"
    #inherit "4DGL_16bitColours.fnc"

    var delay;
    var t;
    var wstack;
    var wstackold;
    var combuf;
    var setstack;
    var n;


    // Main function
    func main()
    // Initialization
    setbaud(BAUD_9600);
    setstack==0;
    //wstack:=20;


    // Recieve serial communication from PLC
    repeat
    wstackold:=wstack;
    // Read Buffer
    com_Init(combuf,5, 0) ;
    if (com_Full())
    n := com_Count();
    while(n>0,n--)
    wstack:= serin();
    wend
    endif
    com_Reset();

    // Tally consecutive values to check to see if decision has been made on weight
    if(wstack:=wstackold)
    setstack:=setstack+1;
    else
    setstack:=0;
    endif

    // Display weight stack value
    while(!media_Init());
    media_SetSector(0x0000, 0x0000);
    media_VideoFrame(0,0,wstack);

    // If weight stack value has not changed flash screen and set value
    if(setstack>40)
    flash();
    endif
    forever

    endfunc

    // Flash the set weight
    func flash()
    delay:=600;
    t:=sys_T()+delay;
    while(!media_Init());

    while(sys_T()!=t);
    media_SetSector(0x0000,0xA9ED);
    media_VideoFrame(0,0,wstack);

    t:=sys_T()+delay;
    while(sys_T()!=t);
    media_SetSector(0x0000,0x0000);
    media_VideoFrame(0,0,wstack);

    t:=sys_T()+delay;
    while(sys_T()!=t);
    media_SetSector(0x0000,0xA9ED);
    media_VideoFrame(0,0,wstack);

    t:=sys_T()+delay;
    while(sys_T()!=t);
    media_SetSector(0x0000,0x0000);
    media_VideoFrame(0,0,wstack);

    t:=sys_T()+delay;
    while(sys_T()!=t);
    media_SetSector(0x0001,0x5506);
    media_VideoFrame(0,0,wstack);
    endfunc

  • #2


    1.) Also just want to check to make sure but if I connect to the 5 pin male header this is com0?
    2.) Before it is brought up I have a converter that takes the serial signal from the plc to ttl. I am confident that it works correctly.

    Comment


    • #3


      If you only need to send between 0 and 150 send it as a byte then it's as the number when you get it at the screen -

      And I will bring it up - you say you have a converter that takes the serial from the pic to TTL ?...

      The pic will give the serial out as TTL levels ?

      And yes programming port ic com0

      Comment


      • #4


        PLC won't easily let me go from hex to octal so byte idea is out. Got everything solved and working for the time being. I will post an update later. I ended up using elements of 4Dsysfan's com state machine in this post:
        http://4d.websitetoolbox.com/post/picking-out-data-from-a-string-5821237?trail=25

        If you are interested in going from rs232 to ttl you might try this:
        http://www.maximintegrated.com/datasheet/index.mvp/id/1798
        "MAX 232ID"

        ...or if you don't want to build your own:
        http://www.commfront.com/rs232-rs485-rs422-serial-converters/RS232-RS485-RS422-to-TTL-Converters.htm

        Comment

        Working...
        X