1. Do not share user accounts! Any account that is shared by another person will be blocked and closed. This means: we will close not only the account that is shared, but also the main account of the user who uses another person's account. We have the ability to detect account sharing, so please do not try to cheat the system. This action will take place on 04/18/2023. Read all forum rules.
    Dismiss Notice
  2. For downloading SimTools plugins you need a Download Package. Get it with virtual coins that you receive for forum activity or Buy Download Package - We have a zero Spam tolerance so read our forum rules first.

    Buy Now a Download Plan!
  3. Do not try to cheat our system and do not post an unnecessary amount of useless posts only to earn credits here. We have a zero spam tolerance policy and this will cause a ban of your user account. Otherwise we wish you a pleasant stay here! Read the forum rules
  4. We have a few rules which you need to read and accept before posting anything here! Following these rules will keep the forum clean and your stay pleasant. Do not follow these rules can lead to permanent exclusion from this website: Read the forum rules.
    Are you a company? Read our company rules

Question Raspberry Pi Pico communication with simtools

Discussion in 'SimTools compatible interfaces' started by Spart, Dec 29, 2021.

  1. Spart

    Spart Member

    Joined:
    Mar 3, 2020
    Messages:
    33
    Occupation:
    Student
    Location:
    Kauno kaimas
    Balance:
    223Coins
    Ratings:
    +26 / 2 / -0
    My Motion Simulator:
    DC motor, Arduino
    Hello,
    First of all, I am new to programming. I am trying to write code (in C++) that will allow Pi Pico to receive data over usb/uart (I found usb serial connection not working quite right so I am using usb to ttl adapter for communication) from SimTools. The problem I have is when I test my code manually by sending values over terminal (putty) it works just fine, it reads numbers that I wrote and converts them into integers and echoes it back, but once I try receiving data from SimTools, I see no data being sent and no data echoes back (I am using to usb to ttl converters to see what's happening). I configured simtools to send decimal data so its easier (atleast for me) to use it.
    For reading the data I am using non blocking version of getchar_timeout_us(0) but I am wondering if thats the problem and I should use something else. If I send data simtools data from one usb to ttl adapter to another it works just fine.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "pico/stdlib.h"
    
    #define ENDSTDIN    255
    #define CR        13
    
    int main() {
        stdio_init_all();
    
        bool onoff = 0;
        const uint LED_PIN = 25;
        char strg[24]; // how much characters are expected minus one
        char pirmas[5];
        char antras[5];
        char trecias[5];
        char ketvirtas[5];
        char penktas[5];
        char chr;
        char narys;
        int lp = 0;
        int x = 0;
        int y = 0;
        int z = 0;
        int xx=0;
        int yy=0;
    
        memset(strg, 0, sizeof(strg));
    
        gpio_init(LED_PIN);
        gpio_set_dir(LED_PIN, GPIO_OUT);
    
        while (true) {
            chr = getchar_timeout_us(0);
            while(chr != ENDSTDIN)
            {   
                strg[lp++] = chr;          //start collecting input char one by one
                if(chr == CR || lp == (sizeof(strg) - 1)) //checks if all data is collected
                {
                    memcpy(pirmas, strg, 4 );
                    memcpy(antras, &strg[4], 4 );
                    memcpy(trecias, &strg[8], 4 );
                    memcpy(ketvirtas, &strg[12], 4 );
                    memcpy(penktas, &strg[16], 4 );
                    narys = strg[20];
                    printf("You wrote - %c\n", narys);
                    strg[lp] = 0;    //terminate string
                    x =  atoi (pirmas);
                    y =  atoi (antras);
                    z =  atoi (trecias);
                    xx =  atoi (ketvirtas);
                    yy =  atoi (penktas);
                    printf("You wrote - %d\n", x);
                    printf("You wrote - %d\n", y);
                    printf("You wrote - %d\n", z);
                    printf("You wrote - %d\n", xx);
                    printf("You wrote - %d\n", yy);
                    //memset(strg, 0, sizeof(strg));
                    lp = 0;        //reset string buffer pointer
                    break;
                }
                chr = getchar_timeout_us(0);
                }
    
            gpio_put(LED_PIN, onoff);
            onoff = !onoff;
            sleep_ms(1);
        }
    
        return 0;
    In my case I am trying to read data for five axis.
  2. yobuddy

    yobuddy Well-Known Member Staff Member Moderator SimAxe Beta Tester SimTools Developer Gold Contributor

    Joined:
    Feb 9, 2007
    Messages:
    5,166
    Occupation:
    Computer Technician
    Location:
    Portland, Oregon - USA
    Balance:
    48,212Coins
    Ratings:
    +5,038 / 16 / -0
    Can you show me what you sending for your test thru putty?
    Also, will you show me how you have SimTools configured for output.
    From this I may be able to see what the problem may be.
    Take care,
    yobuddy
  3. Spart

    Spart Member

    Joined:
    Mar 3, 2020
    Messages:
    33
    Occupation:
    Student
    Location:
    Kauno kaimas
    Balance:
    223Coins
    Ratings:
    +26 / 2 / -0
    My Motion Simulator:
    DC motor, Arduino
    Hey, thanks for reply, I already sorted the problem out, now it works as intended.
    • Like Like x 1