2014-08-31 7 views
-4

이 코드를 복사하여 직렬 포트를 통해 C++로 작성했습니다. 그것은 제대로 작동하지만 내가 필요한 것은 arduino와 통신하기 위해 여러 문자열을 쓰고 싶기 때문에 문자열을 바이트 배열로 변환하는 방법을 아는 것입니다. 나는 여기에 코드를 붙여 넣습니다 : 나는 내 자신의 문자열을 사용하려는C++의 직렬 포트를 통해 데이터를 전송합니다.

// Define the five bytes to send ("hello") 
char bytes_to_send[5]; 
bytes_to_send[0] = 104; 
bytes_to_send[1] = 101; 
bytes_to_send[2] = 108; 
bytes_to_send[3] = 108; 
bytes_to_send[4] = 111; 

을 내가 보낼 수 있도록 바이트로 변환하는 방법 : 여기

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

int main() 
{ 
// Define the five bytes to send ("hello") 
char bytes_to_send[5]; 
bytes_to_send[0] = 104; 
bytes_to_send[1] = 101; 
bytes_to_send[2] = 108; 
bytes_to_send[3] = 108; 
bytes_to_send[4] = 111; 

// Declare variables and structures 
HANDLE hSerial; 
DCB dcbSerialParams = {0}; 
COMMTIMEOUTS timeouts = {0}; 

// Open the highest available serial port number 
fprintf(stderr, "Opening serial port..."); 
hSerial = CreateFile(
      "\\\\.\\COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, 
      OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 
if (hSerial == INVALID_HANDLE_VALUE) 
{ 
     fprintf(stderr, "Error\n"); 
     return 1; 
} 
else fprintf(stderr, "OK\n"); 

// Set device parameters (38400 baud, 1 start bit, 
// 1 stop bit, no parity) 
dcbSerialParams.DCBlength = sizeof(dcbSerialParams); 
if (GetCommState(hSerial, &dcbSerialParams) == 0) 
{ 
    fprintf(stderr, "Error getting device state\n"); 
    CloseHandle(hSerial); 
    return 1; 
} 

dcbSerialParams.BaudRate = CBR_38400; 
dcbSerialParams.ByteSize = 8; 
dcbSerialParams.StopBits = ONESTOPBIT; 
dcbSerialParams.Parity = NOPARITY; 
if(SetCommState(hSerial, &dcbSerialParams) == 0) 
{ 
    fprintf(stderr, "Error setting device parameters\n"); 
    CloseHandle(hSerial); 
    return 1; 
} 

// Set COM port timeout settings 
timeouts.ReadIntervalTimeout = 50; 
timeouts.ReadTotalTimeoutConstant = 50; 
timeouts.ReadTotalTimeoutMultiplier = 10; 
timeouts.WriteTotalTimeoutConstant = 50; 
timeouts.WriteTotalTimeoutMultiplier = 10; 
if(SetCommTimeouts(hSerial, &timeouts) == 0) 
{ 
    fprintf(stderr, "Error setting timeouts\n"); 
    CloseHandle(hSerial); 
    return 1; 
} 

// Send specified text (remaining command line arguments) 
DWORD bytes_written, total_bytes_written = 0; 
fprintf(stderr, "Sending bytes..."); 
if(!WriteFile(hSerial, bytes_to_send, 5, &bytes_written, NULL)) 
{ 
    fprintf(stderr, "Error\n"); 
    CloseHandle(hSerial); 
    return 1; 
} 
fprintf(stderr, "%d bytes written\n", bytes_written); 

// Close serial port 
fprintf(stderr, "Closing serial port..."); 
if (CloseHandle(hSerial) == 0) 
{ 
    fprintf(stderr, "Error\n"); 
    return 1; 
} 
fprintf(stderr, "OK\n"); 

// exit normally 
return 0; 
} 

내가 변경하고자하는 부분이다 시리얼 포트를 통해.

+0

C++ 표준 라이브러리에 포함 된'std :: string' 클래스가 있습니다 (이 라이브러리는 유용한 컨테이너와 알고리즘으로 가득합니다). 'std :: string' 클래스에는 개별 문자 (바이트)를 추출하거나 내용을 바이트 배열로 추출하는 함수가 있습니다. 희망이 도움이됩니다. – patchwork

답변

0

문자열은 이미 바이트 배열입니다.

-1

내가 arduino로 테스트 할 수있을 때, 방금 문자열을 보냈을 때 제대로 작동했습니다.

답변 해 주셔서 감사합니다.

관련 문제