Re: C언어 기준으로 ss=15 명령어를 보내고 문자열 파싱 코드 > Module_Forum

본문 바로가기
사이트 내 전체검색

Module_Forum

Re: C언어 기준으로 ss=15 명령어를 보내고 문자열 파싱 코드

페이지 정보

profile_image
작성자 최고관리자
댓글 2건 조회 354회 작성일 23-09-27 09:50

본문

#define _CRT_SECURE_NO_WARNINGS

#include <windows.h>
#include <stdio.h>

int main()
{
    HANDLE hSerial;
    DCB dcbSerialParams = { 0 };
    COMMTIMEOUTS timeouts = { 0 };
    char ss_data[] = "ss=15\r\n";
    DWORD bytes_written, bytes_read;
    char incoming_data[1024];
    int size = 12;
    double array[12];

    // Open the serial port
    hSerial = CreateFile(L"\\\\.\\COM17",
        GENERIC_READ | GENERIC_WRITE,      // Read/Write Access
        0,                                // No Sharing, ports cant be shared
        NULL,                              // No Security
        OPEN_EXISTING,                    // Open existing port only
        0,                                // Non Overlapped I/O
        NULL);

    if (hSerial == INVALID_HANDLE_VALUE)
    {
        fprintf(stderr, "Error opening serial port.\n");
        return -1;
    }


    dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
    if (!GetCommState(hSerial, &dcbSerialParams))
    {
        fprintf(stderr, "Error getting serial port state.\n");
        CloseHandle(hSerial);
        return -1;
    }
    dcbSerialParams.BaudRate = CBR_115200;
    dcbSerialParams.ByteSize = 8;
    dcbSerialParams.StopBits = ONESTOPBIT;
    dcbSerialParams.Parity = NOPARITY;

    if (!SetCommState(hSerial, &dcbSerialParams))
    {
        fprintf(stderr, "Error setting serial port state.\n");
        CloseHandle(hSerial);
        return -1;
    }

    //Setting Timeouts
    timeouts.ReadIntervalTimeout = 50;
    timeouts.ReadTotalTimeoutConstant = 50;
    timeouts.ReadTotalTimeoutMultiplier = 10;
    timeouts.WriteTotalTimeoutConstant = 50;
    timeouts.WriteTotalTimeoutMultiplier = 10;
   
    if (SetCommTimeouts(hSerial, &timeouts) == FALSE)
    {
        printf_s("\nError to Setting Time outs");
        return -1;
    }

#if 0  //처음 보낼 때만 하고 그 다음에는 1 에서 0으로 변경하세요
   
    // [Send]  ss=15
    if (!WriteFile(hSerial, ss_data, sizeof(ss_data), &bytes_written, NULL))
    {
        fprintf(stderr, "Error writing to serial port.\n");
        CloseHandle(hSerial);
        return 1;
    }

    printf("Data Send: %s", ss_data);

#endif

    if (!ReadFile(hSerial, incoming_data, sizeof(incoming_data), &bytes_read, NULL))
    {
        fprintf(stderr, "Error reading from serial port.\n");
        CloseHandle(hSerial);
        return -1;
    }

    incoming_data[bytes_read] = '\0';

    printf("recv: %s", incoming_data);

    //C언어로 기준으로 " " 파싱

    char* token = strtok(incoming_data, " ");

    int count = 0;
    while (token != NULL && count < size) {
        array[count] = atof(token);
        token = strtok(NULL, " ");
        count++;
    }

    //배열 출력

 
    for (int i = 0; i < count; i++) {
        printf("Value[%d]: %.3lf\n", i, array[i]);
    }

    CloseHandle(hSerial);

    return 0;
}

댓글목록

profile_image

조용진님의 댓글

조용진 작성일

감사합니다. 참고해서 사용해보도록 하겠습니다. 그런데 하나 여쭤보자면 우분투 기반에서 센서를 구동하는데 windows.h 헤더 파일이 사용되는 이유가 따로 있는건가요?

profile_image

최고관리자님의 댓글의 댓글

최고관리자 작성일

아~ 제가 급하게 하다보니 윈도우에서 개발하였습니다. 리눅스는 그거에 맞게 프로그래밍 하시면 됩니다 ㅎㅎㅎ

회원로그인

회원가입