MONG SỰ GIÚP ĐỠ GẤP CỦA MỌI NGƯỜI VỀ RS232 ( MÌNH ĐANG RẤT CẦN )
Mình đang làm một chương trình kết nối cổng RS232 sử dụng visual c++. không hiểu vì sao khi test chương trình thì thấy không truyền xuống PIC6f877a được.
chương trình của mình :
#include "stdafx.h"
#include <windows.h>
// kiem tra che do mo cong.
TCHAR *pcCommPort = TEXT("COM3");
HANDLE hcom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
// cài đặt trạng thái ban đầu .
void Setting()
{
if (hcom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return ;
}
else
{
// set timeouts
COMMTIMEOUTS cto = { 1, 100, 1000, 0, 0 };
DCB dcb;
if(!SetCommTimeouts(hcom,&cto))
printf ("E013_SetCommTimeouts failed\n");
// Thiet lap cac che do cong com
memset(&dcb,0,sizeof(dcb));
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = 9600;
dcb.fBinary = 1;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
if(!SetCommState(hcom,&dcb))
printf ("E013_SetCommTimeouts failed 2\n");
CloseHandle(hcom);
}
}
// Mo cong com
void RS232(CString data)
{
// Chuyen va nhan du lieu
char p;
char data_receive;
DWORD read = 0;
DWORD write=1; // Number of bytes to write to serial port
p = WriteFile(hcom,&data,write,&write,NULL); // write is updated with the number of bytes written
ReadFile(hcom,&data_receive,sizeof(data_receive),& read,NULL); // read is updated with the number of bytes read
DWORD i;
for (i=0; i<read; i++)
printf("%i ", (unsigned char)data_receive);
}
// BAT DAU HAM MAIN GUI MOT KY TU XUONG PIC16F877A.
void main(){
setting();
RS232('R');
}
CẢM ƠN MỌI NGƯỜI RẤT RẤT NHIỀU !!
thay đổi nội dung bởi: xuanducdhcn, 08-11-2010 lúc 04:04 PM.
|