PDA

View Full Version : [C] ci sono quasi :(


RaouL_BennetH
28-11-2004, 21:59
Salve ragazzi. Allora, passo a descrivervi il mio problemino:

Sono riuscito a montare un lettore di badge che funziona via seriale. Ho acquistato delle tessere già programmate e, finito di saldare anche l'ultima resistenza ho fatto la prova e..... il lettore funziona.

Il collegamento avviene tramite Hyper Terminal, che a video, ogni volta che passo un badge, mi fa vedere il codice della tesserina. Ora, questo lettore è in grado anche di ricevere comandi dal PC in questo formato:

*/L mi accende e spegne un led

*/C mi cancella la memoria della eeprom

*/T mi scarica i codici delle tessere che sono passate per il lettore.

Bene, sembra tutto ok ma..... non ho mica capito come accidenti inserire quei comandi dalla sessione di Hyper Terminal. Ma, fosse solo questo il problema..... In effetti a me servirebbe costruire un'applicazione che mi memorizzi anche l'istante in cui il codice viene letto. In sostanza, quello che visualizzo su Hyper Terminal, vorrei farlo memorizzare su un file:

Es.:

passo la tessera con il codice 0001, su un file mi dovrebbe memorizzare:

0001 08,35 giorno/mese/anno.

Per la funzione del tempo, avevo già trovato qualcosa che me la dava:



#include <time.h>

struct tm *newtime;
time_t aclock;

time (&aclock);
newtime = localtime(&aclock);



Ora, le mie ricerche sono state vane per quanto riguarda la lettura e l'invio di comandi verso/da una porta seriale, quindi, mi sapreste dare qualche info?

Thx.


RaouL.

RaouL_BennetH
28-11-2004, 22:16
leggendo qua e la..... mi chiedevo se fosse sufficiente per inviare quei comandi una cosa del genere:



#include <stdio.h>

int main()
{
fprintf(stdaux, "*/T");
}



Ho un pò di timore a provare perchè, da ignorante quale sono, non vorrei involontariamente danneggiare l'apparecchietto. :(

RaouL_BennetH
28-11-2004, 22:26
Originariamente inviato da RaouL_BennetH
leggendo qua e la..... mi chiedevo se fosse sufficiente per inviare quei comandi una cosa del genere:



#include <stdio.h>

int main()
{
fprintf(stdaux, "*/T");
}



Ho un pò di timore a provare perchè, da ignorante quale sono, non vorrei involontariamente danneggiare l'apparecchietto. :(


ma porc!!! :muro:

ho anche letto che 'stdaux' funziona solo per Dos :cry:

ilsensine
29-11-2004, 08:18
Per dialogare con la seriale, sotto windows credo che devi usare CreateFile.

cionci
29-11-2004, 14:33
Sì...cerca CreateFile COM1 con google...

Comunque questo è un programma di esempio che c'è su MSDN:

/* A sample program to illustrate setting up a serial port. */

#include <windows.h>

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

hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices 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);
}

// We will 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);
}

// Fill in the DCB: baud=57,600 bps, 8 data bits, no parity, and 1 stop bit.

dcb.BaudRate = CBR_57600; // 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);
}

RaouL_BennetH
29-11-2004, 20:43
Originariamente inviato da ilsensine
Per dialogare con la seriale, sotto windows credo che devi usare CreateFile.


Grazie :)

Ma non sono legato a windows, potrei provare anche sotto linux, tanto, in termini di incapacità(mia), è uguale :(

RaouL_BennetH
29-11-2004, 21:56
Originariamente inviato da cionci
Sì...cerca CreateFile COM1 con google...

Comunque questo è un programma di esempio che c'è su MSDN:

/* A sample program to illustrate setting up a serial port. */

#include <windows.h>

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

hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices 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);
}

// We will 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);
}

// Fill in the DCB: baud=57,600 bps, 8 data bits, no parity, and 1 stop bit.

dcb.BaudRate = CBR_57600; // 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);
}




grazie mille cionci :eek: prima non l'avevo letta la tua risposta!!

cionci
29-11-2004, 22:00
Questa ì l'inizializzazione della porta... Per la scrittura e la lettura devi usare ReadFile e Writefile...

ilsensine
30-11-2004, 07:32
Originariamente inviato da RaouL_BennetH
Grazie :)

Ma non sono legato a windows, potrei provare anche sotto linux, tanto, in termini di incapacità(mia), è uguale :(
Il principio è simile:
CreateFile() <-> open()
GetCommState() <-> tcgetattr()
SetCommState() <-> tcsetattr()
DCB <-> struct termios
ReadFile/WriteFile <-> read/write

RaouL_BennetH
14-12-2004, 21:40
Dove potrei trovare degli esempi che riguardano la comunicazione con la porta seriale, indipendentemente dal sistema operativo usato?

Ho già cercato su planet-source-code, programmers heaven, cprogramming, ma, purtroppo, i sorgenti di esempio sono quasi tutti per Turbo C, ed includono delle librerie che io non ho, tipo dos.h.

RaouL_BennetH
14-12-2004, 23:09
ho trovato questa guida, spero possa servire anche a qualche altro:

http://www.faqs.org/docs/Linux-HOWTO/Serial-Programming-HOWTO.html

cionci
15-12-2004, 00:06
Originariamente inviato da RaouL_BennetH
Dove potrei trovare degli esempi che riguardano la comunicazione con la porta seriale, indipendentemente dal sistema operativo usato?
Non esistono...la comunicazione con la porta seriale non è indipendente dal sistema operativo usato...