Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Slanje na serijski port

0 views
Skip to first unread message

Aleks

unread,
Jun 10, 2006, 8:34:16 AM6/10/06
to
Pozdrav, radim jedan projekt di bi iz txt datoteke citao char po char i slao
ga na serijski port. Dio sa citanjem znam napravit, problem mi je kako onda
poslat karakter preko serijskog porta (na mikrokontroller, RS232 protokol).
Mozete mi napisat neku funkciju?

Hvala, Aleks


mimix

unread,
Jun 10, 2006, 8:57:07 AM6/10/06
to


Kako citas uopce?

Ja sam korstio ovo za WinXP:
http://www.logix4u.net/inpout32.htm


--
Milan Mimica
http://sparklet.sf.net

Aleks

unread,
Jun 10, 2006, 9:38:18 AM6/10/06
to
"mimix" <milan....@globalnet.hr> wrote in message
news:e6efj3$2c0$1...@news2.carnet.hr...

> On 2006-06-10, Aleks wrote:
> > Pozdrav, radim jedan projekt di bi iz txt datoteke citao char po char i
slao
> > ga na serijski port. Dio sa citanjem znam napravit, problem mi je kako
onda
> > poslat karakter preko serijskog porta (na mikrokontroller, RS232
protokol).
> > Mozete mi napisat neku funkciju?
>
>
> Kako citas uopce?
>
char c;
FILE *f;
f = fopen ( "fajl.txt", "r");
c = fgetc( f );

Tako nekako, sasvim trivijalno. Sad ovaj char c treba na serijski bacit.
Hvala na odgovoru, al ovo mi ne pomaze jer je radjeno za paralelni port, a s
njim ne mogu bas komunicirat sa mikrokontrolerom.

mimix

unread,
Jun 10, 2006, 9:55:41 AM6/10/06
to
On 2006-06-10, Aleks wrote:
> char c;
> FILE *f;
> f = fopen ( "fajl.txt", "r");
> c = fgetc( f );

OK, mislio sam da znas citat s porta a ne znas pisat sto bi bilo cudno.

> Tako nekako, sasvim trivijalno. Sad ovaj char c treba na serijski bacit.
> Hvala na odgovoru, al ovo mi ne pomaze jer je radjeno za paralelni port, a s
> njim ne mogu bas komunicirat sa mikrokontrolerom.

Ah, da, jednom sam koristio paralelni port za serijsku komunikaciju
pa me to zbunilo. Nista korisnog od mene :-/

Goran Sliskovic

unread,
Jun 10, 2006, 4:40:41 PM6/10/06
to

Ako je rijec o win platformo, otvoris com port sa CreateFile("COMx",
...) i dalje imas ReadFile/WriteFile standardno. Onaj x u COMx je
naravno com port koji zelis otvorit (COM1, COM2...). Jedino moras
namjestit parity, stop bits, flow control (SetCommState) i timout-e
(SetCommTimeouts)... Pogledaj u MSDN.

Goran

Aleks

unread,
Jun 10, 2006, 5:26:16 PM6/10/06
to
"Goran Sliskovic" <gsli...@yahoo.com> wrote in message
news:e6fao6$9jj$1...@ss408.t-com.hr...

Zahvaljujem, evo iskopao sam ovo za pisanje ako nekom jos bude trebalo:

#include <windows.h>

#include <stdio.h>

int main(int argc, char *argv[])
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
char *pcCommPort = "COM2";

hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);

if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}

// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.

fSuccess = GetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}


dcb.BaudRate = 9600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit

fSuccess = SetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}

printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
// return (0);


char StringA50[4] = {'A','5','0'};
char StringB85[4] = {'B','8','5'};

unsigned long zapisano;
int status = WriteFile(hCom, // Handle
&StringA50, // Address of Outgoing data
3, // Number of bytes to write
&zapisano, // Returned number of bytes written
NULL);

if ( zapisano == 3)
printf("Uspjesno poslano => %s\n",StringA50);
else
printf("Neuspjelo slanje!\n");

return (0);

}


0 new messages