Archived

This topic is now archived and is closed to further replies.

Xtremehobo

Serial Port Programming

Recommended Posts

Xtremehobo    820
I suppose I'll just stick my problem right out there: I want to send data like this over a serial port (rs-232?) at 250,000 bits per second: Now, as far as making high's and lows for the appropriate lengths, I know that at ~250,000bps, 4µs is one bit. Now, my question is, If I were to send a stream of bytes such as the first diagram, wouuld my computer automatically insert stop bits, parity bits, etc.. after each byte I send? Is there a way I can gain raw access to the serial port so it doesnt? Thanks, Matt EDIT: To make this easier to understand, I guess all I need is complete control over what's going over the line (i.e. stop bits, start bits, etc..) [edited by - Xtremehobo on March 21, 2004 12:27:08 PM]

Share this post


Link to post
Share on other sites
Xtremehobo    820
Windows XP

If there''s no possible way in XP, I might just code a simple real-mode bootstrap, put it on a floppy, and try to control the serial port from that, but I''d really preffer to find a way in XP.

Share this post


Link to post
Share on other sites
Skizz    794
How bizarre. I've just started writing an application to control a multimeter via the RS-232 connection. It's really simple to implement in Win32:

HANDLE port = CreateFile ("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);

DCB state = {/* com port settings*/};
SetCommState (port, &state);

// receiving data
ReadFile (port, buffer, bytes_to_read, &bytes_read, 0);
// sending data
WriteFile (port, buffer, bytes_to_write, &bytes_written, 0);

CloseHandle (port);

Look in the MSDN for the contents of the DCB structure. You can specify baud rate, number of data bits, number of stop bits, parity and lots of other stuff.

Skizz

EDIT: Should have said, you can't get raw access to the data stream - there's a chip on your motherboard (the UART) which converts data sent to it into a serial stream and vice-versa. It adds (or removes) the start, stop and parity bits as defined by the configuration settings. You can change those settings in the DCB structure.

[edited by - Skizz on March 22, 2004 7:25:17 AM]

Share this post


Link to post
Share on other sites
fractoid    703
You can frob the serial port bits directly under XP, but only if you unlock them. Look at WinIO and the documentation for _outp().

I don't recommend it unless it's a hobbyist project, though - serial ports seem to be a dying breed (eg. most notebooks simply don't have them) so if you're making a product that you want people in general to be able to use, stick to standard RS232 IO, and that way it'll work through USB-to-serial adapters.

[edit: Yes, yes, you can write to the serial port with _inp(), really you can. Yes.]

[edited by - fractoid on March 22, 2004 8:03:52 AM]

Share this post


Link to post
Share on other sites