C de com porttan pic12f683 den veri okuma ccs c compiler. windows.h kütüphanesini kullanarak pic serail bağlantısını kullanark veri gönderme. com porttan okunan değer hexedecimal hexedecimali ascii kod karşılığına çevirme ile istenen değeri elde etmiş oluyoruz. Biz burada ds18b20 sicaklık sensöründen alınan verileri okuduk
https://www.sifirzero.com/2023/04/ds18b20-scaklk-sensoru-php-serial-port.html
C kodu:
#include <windows.h>
#include <stdio.h>
int main()
{
HANDLE hSerial;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};
DWORD dwBytesRead;
hSerial = CreateFile("COM5", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hSerial == INVALID_HANDLE_VALUE) {
printf("Error %d from CreateFile", GetLastError());
return 1;
}
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams)) {
printf("Error %d from GetCommState", GetLastError());
CloseHandle(hSerial);
return 1;
}
dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if (!SetCommState(hSerial, &dcbSerialParams)) {
printf("Error %d from SetCommState", GetLastError());
CloseHandle(hSerial);
return 1;
}
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts(hSerial, &timeouts)) {
printf("Error %d from SetCommTimeouts", GetLastError());
CloseHandle(hSerial);
return 1;
}
char buffer[256];
memset(buffer, 0, sizeof(buffer));
while(1) {
if (ReadFile(hSerial, buffer, sizeof(buffer), &dwBytesRead, NULL)) {
if (dwBytesRead > 0) {
//printf("Received %d bytes: ", dwBytesRead);
int i;
for ( i = 0; i < dwBytesRead; i++) {
//printf("%02x \n\n", buffer[i]);
printf("%c",buffer[i]);
}
printf("\n");
}
}
}
CloseHandle(hSerial);
return 0;
}
Hiç yorum yok:
Yorum Gönder
Her yorum bilgidir. Araştırmaya devam...