Komunikace UART ATmega8
Moderátor: Moderátoři
Komunikace UART ATmega8
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#define UART_BAUD_RATE 2400
#define F_CPU 1000000
#define UART_BAUD_CALC(UART_BAUD_RATE,F_CPU) ((F_CPU)/((UART_BAUD_RATE)*16l)-1)
#define RBUFFLEN 40 //Pufferlänge für seriellen Empfang
volatile unsigned char rbuff[RBUFFLEN]; // Ringpuffer
volatile uint8_t rbuffpos, // Position, die als nächstes gelesen werden muß im Ringpuffer
rbuffcnt, // Anzahl zu lesender Zeichen im Puffer
udr_data; // Daten aus dem UART (volatile, damit nicht wegoptimiert wird vom Präprozessor)
// Interruptroutine, die Zeichen aus dem UART sofort ausliest, wenn empfangen
SIGNAL (SIG_UART_RECV)
{
udr_data= UDR; //Byte auf jeden Fall abholen, sonst Endlosinterrupt
if(rbuffcnt < RBUFFLEN) // keinen Zeichen in einem vollen Ringpuffer überschreiben
rbuff[(rbuffpos+rbuffcnt++) % RBUFFLEN] = udr_data; // welche Position? Gelesene Zeichenpos + Anzahl Zeichen MODULO Pufferlänge
// (von 0 wieder anfangen, wenn Ende erreicht)
}
// Nächstes zu lesendes Zeichen aus Ringpuffer zurückgeben
unsigned char ser_getc (void)
{
unsigned char c;
while(!rbuffcnt); // Warte bis ein Zeichen vorhanden ist
cli(); // Interruptbehandlung kurz aussetzen. Ab jetzt muß es schnell gehen (wenig Befehle), damit Zeichen, die
// ab jetzt eintreffen nicht verloren gehen.
rbuffcnt--; // anschl. ein Zeichen weniger zum ausgeben
c = rbuff [rbuffpos++]; // Zeichen holen, was nach dem bereits gelesenen liegt
if (rbuffpos >= RBUFFLEN) rbuffpos = 0; // wenn hinterstes Zeichen (rechts im Puffer) gelesen wurde, dann wieder vorne anfangen
sei(); // Interruptbehandlung wieder aktivieren
return (c); // Zeichen zurückgeben
}
void uart_putc(unsigned char c)
{
while(!(UCSRA & (1 << UDRE))); // warte, bis UDR bereit
UDR = c; // sende Zeichen
}
void uart_puts (char *s)
{
while (*s)
{ // so lange *s != NULL
uart_putc(*s);
s++;
}
}
void uart_ini ()
{
sei(); // Interruptbehandlung aktivieren
UCSRB |= (1 << TXEN); // UART TX (senden) einschalten
UCSRB |= (1 << RXEN ); // UART RX (empfangen) einschalten
UCSRB |= (1 << RXCIE); // Interruptauslösung für eingehende Daten aktivieren
UCSRC |= (1<<URSEL)|(3<<UCSZ0); // Asynchron, 8N1
UBRRH=(uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_CPU)>>8); // tady je 8
UBRRL=(uint8_t)UART_BAUD_CALC(UART_BAUD_RATE,F_CPU);
}
void main()
{
unsigned char zeichen;
DDRC=0x02;
uart_ini();
//lcd_ini();
int g;
unsigned int h;
while (1)
{
zeichen = ser_getc();
h=atoi(zeichen);
uart_putc (zeichen);
PORTC = 0X02;
for(g=0;g<2000;g++);
PORTC=0x00;}
}
}
nevíte někdo co s tím? Díky
zeichen = ser_getc();
h=atoi(zeichen);
uart_putc (zeichen);
Použití atoi() je tu úplně nelogické.
Funce atoi() konvertuje string na číslo, např "1234" na 1234.
Proč nezačít s jednoduchým kódem, např. z datašítu:
Kód: Vybrat vše
// vrací znak přijatý z terminálu
#include <avr/io.h>
//------------------------------------------------------------
void USART_Init( unsigned int baud )
{
/* Set baud rate */
baud = (F_CPU / (16UL * baud)) - 1;
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
}
//------------------------------------------------------------
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR = data;
}
//------------------------------------------------------------
unsigned char USART_Receive( void )
{
/* Wait for data to be received */
while ( !(UCSRA & (1<<RXC)) )
;
/* Get and return received data from buffer */
return UDR;
}
//------------------------------------------------------------
//------------------------------------------------------------
int main()
{
unsigned char bajt;
USART_Init(9600); // baud = 9600
while(1)
{
bajt = USART_Receive();
USART_Transmit(bajt);
}
}
Když posíláš kód, tak ho dej mezi tagy (code) a (/code), závorky jsou hranaté. [/code]
Edit:
Ještě jsem si všimnul, že máš oscilátor 1 MHz, zřejmě interní.
S ním bude max. baud 4800.
V datašítu je tabulka použitelných baudových rychlostí pro různé kmitočty oscilátoru.
A pro zařízení, které má spolehlivě chodit, se používá krystalový oscilátor.
Kód: Vybrat vše
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
HANDLE hport;
DWORD status;
DWORD znaku;
char prijmuto[6];
char bd[200];
unsigned int *k;
char *l;
int p1,p2,p3;
char d[2];
HWND hwnd,hwnda,hwndb,hwndc,hwndd,hwnde,hwndf,hwndg;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char ReadABuffer()
{
OVERLAPPED *Overlapped = ( OVERLAPPED *)LocalAlloc(LMEM_ZEROINIT, sizeof(OVERLAPPED));
DWORD dwCommEvent;
DWORD dwRead;
char chRead;
ZeroMemory(&bd,200*sizeof(char));
if (!SetCommMask(hport, EV_RXCHAR))
{
printf("Error setting communications event mask!!! \n");
return 0;
}
for ( ;; )
{
if (WaitCommEvent(hport, &dwCommEvent, NULL)) {
do {
if (ReadFile(hport, &chRead, 1, &dwRead, Overlapped))
{
sprintf(bd,"%s%c",bd,chRead);
SetWindowText(hwnd,bd);//tady to píšu na lištu
return(chRead);
}
else
{
break;
}
} while (dwRead);
}
else
// Error in WaitCommEvent
return 0;
//break;
}
}
BOOL WriteABuffer(char * lpBuf, DWORD dwToWrite)
{
OVERLAPPED osWrite = {0};
DWORD dwWritten;
DWORD dwRes;
BOOL fRes;
osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osWrite.hEvent == NULL) return FALSE;
if (!WriteFile(hport, lpBuf, dwToWrite, &dwWritten, &osWrite))
{
if (GetLastError() != ERROR_IO_PENDING)
{
fRes = FALSE;
}
else
{
dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE);
switch(dwRes)
{
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hport, &osWrite, &dwWritten, FALSE))
{
fRes = FALSE;
}
else
{
fRes = TRUE;
}
break;
default:
fRes = FALSE;
break;
}
}
}
else
{
fRes = TRUE;
}
CloseHandle(osWrite.hEvent);
return (fRes);
}
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
/* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
ZeroMemory(&bd,200*sizeof(char));
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
hwnda=CreateWindowEx(WS_EX_CLIENTEDGE,"BUTTON","otevri",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,10,20,70,25,hwnd,0,hThisInstance,NULL);
hwndb=CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_CHILD | WS_VISIBLE ,10,50,70,25,hwnd,0,hThisInstance,NULL);
hwndc=CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_CHILD | WS_VISIBLE ,110,50,70,25,hwnd,0,hThisInstance,NULL);
hwndd=CreateWindowEx(WS_EX_CLIENTEDGE,"BUTTON","posli",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,110,20,70,25,hwnd,0,hThisInstance,NULL);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_COMMAND:
{
if(lParam==(LPARAM)hwnda)
{
int a=GetWindowTextLength(hwndb);
char b[a++];
GetWindowText(hwndb,b,a);
//tady se do okna napíše COM1
hport=CreateFile(b,GENERIC_READ | GENERIC_WRITE ,0,NULL ,OPEN_EXISTING ,0,NULL); //otevře port
if (hport==INVALID_HANDLE_VALUE)MessageBox(hwnd,"nepodarilo se otevrit port","chyba",MB_YESNO);
else
SetWindowText(hwnd,b);
}
if(lParam==(LPARAM)hwndd)//tady to posílá do comu data a získané se zobrazí na liště
{
int a=GetWindowTextLength(hwndc);
char b[a++];
char kl[2];
GetWindowText(hwndc,b,a);
unsigned int c=atoi(b);
char tre=(char)c;
l=&tre;//zkoušel jsem i posílat pointery, je to furt to samý nefunguje to
k=&c;
sprintf(kl,"%c",*l);SetWindowText(hwndf,kl);
ZeroMemory(&d,2*sizeof(char));
sprintf(d,"%c",c);
SetWindowText(hwndd,d);
WriteABuffer(d,2);
ReadABuffer();
}
}
break;
case WM_DESTROY:
{CloseHandle(hport);
PostQuitMessage (0); } /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}2) pokud k tomu není zvláštní důvod, proč máte na straně PC pro rozchození komunikace ovelapped funkce. To je na uchození daleko složitější a předpokládá to, že na úrovni HW to funguje bezchybně. To je součást nečeho většího? Pro rozchození komunikace po RS232 bych použil úplně nejjednodušší synchronní způsob - CreateFile, SetupComm, BuildCommDCB + SetCommState, SetCommTimeouts, a pak už rovnou WriteFile a ReadFile, případně ClearCommError.
To nakonfigurování RS232 ve vašem kódu nějak nevidím, je třeba si dát pozor na nastavení RtsControl a DtrControl.
Tohle je vyzkoušený úryvek kódu pro otevření portu a jeho nastavení. cfgdt jsou externí konfigurační data, musíte si doplnit proměnné, které jsou definovány jinde:
Kód: Vybrat vše
// ------------------------ OPEN COMM ----------------------
// Funkce pro otevreni serioveho kanalu na zarizeni.
void open_comm(void)
{
char t[50];
DCB comm_blok;
COMMTIMEOUTS comm_to;
HANDLE comm_device;
strcpy(t, "\\\\.\\");
strcat(t, cfgdt.port);
comm_device = CreateFile(t, GENERIC_READ|GENERIC_WRITE,
0, NULL, OPEN_EXISTING,
NULL, NULL);
if(comm_device == INVALID_HANDLE_VALUE) // otevri port
{
MessageBox(MainWindowHandle, STR(ID_S_OpenCommErr), "KTA",
MB_OK|MB_ICONEXCLAMATION);
return; // navrat pri chybe
}
SetupComm(comm_device, 7000L, 7000L); // nastav I/O fronty
strstream(t, sizeof(t), ios::out) << cfgdt.port <<
":9600,n,8," << cfgdt.stopbits << ends; // sestav retezec parametru
if(!BuildCommDCB(t, &comm_blok)) // sestav ridici blok spojeni
{
MessageBox(MainWindowHandle, STR(ID_S_OpenCommErr), "KTA",
MB_OK|MB_ICONEXCLAMATION);
CloseHandle(comm_device); // navrat pri chybe
comm_device = INVALID_HANDLE_VALUE;
return;
}
comm_blok.fBinary = 1; // binarni prenos
comm_blok.fOutxCtsFlow = 0; // nastav pouziti signalu
comm_blok.fOutxDsrFlow = 0;
comm_blok.fRtsControl = RTS_CONTROL_ENABLE;// rizeni RTS
comm_blok.fDtrControl = DTR_CONTROL_ENABLE;// rizeni DTR
comm_blok.fDsrSensitivity = false;
comm_blok.EvtChar = '\xff'; // nastav markant
if(!SetCommState(comm_device, &comm_blok))
{
MessageBox(MainWindowHandle, STR(ID_S_OpenCommErr), "KTA",
MB_OK|MB_ICONEXCLAMATION);
CloseHandle(comm_device);
comm_device = INVALID_HANDLE_VALUE;
return;
}
if(!SetCommTimeouts(comm_device, &comm_to))
{
MessageBox(MainWindowHandle, STR(ID_S_OpenCommErr), "KTA",
MB_OK|MB_ICONEXCLAMATION);
CloseHandle(comm_device);
comm_device = INVALID_HANDLE_VALUE;
return;
}
. . .
atd.
Výsledek je na obrázku. Hromada chyb a varování.
Takže nerozumím jak jsi s tím mohl něco zkoušet.
2. Po odstranění chyb a kompilaci jsem kód zkoušel na mé desce
s Atmega8 a krystalovým oscilátorem.
Funguje spolehlivě s terminálem v PC.
3. Doporučuji nejdřív zkoušet kód s terminálem a pak teprve s vlastním programem.
Pokud testuješ nevyzkoušený kód v mproc s nevyzkoušeným PC programem, tak je těžké najít kde je chyba.
4. Problém může být i v nepřesném interním oscilátoru.
To jde také zjistit pomocí terminálu a jednoduchého programu v mproc.
Oscilátor se dá doladit změnou registru OSCCAL.
- Přílohy
-
build.gif- (24.91 KiB) Staženo 232 x
Kód: Vybrat vše
#include <windows.h>
#include <cstdio>
using namespace std;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HWND hwnd,hwnda,hwndb,hwndc,hwndd,hwnde;
HANDLE hComm;
DCB m_dcb;
BOOL m_bPortReady;
DWORD iBytesWritten;
char br[10];
BYTE res;
unsigned int c;
COMMTIMEOUTS m_CommTimeouts;
char szClassName[ ] = "WindowsApp";
BOOL OpenPort(int number)
{
char portname[9];
ZeroMemory(&portname,9*sizeof(char));
sprintf(portname,"//./COM%d",number);
hComm = CreateFile(portname,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
if(hComm==INVALID_HANDLE_VALUE){
return false;}
else
return true;
}
BOOL ConfigurePort(DWORD BaudRate, BYTE ByteSize,
DWORD fParity, BYTE Parity, BYTE StopBits)
{
if((m_bPortReady = GetCommState(hComm, &m_dcb))==0)
{
MessageBox(hwnd,"GetCommState Error","Error",MB_OK+MB_ICONERROR);
CloseHandle(hComm);
return false;
}
m_dcb.BaudRate =BaudRate;
m_dcb.ByteSize = ByteSize;
m_dcb.Parity =Parity ;
m_dcb.StopBits =StopBits;
m_dcb.fBinary=TRUE;
m_dcb.fDsrSensitivity=false;
m_dcb.fParity=fParity;
m_dcb.fOutX=false;
m_dcb.fInX=false;
m_dcb.fNull=false;
m_dcb.fAbortOnError=TRUE;
m_dcb.fOutxCtsFlow=FALSE;
m_dcb.fOutxDsrFlow=false;
m_dcb.fDtrControl=DTR_CONTROL_DISABLE;
m_dcb.fDsrSensitivity=false;
m_dcb.fRtsControl=RTS_CONTROL_DISABLE;
m_dcb.fOutxCtsFlow=false;
m_dcb.fOutxCtsFlow=false;
m_bPortReady = SetCommState(hComm, &m_dcb);
if(m_bPortReady ==0)
{
MessageBox(hwnd,"SetCommState Error","Error",MB_OK+MB_ICONERROR);
CloseHandle(hComm);
return false;
}
return true;
}
BOOL WriteByte(BYTE bybyte)
{
iBytesWritten=0;
if(WriteFile(hComm,&bybyte,1,&iBytesWritten,NULL)==0)
return false;
else
return true;
}
BOOL ReadByte(BYTE &resp)
{
BYTE rx;
resp=0;
DWORD dwBytesTransferred=0;
if (ReadFile (hComm, &rx, 1, &dwBytesTransferred, 0))
{
if (dwBytesTransferred == 1)
{
resp=rx;
return true;
}
}
return false;
}
void ClosePort()
{
CloseHandle(hComm);
return;
}
BOOL SetCommunicationTimeouts(DWORD ReadIntervalTimeout,
DWORD ReadTotalTimeoutMultiplier,
DWORD ReadTotalTimeoutConstant,
DWORD WriteTotalTimeoutMultiplier,
DWORD WriteTotalTimeoutConstant)
{
if((m_bPortReady = GetCommTimeouts (hComm, &m_CommTimeouts))==0)
return false;
m_CommTimeouts.ReadIntervalTimeout =ReadIntervalTimeout;
m_CommTimeouts.ReadTotalTimeoutConstant =ReadTotalTimeoutConstant;
m_CommTimeouts.ReadTotalTimeoutMultiplier =ReadTotalTimeoutMultiplier;
m_CommTimeouts.WriteTotalTimeoutConstant = WriteTotalTimeoutConstant;
m_CommTimeouts.WriteTotalTimeoutMultiplier =WriteTotalTimeoutMultiplier;
m_bPortReady = SetCommTimeouts (hComm, &m_CommTimeouts);
if(m_bPortReady ==0)
{
MessageBox(hwnd,"StCommTimeouts function failed",
"Com Port Error",MB_OK+MB_ICONERROR);
CloseHandle(hComm);
return false;
}
return true;
}
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
/* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
hwnda=CreateWindowEx(WS_EX_CLIENTEDGE,"BUTTON","otevri",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,10,20,100,25,hwnd,0,hThisInstance,NULL);
hwndb=CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","4",WS_CHILD | WS_VISIBLE | ES_NUMBER ,10,50,70,25,hwnd,0,hThisInstance,NULL);
hwndc=CreateWindowEx(WS_EX_CLIENTEDGE,"BUTTON","posli",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,150,20,100,25,hwnd,0,hThisInstance,NULL);
hwndd=CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_CHILD | WS_VISIBLE | ES_NUMBER ,150,50,70,25,hwnd,0,hThisInstance,NULL);
hwnde=CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_CHILD | WS_VISIBLE | ES_NUMBER ,150,80,70,25,hwnd,0,hThisInstance,NULL);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_COMMAND:
{
if(lParam==(LPARAM)hwndc)
{
int a=GetWindowTextLength(hwndd);
char b[a++];
ZeroMemory(&br,10*sizeof(char));
GetWindowText(hwndd,b,a);
BYTE c=atoi(b);
if((WriteByte(c))==true)
{
if((ReadByte(res))==true)
{
sprintf(br,"%c %d",res,res);
SetWindowText(hwnde,br);
}
else
MessageBox(hwnd,"něco se pokazilo","time out",MB_YESNO);
}
else
{ MessageBox(hwnd,"něco se pokazilo","chyba4",MB_YESNO);
}
}
if(lParam==(LPARAM)hwnda)
{
int a=GetWindowTextLength(hwndb);
char b[a++];
GetWindowText(hwndb,b,a);
c=atoi(b);
if((OpenPort(c))==true)
{
if((ConfigurePort(CBR_2400, 8, true, EVENPARITY , ONESTOPBIT ))==true)
{
if((SetCommunicationTimeouts(0,1500,0,0,0))==true)
SetWindowText(hwnda,"Com otevren");
else
MessageBox(hwnd,"něco se pokazilo","chyba3",MB_YESNO);
}
else
MessageBox(hwnd,"něco se pokazilo","chyba2",MB_YESNO);
}
else
MessageBox(hwnd,"něco se pokazilo","chyba1",MB_YESNO);
}
}
break;
case WM_DESTROY:
{ClosePort(); PostQuitMessage (0);} /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
chtěl jsem s tím řídit krokáč, no ale četl jsem nakonec že je lepší koupit hotový driver, tak to asi udělám, díky za snahu