0
0

More than 3 years have passed since last update.

プロジェクト型演習 #2 (raspberrypi emboard RS232C通信)

Last updated at Posted at 2021-06-02

理想の流れ

RS232C を使用し em board (RX63n) と Raspberry Pi を接続する。
ゲーム終了後、em board から曲番号とスコアを送信し、Raspberry Pi で受信する。
Raspberry Pi は受け取ったデータを一度ファイルに書き込む。
その後、Pythonのプログラムを呼び出し、DBへ格納する。

実装

em board側

送信側プログラム
RS232C_em_board.c
#include <machine.h>
#include "iodefine.h"
#include "initBASE.h"

void main(void);

void main(void)
{   
    volatile int cnt = 0;
    volatile unsigned char str;
    volatile char list[8] = {'1', '2', ',', '3', '4', ',', '5', '6'};
    volatile int i;
    volatile int delay = 0;

    /* CMTO初期化 */
    SYSTEM.PRCR.WORD = 0xA502;
    MSTP(CMT0) = 0;
    SYSTEM.PRCR.WORD = 0xA500;
    CMT0.CMCR.WORD = 0x00C2;
    CMT0.CMCNT = 1;
    CMT0.CMCOR = 374;
    IR(CMT0, CMI0) = 0;
    CMT.CMSTR0.BIT.STR0 = 1;

    /* クロック初期化 */
    initBASE();

    /* SCI2 */
    SYSTEM.PRCR.WORD = 0xA502;
    MSTP(SCI2) = 0;
    SYSTEM.PRCR.WORD = 0xA500;

    MPC.PWPR.BIT.B0WI = 0;
    MPC.PWPR.BIT.PFSWE = 1;
    MPC.P50PFS.BYTE = 0x0A;
    MPC.P52PFS.BYTE = 0x0A;
    MPC.PWPR.BIT.PFSWE = 0;
    MPC.PWPR.BIT.B0WI = 1;

    PORT5.PMR.BIT.B0 = 1;
    PORT5.PMR.BIT.B2 = 1;
    SCI2.SMR.BYTE = 0x00;
    SCI2.BRR = 155;

    SCI2.SCR.BYTE = 0x30;


    while(1){
        while (IR(CMT0, CMI0) == 0) {
            ;
        }
        IR(CMT0, CMI0) = 0;
        while (cnt < 10000000) {
            cnt ++;
        }
        cnt = 0;
        //if (SCI2.SSR.BIT.RDRF == 1) {
            //str = SCI2.RDR;
            //SCI2.SSR.BIT.RDRF = 0;
            //SCI2.TDR = str;
        //}
        for (i = 0; i < 8; i++) {
            while (delay < 100000) {
                delay ++;
            }
            delay = 0;
            SCI2.TDR = list[i];

        }
        break;
    }

}

GitHub : RS232C_raspberry_pi.c

Rspberry Pi側

受信側プログラム
RS232C_raspberry_pi.c
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <wiringSerial.h>

int main(void){
    int indat, count;
    int fd;
    char buf[17];
    FILE *fp;

    fd = serialOpen("/dev/ttyUSB0", 9600);
    if (fd < 0) {
        printf("serialOpen error!!\n");

        return -1;
    }

    printf("fd = %d\n", fd);
    serialFlush(fd);
    while (1) {
        printf("\n");
        serialPuts(fd, buf);
        delay(10000);
        while (!(count = serialDataAvail(fd)));
        /* 受信開始 */
        fp = fopen("result.txt", "w");
        if (fp == NULL){
                printf("error\n");
                exit(1);
        }
        while (count--) {
            /* 1文字づつ格納 */
            indat = serialGetchar(fd);
            printf("%c", indat);
            fprintf(fp, "%c", indat);
        }
        /* 受信終了 */
        fclose(fp);
    }
    serialClose(fd);

    return 0;
}

GitHub : RS232C_raspberry_pi.c

実行

em board (RX63n) は CS+ で RS232C_em_board.c を実行。
Rspberry Pi は RS232C_raspberry_pi.c をコンパイルして実行。
同一ディレクトリ内に以下のresult.txtが生成される。

result.txt
12,34,56

今後

  • em board (RX63n) 側で仮の結果を格納したchar型配列を作りる。
  • 一文字づつ取り出して送信し、うまく書き出せるか試す。
  • ファイル生成後、DBへ送信するPythonプログラムを呼び出す。
  • データの最後に特殊文字などを送信し、それで判定させる。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0