Simple C or C++ script to read serial port buffer

D
source code for what OS? I've written some stuff in VC++ 6.0, and in VC++ .NET 2005.

Davis Gentry
 
I have need of your code to communicate 2 PCs,
send & receive string. I will be thankful to you for sending me the code, or to anyone who has had the code source prior to me to the following address: said_naanani @ hotmail. com
 
Can someone send me code which reads data from the serial port and stores it in a file? Please help me as it is urgent and I have been trying to do this since a week without much success.
 
J

James Ingraham

C
I'd assume Windows, as in Linux reading the serial port buffer is the same as reading any other file, after all the ioctl stuff is done. No idea for Windows, never got up the cash or the interest to buy a compiler. Too bad, as I have some serial code I could send. I'm in deep lurk mode as this new format makes the list more time and trouble to read and I'm losing interest.

Regards

cww
 
J

James Ingraham

cww: "No idea for Windows, never got up the cash or the interest to buy a compiler."

Visual Studio Express is free. However, I find it very difficult to use. It turns out that half the function calls I use are actually POSIX, not ANSI C. On top of that, Windows has small little differences in how it handles everything. So I have trouble even with "Hello, World!"

cww: "I'm in deep lurk mode as this new format makes the list more time and trouble to read and I'm losing interest."

I'm sorry to hear that; we'd miss you. I think the format's a mixed bag. I like that I don't scroll through 7 messages on the main page that are all in the same thread, and that means that any given thread says "alive" on the main page longer. But I agree that it's harder to use.

-James Ingraham
 
As far as compilers go, gcc works on MS Windows. You have to figure out how to get cygwin or mingw working though, which I've never had any luck with. You would also need a good communications library though, as serial ports don't work very well in MS Windows at the OS level.

The easiest way to do any serial communications with MS Windows is to use the pyserial library with Python. You also need the pywin32all libraries to make up for missing functionality in Windows (and possibly hunt down a copy of mfc71.dll if that is missing from the MS Windows installation). I just went though this today again on a project.

Here's a simplified example to read a serial port. This works exactly the same in either Linux or MS Windows (it probably also works on Mac OS/X, BSD, Solaris, etc.)
<pre>
import serial

# Open a serial port.
ser = serial.Serial(_TTY_PORTNUMB,
baudrate = _TTY_BAUDRATE,
bytesize = serial.EIGHTBITS,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
xonxoff = 0,
rtscts = 0,
timeout = _TTY_TIMEOUT)

# Check for something in the buffer.
buffwaiting = (ser.inWaiting() != 0)

# Read the port.
sreply = ser.read(_TTY_BUFFLENGTH)

# Close the port.
ser.close()
</pre>
 
C
Of course, there's also the problem of having to have Windows on a machine. I suppose I'd have to buy that. I haven't had any on the premises since 3.1. I just recently scrounged a Compaq Armada that was being thrown away at a "recycling" event because a 1 GHz processor isn't enough to run current Windows.

I have it running Ubuntu Linux, which runs like a dream on this "obsolete" laptop, but I've toyed with the idea of getting a spare drive and having 98 or XP on it in case I need to run any automation software.

But the idea of my actually supporting the monopoly is revolting enough that I haven't let it become a reality. I should check with my employer to see if I can load a compiler on one of their machines. They've been on a "security" jag lately, so I doubt they would agree. I guess I just don't have enough interest to overcome the obstacles and hassles to run something I really don't like anyway.

I suppose it would be different if I was like most people and didn't have or know anything else, but I really don't have any need for MS except at work to run PLC stuff and by the end of the day, I've usually had enough of machines with nothing interesting on them and am really happy to get back to a fully stuffed Linux environment. I've got compilers and interpreters for any language I've ever messed with and utilities and applications to do anything I've ever
needed to do. I feel really powerless running Windows, which usually has very little in the way of software or tools due to cost and risk.

Koyo has a new PLC that I want to look at, the software is free and the PLC is cheap enough to buy for myself. Maybe I can use that as a vehicle to get a Windows setup going. Or maybe I'll just get a Linux job and live free.

Regards
cww
 
J

James Ingraham

cww: "Of course, there's also the problem of having to have Windows on a machine. I suppose I'd have to buy that."

Yes. Although if you buy a system it's virtually impossible to get it without Windows. MS can give their dev tools away (or at least a trimmed-down version) for free because they don't really make money off of tools anyway. In fact, they don't make money off of ANYTHING except Windows and Office.

cww: "...the idea of my actually supporting the monopoly is revolting enough... "

Doesn't bother me that much. I mostly find it humorous.

cww: "I really don't have any need for MS except at work to run PLC stuff"

And there's the problem. ALL of the software I use at work is Windows only. AutoCAD, our CRM software, anything having to do with PLCs, drives, or anything else in the automation world, etc. So I'm stuck. And at home I play games, so I'm stuck there, too.

"I feel really powerless running Windows, which usually has very little in the way of software or tools due to cost and risk."

This is a surmountable problem, I think. You can get pretty much everything you need for Windows, often for free. But it is DIFFERENT, so it's a lot like learning a (human) language. You're a bright, interesting person in English. Then you try and learn Norweigian and all you can say is "Where's the library?"

Granted, IT lockdown is an issue. There are two ways around this. (1) Don't work for a company with IT lock down, or (2) raise such a huge stink with your manager that IT is getting in your way that eventually IT will stop "helping" you.

As a side note, I just spent an hour trying to figure out why a customer was telling us to look at something on a TIFF file that clearly wasn't there. It turns out that Microsoft Office Document Imaging (MODI) has some weird annotation function that works only with MODI itself. You can view the TIFF in any viewer, but the annotations don't show up, and you have no idea that you're missing anything. All this in a supposedly open format. Thanks again, Microsoft.

Thread is now thoroughly hi-jacked, but oh, well.

-James Ingraham
Sage Automation, Inc.
 
James Ingraham: "MS can give their dev tools away (or at least a trimmed-down version) for free because they don't really make money off of tools anyway."

Reply: I believe Microsoft's developer tools division is profitable. Software development companies that are "Microsoft only" shops tend to pay annual per seat licensing for a subscription. The fees can be pretty stiff if you have an office full of developers. However, it's the only way to get a lot of the developer tools. The way things are licensed, you end up paying for a lot of things you don't want just to get the few things you need. That's part of the joys of product bundling which keeps the competition away.

A lot of the cut-down versions Microsoft gives away are missing important pieces, or they have licensing restrictions prohibiting "commercial use". They are often licensed for hobbyist or student use only. A lot of people who use the cut-down versions for paying work are technically committing software piracy by doing so. Microsoft has successfully sued people over this. These licenses change with just about every release, so its hard to give a current example. Just make sure you read the license in detail.

James Ingraham: "In fact, they don't make money off of ANYTHING except Windows and Office."

Reply: That is largely true. It's just that developer tools is one of the few exceptions.
 
C
Actually, a one day event (and extreme boredom with the whole proprietary meadow muffin) has suggested a stopgap which I will try. Since none of the majors has had the courage to support Linux and bear the Wrath of Redmond, I will attempt to continue to exclude Windows from these premises yet run PLC programming software that at least recognizes that paying big bucks discourages the sales of their hardware.

In a one day event, Codeweavers (a local OSS house) made good on a bet that the price of gas would not go down below $3 a gallon. They offered their Crossover version of wine in a unlocked version for free today, one day only. Linux folks like free, so I got a copy or two. I will see if the new AD Click free programming software will run on it. And if it will, I'll buy some Click gear and make every effort to do appropriate PLC projects on it. And yes, I know I already have wine, but the Crossover version has installers, etc. that will make this more of a whim and less of an endeavor. It doesn't help withe C++, but I'm not that OO anyway. I may also try the GE Duras stuff. This way I can run PLC programming software without running a virus and malware collector.

Regards

cww
 
J

James Ingraham

M Griffin: "I believe Microsoft's developer tools division is profitable."

It's hard to tell now, thanks to the way they've reorganized their statements. But from looking at http://www.microsoft.com/msft/reports/ar08/10k_fr_dis.html I stand by my statement. It's not a question of whether it's profitable. As an example, the Entertainment and Devices Division (read: Xbox) made $426 million. That's technically profitable. But "Client" (read: Windows except Server version) pulled in $13 BILLION, and "Microsoft Business Division" (read: Office) made $12.3 BILLION. (That's profit, not revenue.) The development tools are under "Server and Tools," which includes Windows Server and SQL Server. That division had $4.6 billion in profit; not too shabby. How much of that is tools? They don't say, but I think it's dwarfed by the Server stuff. Tools are essentially insignificant to the bottom line of $22.5 billion. (If you added up the numbers above you get a lot more than $22.5 billion; they lost over $1 billion in "Online Services Business" and spent almost $7 billion on "Corporate-Level Activity.")

M Griffin: "The fees can be pretty stiff if you have an office full of developers."

Yeah. It's a lot of money to us. But it's peanuts to Microsoft.

M Griffin: "A lot of people who use the cut-down versions for paying work are technically committing software piracy by doing so... These licenses change with just about every release, so its hard to give a current example. Just make sure you read the license in detail."

No kidding. This is one of the biggest advantages of Free/Libre/Open Source Software to me. I don't mind paying for software. But the hassle of keeping up with license counts (and types) costs me a ton.

James Ingraham: "In fact, they don't make money off of ANYTHING except Windows and Office."

M Griffin: "That is largely true. It's just that developer tools is one of the few exceptions."

Recapping, I'm not saying it makes or looses money. I'm saying the money it makes or looses doesn't matter. It's a rounding error to a 60 billion dollar a year company. They don't even bother to break out the numbers they're so small.

-James Ingraham
Sage Automation, Inc.
 
J

James Ingraham

As a follow up, here's some info from the Visual Studio Express FAQ at
http://www.microsoft.com/express/support/faq/

"Q: Can I use Express Editions for commercial use?
A: Yes, there are no licensing restrictions for applications built using Visual Studio Express Editions."

"Express Edition products are designed for hobbyists, students, and novice developers. As such, they lack the full breadth of features found in higher-end Visual Studio and SQL Server Editions."

As Michael points out, the licensing may change out from under you at any moment, so it pays to be careful. And the free (as in beer) version of Visual Studio Express (and many other examples as well) are crippled versions of the real thing.

Having said all that, if you need a compiler in Windows you can get it with no cost beyond that of the Windows license itself.

-James Ingraham
Sage Automation, Inc.
 
In reply to Curt Wuollet, be prepared for an endeavour to get anything working on WINE/Codeweavers that hasn't been done already by someone else. I don't want to discourage you, because I would like to see that working as well. I would however suggest either setting it up in a VM (QEMU or Virtualbox) and making snapshot copies of the VM image or else making copies of the WINE directory at each step as you go along. That way if you run into trouble at one point you can go back to the last working version by just copying a file. WINE's compatibility with MS Windows includes having the WINE registry remembering things you would rather it forgot if an installation didn't complete successfully.

I had a brief look at the AD Click after reading your post. It looks like a very interesting new product. I suspect that it's the first product in a new generation of PLCs for Koyo. It looks like they've decided to break backwards compatibility so they can jettison the older BCD instruction set baggage. The addressing and numbering systems are now decimal (16 bit natural binary) and they don't have three or four variations on each instruction. The general design however is still similar to the older models, just simplified.

They keep emphasising that there are "only 21 instructions", but there are actually 21 *types* of instruction. Each type can have several different instructions. For example, they count "timers" as one instruction, whereas there are actually 4 different timers. Overall, the instruction set looks fairly complete. I would have preferred that they had kept some form of stage relay instruction though.

I have been using the Koyo DL205 as the initial model for the soft logic system that I have been working on. A big problem for me has been the number of instructions (three or four PLC product generations all rolled into one) which makes testing a major effort. Having looked at this, I might switch to modelling the first version on the Click (I can still add other models later).
 
C
I played around a bit with their software and came away with the impression that their free software is better than their non-free software. It also seems a bit better than the GE Durus free software which seems quite a bit rougher around the edges. The great part is that I can do most of the work I do lately on these low buck platforms which make many more small projects practical. I do a lot of conveyor sorting and the Click has the shift register and other instructions to handle this. Being new, they all seem to have enough speed for typical stuff. Of course, they probably can't buy any really slow processors any more.

Regards

cww
 
Top