Search code examples
c++unixserial-portfilemaker

C++ Serial Port Question


Problem: I have a hand held device that scans those graphic color barcodes on all packaging. There is a track device that I can use that will slide the device automatically. This track device functions by taking ascii code through a serial port. I need to get this thing to work in FileMaker on a Mac. So no terminal programs, etc...

What I've got so far: I bought a Keyspan USB/Serial adapter. Using a program called ZTerm I was successful in sending commands to the device. Example: "C,7^M^J"

I was also able to do the same thing in Terminal using this command: screen /dev/tty.KeySerial1 57600 and then type in the same command above(but when I typed in I just hit Control-M and Control-J for the carriage return and line feed)

Now I'm writing a plug-in for FileMaker(in C++ of course). I want to get what I did above happen in C++ so when I install that plug-in in FileMaker I can just call one of those functions and have the whole process take place right there.

I'm able to connect to the device, but I can't talk to it. It is not responding to anything.

I've tried connecting to the device(successfully) using these:

FILE *comport;
if ((comport = fopen("/dev/tty.KeySerial1", "w")) == NULL){...}

and

int fd;
fd = open("/dev/tty.KeySerial1", O_RDWR | O_NOCTTY | O_NDELAY);

This is what I've tried so far in way of talking to the device:

fputs ("C,7^M^J",comport);

or

fprintf(comport,"C,7^M^J");

or

char buffer[] = { 'C' , ',' , '7' , '^' , 'M' , '^' , 'J' };
fwrite (buffer , 1 , sizeof(buffer) , comport );

or

fwrite('C,7^M^J', 1, 1, comport);

Questions: When I connected to the device from Terminal and using ZTerm, I was able to set my baud rate of 57600. I think that may be why it isn't responding here. But I don't know how to do it here.... Does any one know how to do that? I tried this, but it didn't work:

comport->BaudRate = 57600;

There are a lot of class solutions out there but they all call these include files like termios.h and stdio.h. I don't have these and, for whatever reason, I can't find them to download. I've downloaded a few examples but there are like 20 files in them and they're all calling other files I can't find(like the ones listed above). Do I need to find these and if so where? I just don't know enough about C++ Is there a website where I can download libraries??

Another solution might be to put those terminal commands in C++. Is there a way to do that?

So this has been driving me crazy. I'm not a C++ guy, I only know basic programming concepts. Is anyone out there a C++ expert? I ideally I'd like this to just work using functions I already have, like those fwrite, fputs stuff. Thanks!


Solution

  • Sending a ^ and then a M doesn't send control-M, thats just the way you write it, to send a control character the easiest way is to just use the ascii control code.

    ps. ^M is carriage return ie "\r" and ^J is linefeed "\n"

    edit: Probably more than you will (hopefully) ever need to know - but read The Serial Port Howto before going any further.