Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Use of RS232 port

k2teknik
2015-03-08
2015-04-14
  • k2teknik - 2015-03-08

    I am trying to communicat on the seriel port on my PLC with this code:

    (*Init comport*)
    TripCounter:=TripCounter + 1;
    IF NOT port_opened THEN
       com_handle:=SysComOpen(com_num);
       IF com_handle<>16#FFFFFFFF THEN
          port_init:=TRUE;
       ELSE
          SysComClose(com_num);
          com_handle:=SysComOpen(com_num);
          port_init:=TRUE;
       END_IF;
    (*ComPort settings*)
       com_set.Port:=com_num;
       com_set.dwBaudRate:=9600;
       com_set.byParity:=0;
       com_set.dwTimeout:=0;
       com_set.byStopBits:=1;
       com_set.dwBufferSize:=0;
       com_set.dwScan:=0;
       res:=SysComSetSettings(com_num,ADR(com_set));
    (*Setup com port*)
       IF NOT res THEN
       port_opened:=TRUE;
       END_IF
    END_IF
    

    I get my com_handle, but things fail when I try to set the settings on the com port, what do I do wrong here?

     
  • shooter - 2015-03-11

    is it an extra card or do you want the programming interface to talk (not possible)

     
  • k2teknik - 2015-03-11

    shooter hat geschrieben:
    is it an extra card or do you want the programming interface to talk (not possible)
    It is the port present on my PLC, and yes it is the programming interface too.
    So there is no chance to use that programming interface to something else at run time (run time for my PLC code that is)?

    BTW I am about to make my PLC send SMS via a GSM modem.

     
  • shooter - 2015-03-11

    what PLC is used?
    as codesys is running on > 100 types.

     
  • shooter - 2015-03-16

    sorry to bad in russian, to read it.

     
  • k2teknik - 2015-04-11

    shooter hat geschrieben:
    sorry to bad in russian, to read it.
    Me too.
    But anyway they claim that you can use the RS232 port, and with some question to there support, some reading on there forum I manage to use the port, but first I had to use the right cable, then add the port in the "PLC configuration", and then use the right COM port number.

    But that just gives me some new challenge:
    1.: How to reset/empty an array?, with a string I can just use MyString := '', but I can't make it work for an array.

    2.: How to join two strings? In VB6 I would use something like "NewString = FirstString + NextString"

     
  • shooter - 2015-04-11

    join is concat
    and no need to empty an array, however you can overwrite it with spaces
    another way is use oscat library.
    .

     
  • k2teknik - 2015-04-13

    Thanks for the fast answer.

    I have these two line of code that send a command to a GSM modem, and then get the answer:```

    SysComWrite(com_num,ADR(snd_str),LEN(snd_str),0);
    sz:=SysComRead(com_num,ADR(rcvBUF),1024,0);

    ```It works well when I single step the code, but the PLC is much faster that my GSM modem so when running the code it never get the answer from the modem.
    I had try to make a 5 seconds delay with a While-do loop, but it make my PLC reboot. Is there en more nice way to make a waiting function?

    In the help there is an example of "traffic light" with a wait thing, but it is in IL language and I am not ready for this yet.

     
  • shooter - 2015-04-13

    test until you see some characters in the buffer, if received enough then go to a sub program to evaluate the received answer.

     
  • k2teknik - 2015-04-14

    I try this code:

    WHILE sz < 6 DO
    sz:=SysComRead(com_num,ADR(rcvBUF),1024,0);
    END_WHILE

    But it just make my PLC reboot, just like any other loop I try to kill some time with.

    If I put in a breakpoint and start the running of the PLC after 5 seconds of halt at the breakpoint, all is fine and I get my data in the rcvBUF as expected.

     

Log in to post a comment.