Help us understand the problem. What is going on with this article?

PLCとRaspberryPi間をC言語でソケット通信 MCプロトコル編2

More than 1 year has passed since last update.

前回の課題

PLCとRaspberryPi間をC言語でソケット通信 MCプロトコル編で紹介したプログラムを改良します.

sprintfが便利

request関数部分を以下に置き換えてください.

word_read
int word_read(char *buffer,unsigned char reg,int start_num,int num) {

    char sub_h[]="5000";
    char net_num[]="00";
    char pc_num[]="FF";
    char unit_io[]="03FF";
    char unit_ch[]="00";
    char nlen[]="0018";
    char cputimer[]="0020";
    char command[]="0401";
    char subcommand[]="0000"; 
    sprintf(buffer,"%s%s%s%s%s%s%s%s%s%c*%06d%04d",sub_h,net_num,pc_num,unit_io,unit_ch,nlen,cputimer,command,subcommand,reg,start_num,num);
    return strlen(buffer);
}

例えばD1から5つ分の値を受け取りたいときはこのように指定します.

word_read(send_buffer,'D',1,5);//(伝文配列,読み取りたいレジスタ,開始場所,個数)

実行結果

関数か.PNG

日付の年以下の値も返ってきました.

このプログラムは1回読み取れば終了ですが、無限ループとして常時監視したり(できれば割り込みの方がよい)GPIOを使ったりとまだまだ改善の余地はあると思います。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした