2 registered members (dr_panther, Ayumi),
702
guests, and 2
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: serial communication
[Re: charrua]
#236477
11/14/08 13:31
11/14/08 13:31
|
Joined: Oct 2008
Posts: 15 Montevideo, Uruguay
charrua
OP
Newbie
|
OP
Newbie
Joined: Oct 2008
Posts: 15
Montevideo, Uruguay
|
Ok seems to me that no one is using the old serial communication port's. For me they still are the easiest way for communication between the PC and a microcontroler project, excluding a paralel port that is easiest! Here I post some code I done, that's my first attempt to write some code in LiteC, I rewrote the LiteC workshop 2 (i use as a template) so there should be "no good programing technics" but, I'm not a programmer, mine is electronics.
// Simple COM1 test program
// my first try in LiteC
// I supose that some things should be done in a better way
// but after reading the LiteC WorkShop 2 it's enaugh for me!
//
// Autor: Juan Ignacio Odriozola
//
// sorry for my bad english!
//
// Open (or try to) the Communication Port 1
// Sends some characters "Hello !!" twice
// and waits for characters to arrive
// Escape finish the program
#include <acknex.h>
#include <default.c>
#include <windows.h>
typedef struct {
int DCBlength;
int BaudRate;
int Flags;
short wReserved;
short XonLim;
short XoffLim;
char ByteSize;
char Parity;
char StopBits;
char XonChar;
char XoffChar;
char ErrorChar;
char EofChar;
char EvtChar;
short wReserved1;
} DCB;
DCB LPDCB;
typedef struct {
int ReadIntervalTimeout;
int ReadTotalTimeoutMultiplier;
int ReadTotalTimeoutConstant;
int WriteTotalTimeoutMultiplier;
int WriteTotalTimeoutConstant;
} COMMTIMEOUTS;
COMMTIMEOUTS LPCommTO;
char TxBuffer[8];
char RxChar;
char TxChar;
int TxCount;
int NumRx;
int NumTx;
#define GENERIC_READ_WRITE 0xC0000000;
long hPort;
var a = 0;
var b = 0;
var c = 0;
/////////////////////////////////////////////////////////////////////
PANEL* panDisplay =
{
digits(35, 10, "Handle = %0.f", *, 1, a);
digits(35, 19, "BaudRate = %0.f", *, 1, b);
digits(35, 28, "LastRxChar = %0.f", *, 1, c);
flags = VISIBLE;
}
/////////////////////////////////////////////////////////////////////
function main()
{
video_mode = 7;
screen_color.blue = 150;
LPDCB.DCBlength = sizeof(LPDCB);
// COM1, COM2, as you need
hPort = CreateFile ("COM1", // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// Access (read-write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING, // How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute to copy
a = hPort;
if (hPort != 0)
{
SetupComm(hPort,1024,1024); // sets de buffers for Tx and Rx
GetCommState(hPort,&LPDCB); // get the state of the device
// modify some parameters
LPDCB.BaudRate = 9600; // 300, 600, 1200 and so on
LPDCB.Flags = 1; // no hardware, no software handshake
LPDCB.ByteSize = 8; // 8 bits for each char
LPDCB.Parity = 0; // 0=none, should be odd, even, mark, space
LPDCB.StopBits = 1; // depending on ByteSize should be 1, 1.5 or 2
SetCommState(hPort,&LPDCB);
GetCommState(hPort,&LPDCB); // just for verify that the device acepted de BaudRate
b=LPDCB.BaudRate;
GetCommTimeouts(hPort, &LPCommTO); // this function fills LPCommTO structure
LPCommTO.ReadTotalTimeoutConstant = 5; // just modify one of the time out's
SetCommTimeouts(hPort, &LPCommTO); // set the read time out, wait no more than 5 ms for
// a char to arrive
TxBuffer[0]=str_to_asc("H");
TxBuffer[1]=str_to_asc("e");
TxBuffer[2]=str_to_asc("l");
TxBuffer[3]=str_to_asc("l");
TxBuffer[4]=str_to_asc("o");
TxBuffer[5]=str_to_asc(" ");
TxBuffer[6]=str_to_asc("!");
TxBuffer[7]=str_to_asc("!");
TxCount = 1;
// some examples about how to write
// one by one
var i;
for (i=0; i<8; i++)
{
TxChar = TxBuffer[i];
WriteFile (hPort, // Port handle
&TxChar, // Pointer to the data to write
1, // Number of bytes to write
&NumTx, // Pointer to the number of bytes written
NULL // Must be NULL for Windows CE
);
}
//four in one call from offset 0
WriteFile( hPort, &TxBuffer[0], 4, &NumTx, NULL);
//four in one call from offset 4
WriteFile( hPort, &TxBuffer[4], 4, &NumTx, NULL);
}
while (1)
{
if (hPort != 0)
// waits no more than 5 ms for a character arrival
ReadFile (hPort, // Port handle
&RxChar, // Pointer to data to read
1, // Number of bytes to read
&NumRx, // Pointer to number of bytes read
NULL // Must be NULL for Windows CE
);
if (NumRx != 0)
// if one was received then show it
c=RxChar;
wait (1);
}
if (hPort != 0)
CloseHandle(hPort); // don't forget to close the port!
}
I hope that should be helpful for someone else. Saludos Juan
|
|
|
Re: serial communication
[Re: charrua]
#237879
11/23/08 04:29
11/23/08 04:29
|
Joined: Aug 2002
Posts: 164 Houston
Nicholas
Member
|
Member
Joined: Aug 2002
Posts: 164
Houston
|
Thanks. This is the best serial help I've found on here. I am having some issues with it and was wondering if you had the same problem or know what's up. I am using picBasic pro with a direct serial connection on a 18f4550.
I try to send some numbers, but it's not showing up in acknex properly. I added "file_open_write" code to create a log file of my input because it was too fast to see on screen. As one example I am trying to send just one number, but it ends up showing 2 numbers in my log.
serout2 portC.6, 813, [77] 'sends 77 out at 1200 baud
what I end up seeing in acknex (also set up for 1200) was: 60 127 60
as 3 seperate inputs I also tried it with binary, hex, dec. So the code here might be for one of those too, but none of them come through as 77.
any ideas?
Black holes are where God divided by zero.
|
|
|
Re: serial communication
[Re: Nicholas]
#238102
11/24/08 21:47
11/24/08 21:47
|
Joined: Oct 2008
Posts: 15 Montevideo, Uruguay
charrua
OP
Newbie
|
OP
Newbie
Joined: Oct 2008
Posts: 15
Montevideo, Uruguay
|
Hi! It seems that I'm not alone!
first I found a bug in the code I posted.
I define:
#define GENERIC_READ_WRITE 0xC0000000
but then I don't use it:
hPort = CreateFile ("COM1", // Pointer to the name of the port GENERIC_READ | GENERIC_WRITE, // Access (read-write) mode 0, // Share mode NULL, // Pointer to the security attribute OPEN_EXISTING, // How to open the serial port 0, // Port attributes NULL); // Handle to port with attribute to copy
sorry, but the code I posted were not exactly the one I used when I do the test!, so the correct line is:
hPort = CreateFile ("COM1", // Pointer to the name of the port GENERIC_READ_WRITE, // Access (read-write) mode 0, // Share mode NULL, // Pointer to the security attribute OPEN_EXISTING, // How to open the serial port 0, // Port attributes NULL); // Handle to port with attribute to copy
second.
the trouble you have should be the speed. How do you test the 1200? I first do a hypertérminal connection between 2 instances, then between hypertérminal and a code made in LiteC or wathever and finally I test the serial connection between the microcontroller and the hyperterminal or liteC.
When all this doesn't work, take an osciloscope! it never lies.
If you look my code, I set the baudrate and then I read the state of the comm and display specifically the baudrate, because I have told one baudrate and the port were in another and I've getting more than one character as I tx one! Another trouble I had in LiteC was that characters enclosed with ' doesn´t be the ones I wanted till I found str_to_asc! I lool for CHR() and ASC() function of Pascal, Basic etc but LiteC has their own's.
If you have no success in set the baurate, try the Windows configuration that stablish the "Default" and change int to suit your need's. But theoretically you should set the communication parameters the way I do.
Try to place a delay between the set and the get functions to be sure I don´t know if needed.
saludos
Juan
Last edited by charrua; 11/24/08 23:54. Reason: my bad english!
|
|
|
Re: serial communication
[Re: charrua]
#241153
12/14/08 19:23
12/14/08 19:23
|
Joined: Aug 2002
Posts: 164 Houston
Nicholas
Member
|
Member
Joined: Aug 2002
Posts: 164
Houston
|
Thanks for the update. I think I had found out what my problem is. I am trying to directly connect the serial output of my 18f4550 to my pc com port. I don't think this would work for 232 directly. This was probably why I was getting the problems on this program and on my regular com programs and why I could bridge the pins on the serial cable of the pc and still get it to echo properly. On the other hand, I do have another serial device that was home made, that I know sends the right signals that won't work on my pc but works on someone else's, so it could be my drivers and install of windows XP. Either way it would be better for me to use an FTDI chip to convert to 232 and use it as a USB com port, so I ordered a breakout kit from sparkfun and will try that next.
If you have a working .cd folder that could take a few numbers in from a com port, would you mind zipping it and mailing it to me? It would help working backwards from a good starting point software wise so I know that won't be the issue. If you don't have it anymore, I'll try doing it myself. thanks for the help and I'll try the new code when I get that far.
Dylan
Black holes are where God divided by zero.
|
|
|
Re: serial communication
[Re: Nicholas]
#247158
01/19/09 09:03
01/19/09 09:03
|
Joined: Aug 2002
Posts: 164 Houston
Nicholas
Member
|
Member
Joined: Aug 2002
Posts: 164
Houston
|
I got my serial port working properly with a few unexpected values. As a plus, I now have it as a USB device too. Just so you know. GENERIC_READ_WRITE (what you wrote as a correction) doesn't seem to workk. It gave me an error on runtime and I checked the microsoft msdn site and GENERIC_READ | GENERIC_WRITE does seem to be correct if you want to use both. The FTDI serial-to-USB chip I have has some LEDs hooked up to tx and rx. Using these LEDs I can verify that the code works (or at least sends data to the right com port) I still have not hooked it up to a PIC with LCD to verify the right baud rate or anything, but it's a start. // Simple COM1 test program
// my first try in LiteC
// I supose that some things should be done in a better way
// but after reading the LiteC WorkShop 2 it's enaugh for me!
//
// Autor: Juan Ignacio Odriozola
//
// sorry for my bad english!
//
// Open (or try to) the Communication Port 1
// Sends some characters "Hello !!" twice
// and waits for characters to arrive
// Escape finish the program
#include <acknex.h>
#include <default.c>
#include <windows.h>
typedef struct {
int DCBlength;
int BaudRate;
int Flags;
short wReserved;
short XonLim;
short XoffLim;
char ByteSize;
char Parity;
char StopBits;
char XonChar;
char XoffChar;
char ErrorChar;
char EofChar;
char EvtChar;
short wReserved1;
} DCB;
DCB LPDCB;
typedef struct {
int ReadIntervalTimeout;
int ReadTotalTimeoutMultiplier;
int ReadTotalTimeoutConstant;
int WriteTotalTimeoutMultiplier;
int WriteTotalTimeoutConstant;
} COMMTIMEOUTS;
COMMTIMEOUTS LPCommTO;
char TxBuffer[8];
char RxChar;
char TxChar;
int TxCount;
int NumRx;
int NumTx;
#define GENERIC_READ_WRITE 0xC0000000;
long hPort;
var a = 0;
var b = 0;
var c = 0;
var d = 7;
FONT* arial_font = "Arial#26b";
/////////////////////////////////////////////////////////////////////
PANEL* panDisplay =
{
digits(35, 08, "Handle = %0.f", arial_font, 1, a);
digits(35, 48, "BaudRate = %0.f", arial_font, 1, b);
digits(35, 88, "LastRxChar = %0.f", arial_font, 1, c);
digits(35, 128, "DCBlength = %0.f", arial_font, 1, d);
flags = VISIBLE;
}
/////////////////////////////////////////////////////////////////////
function main()
{
video_mode = 7;
screen_color.blue = 100;
LPDCB.DCBlength = sizeof(LPDCB);
d = sizeof(LPDCB);
// COM1, COM2, as you need
hPort = CreateFile ("COM3", // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// Access (read-write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING, // How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute to copy
a = hPort;
if (hPort != 0)
{
SetupComm(hPort,1024,1024); // sets de buffers for Tx and Rx
GetCommState(hPort,&LPDCB); // get the state of the device
// modify some parameters
LPDCB.BaudRate = 1200; // 300, 600, 1200 and so on
//b = LPDCB.BaudRate;
LPDCB.Flags = 1; // no hardware, no software handshake
LPDCB.ByteSize = 8; // 8 bits for each char
LPDCB.Parity = 0; // 0=none, should be odd, even, mark, space
LPDCB.StopBits = 1; // depending on ByteSize should be 1, 1.5 or 2
SetCommState(hPort,&LPDCB);
GetCommState(hPort,&LPDCB); // just for verify that the device acepted de BaudRate
b=LPDCB.BaudRate;
GetCommTimeouts(hPort, &LPCommTO); // this function fills LPCommTO structure
LPCommTO.ReadTotalTimeoutConstant = 5; // just modify one of the time out's
SetCommTimeouts(hPort, &LPCommTO); // set the read time out, wait no more than 5 ms for
// a char to arrive
//Commented because I am testing the transmit in a loop below.
TxBuffer[0]=str_to_asc("H");
// TxBuffer[1]=str_to_asc("e");
// TxBuffer[2]=str_to_asc("l");
// TxBuffer[3]=str_to_asc("l");
// TxBuffer[4]=str_to_asc("o");
// TxBuffer[5]=str_to_asc(" ");
// TxBuffer[6]=str_to_asc("!");
// TxBuffer[7]=str_to_asc("!");
//
// TxCount = 1;
//
// // some examples about how to write
// // one by one
// var i;
// for (i=0; i<8; i++)
// {
// TxChar = TxBuffer[i];
// WriteFile (hPort, // Port handle
// &TxChar, // Pointer to the data to write
// 1, // Number of bytes to write
// &NumTx, // Pointer to the number of bytes written
// NULL // Must be NULL for Windows CE
// );
// }
//
// //four in one call from offset 0
// WriteFile( hPort, &TxBuffer[0], 4, &NumTx, NULL);
// //four in one call from offset 4
// WriteFile( hPort, &TxBuffer[4], 4, &NumTx, NULL);
}
while (1)
{
// send this data over and over to make sure there is a connection and a stable one
TxChar = TxBuffer[0];
WriteFile (hPort, // Port handle
&TxChar, // Pointer to the data to write
1, // Number of bytes to write
&NumTx, // Pointer to the number of bytes written
NULL // Must be NULL for Windows CE
);
//four in one call from offset 0
WriteFile( hPort, 77, 4, &NumTx, NULL); // <-- not sure if 77 shows up, but it does sent some data
/// where is the bracket set for the following if? or doesn't it need it in lite C code
if (hPort != 0)
// waits no more than 5 ms for a character arrival
ReadFile (hPort, // Port handle
&RxChar, // Pointer to data to read
1, // Number of bytes to read
&NumRx, // Pointer to number of bytes read
NULL // Must be NULL for Windows CE
);
if (NumRx != 0)
// if one was received then show it
c=RxChar;
//WriteFile( hPort, &TxBuffer[0], 8, &NumTx, NULL);
if(key_esc == 1) // <--- this probably also needs to recognize any termination of the program
{
if (hPort != 0)
CloseHandle(hPort); // don't forget to close the port!
}
wait (100); // <-- long enough to see the LED blink
}
} I had to change mine to com3 for my device. This could probably have a string dependant on what com port the user chooses, same with the baud rate. I noticed a few things though. 1. under createfile you have 0 in the port attributes, I don't see that as an option on the msdn website, but I would assume it's because it's connecting to hardware and not a file itself 2. every time I connect I get a different number under "a" the Handle why is this 3. It has 9600 listed as the baud in the code, but 1200 is what shows up in the text panel. 4. what is the DBClength and why is it 28, does it need to be that number? I just thought you'd want to know the progress, especially if you haven't done much with it yet. Also, to anyone wanting to get into serial communication with your own device, I really would suggest looking into those FTDI chips. The sparkfun breakout board for it is so freaking easy, you don't even have to solder on that tiny little chip. This is the one I used it seems to be out of stock. this one should work too. and they're cheap too.
Black holes are where God divided by zero.
|
|
|
Re: serial communication
[Re: MMike]
#270962
06/10/09 18:01
06/10/09 18:01
|
Joined: Oct 2008
Posts: 15 Montevideo, Uruguay
charrua
OP
Newbie
|
OP
Newbie
Joined: Oct 2008
Posts: 15
Montevideo, Uruguay
|
hi! I returned from death.
i been working in other thign's and because is the firs time that i look in 3dgs forums since a year, i look in this thread. I hope you still have interest in that thing's. This weekend i look carefully what you posted and try to answer if i can or at leas colaborate with you. best regards Juan
edit#1 here some in advance
1) port_out is for outputing data to a parallel por (serial is other thing)
2) what you mean with "connect pin" if you wish to use the serial port, you have to have a serial device on the other end of the wire (wich is the case of nicholas that uses a microcontroller to play with) the serial connector is db9 male and the pins used to do a simple tx/rx are 2, 3 and 5. pin 5 is the common or ground, pin 3 is Transmit and pin 2 is receive.
Nicholas 1) probably is for that 2) it's a handle, operating system says that number and we have to use it and just that! 3) i dont rembember exacly what my code does (i look the forum but not my work! sorry) but, b=LPDCB.BaudRate is for read the baud rate of the device. so if the pannel says 1200, the device is working at 1200 or we have write/read the configuration wrong. 4) DCBLenght is the lenght of the structure and it is fixed. is required for so many calls that the firs integer contains the lenght of the structure and is required to be exactly that always in this case (because a device control block is fixed)
Last edited by charrua; 06/10/09 18:44.
|
|
|
Re: serial communication
[Re: charrua]
#270996
06/10/09 19:15
06/10/09 19:15
|
Joined: Oct 2008
Posts: 15 Montevideo, Uruguay
charrua
OP
Newbie
|
OP
Newbie
Joined: Oct 2008
Posts: 15
Montevideo, Uruguay
|
hi again
i do prefere to out an in data via a parallel port but, that kind of port tends to dissapear (lamentably). Out_port only is supported in comercial version as i saw and has another trouble: accesing hardware directly is not well seen by the operating sistem so the nt based windows restrict that kind of instructions. I saw something that this command doesn't work porperly under vista or even xp don't remember.
about serial communication, just for test you should conect two ports on the same machine, or with two machines if you have, one instance running an hyperterminal and the other with a software made in litec to test. One important thing is that if you have a usb-to-serial converter both cables are terminated with a male connector and we have to do (construct or buy) a crossover cable that has two female connectors with pin 2 connected with 3 and 3 with 2 and 5 with 5.
if you want to do some thing like tun on/off lights or sense key's etc, you probably need a microcontroller on the other end. If this is the case you have to have a development board or if you has some soldering skills i could send some schematics to work with PIC's: microcontrllers from Microchip.
there are some cheap programmers or should be home made with few components. about the code for the microcontroller, i'm a teacher in this area and have some code that i give to my student's so i could e-mail you. If you win 1000000 dolars (beter if euros) send me some please! (je)
best regards Juan
Last edited by charrua; 06/10/09 19:18. Reason: bad english
|
|
|
|