Вот код для pic:
- Код: Выделить всё • Развернуть
#include <pic.h>
void transmit(unsigned char byte) {
while(!TXIF) continue;
TXREG= byte;
}
void main(void) {
SPBRG= 2; //4MHz, 19200
BRGH= 0; //4MHz, 19200
SYNC= 0;
SPEN= 1;
TXIE= 0;
TX9= 0;
TXEN= 1;
GIE= 0;
PEIE= 0;
TRISB= 0;
PORTB= 0;
RCREG= 0;
while (1) {
transmit(50);
};
}
Это для PC:
- Код: Выделить всё • Развернуть
//______________________________________________________tcom_port___________________________________________________________
//--------------------------------------------------------------------------------------------------------------------------
class tcom_port {
HANDLE hcom_;
COMMTIMEOUTS commTimeOuts_;
DCB portDcb_;
static const char* pdefault_name;
char* pname;
public:
tcom_port();
bool open();
void close();
bool set_baud_rate(const int nrate);
bool write(const char* pbuffer, int length);
bool read (const char* pbuffer, int length, int* pnread);
~tcom_port();
};
const char* tcom_port::pdefault_name= "COM1";
//_____________________________________
tcom_port::tcom_port(): hcom_(NULL), pname((char*)pdefault_name) {
commTimeOuts_.ReadIntervalTimeout= 1;
commTimeOuts_.ReadTotalTimeoutMultiplier= 1;
commTimeOuts_.ReadTotalTimeoutConstant= 1;
commTimeOuts_.WriteTotalTimeoutMultiplier= 1;
commTimeOuts_.WriteTotalTimeoutConstant= 1;
memset(&portDcb_, 0, sizeof(portDcb_)); portDcb_.DCBlength=sizeof(portDcb_);
};
//_____________________________________
bool tcom_port::open() {
hcom_ = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
if ( hcom_==INVALID_HANDLE_VALUE ) return false;
if ( !SetCommTimeouts(hcom_, &commTimeOuts_) ) return false;
if ( !GetCommState(hcom_, &portDcb_) ) return false;
return true;
};
//_____________________________________
void tcom_port::close() {
if (hcom_) { CloseHandle(hcom_); hcom_= NULL; };
};
//_____________________________________
bool tcom_port::set_baud_rate(const int nrate) {
if ( !GetCommState(hcom_, &portDcb_) ) return false;
portDcb_.BaudRate= nrate;
if ( !SetCommState(hcom_, &portDcb_) ) return false;
return true;
};
//_____________________________________
bool tcom_port::write(const char* pbuffer, int length) {
DWORD dwWritten= 0;
if ( !WriteFile(hcom_, (void*)pbuffer, length, &dwWritten, NULL) ) return false;
return dwWritten==length;
};
//_____________________________________
bool tcom_port::read (const char* pbuffer, int length, int* pnread) {
if ( !ReadFile(hcom_, (void*)pbuffer, length, (DWORD*)pnread, NULL) ) return false;
return true;
};
//_____________________________________
tcom_port::~tcom_port() {
if (hcom_) CloseHandle(hcom_);
};
//__________________________________________________________________________________________________________________________
//--------------------------------------------------------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, INT ) {
char value1= 8;
char read_value= 100;
int nread= 0;
tcom_port com_port;
com_port.open();
com_port.set_baud_rate(19200);
while (true ) {
while ( nread==0 ) com_port.read(&read_value, 1, &nread);;
};
return 0;
};
//__________________________________________________________________________________________________________________________
//--------------------------------------------------------------------------------------------------------------------------
С чем это может быть связано?