My name is Ted Burke (aka username batchloaf). I’m a lecturer in the School of Electrical and Electronic Engineering at the Dublin Institute of Technology (DIT Kevin Street) in Ireland. I teach a range of subjects including robotics, engineering computing, RoboSumo and biomedical engineering.
My primary research interests are signal processing (especially biomedical signals) and human-machine interfaces. I’m also interested in robotics, embedded systems, music technology and toys. I’m a founding member of the tPOT research group, which focuses on what we call ‘People Oriented Technology’ – technology that interacts well with humans.
This blog is really just a scrapbook for bits and pieces of code, notes, ideas, etc that crop up in my research and teaching. I created it for ad hocumentation.
|Loves:||C, Python, PIC microcontrollers, Inkscape, BASH, Horowitz & Hill.|
|Likes:||C++ , MATLAB / Octave, GIMP, gmail, Linux, LaTeX, OpenGL, MSP430 microcontrollers.|
|Useful things:||awk, sed, grep, curl, wget, Notepad++, make, gcc, git, github, gnuplot, matplotlib, .svg, .csv, .pgm, ImageMagick, WordPress, PICkit 2, TI LaunchPad MSP430.|
|Bag of chips:||dsPIC30f4011, PIC18F4620, MSP430Gxxxx, SN754410NE, L293D, LM358, AD627, pl2303hx.|
That looks great. I’ll transfer my DSP blog or some of it to here.
You are very anonymous on this BLOG, no name anywhere???
Hi, i was wandering if you could add to CommandCam the functionality to select the cam by knowing it’s name, something like “CommandCam.exe /devname ‘USB2.0Camera’ “. Best Regards, Alex
Your wish is my command! I’ve just added that feature. See the updated CommandCam page for details.
Thank you!! 😀
You’re welcome. Let me know if you have any problems getting it to work for you. I was only able to try it out with one device name so far since I only have one camera here.
I love commandcam, will willingly pay for camera brightness and contrast control feature added ! Hope u get this Jeff Marc USA
Sorry, the new term has just begun so I’m flat out with teaching duties and working on CommandCam is not an option. To be honest though, I’m not sure if it would be possible adjust brightness and contrast at the image capture stage – I’m not sure if the DirectShow API provides a way of doing that. If it’s sufficient to adjust brightness and contrast after the image is already captured, then I would recommend ImageMagick, which is a free command line image processing program (actually a suite of programs). It’s very powerful and you can find a lot of examples online. Maybe you could use CommandCam to capture the image and then run an ImageMagick command to adjust brightness and contrast?
Other possibilities include VLC and ffmpeg. Both are free to download and I think they would support image capture to a file from a camera device – maybe they would provide a way to control brightness / contrast of the captured image?
Thanks for your reply! I know u are busy.
researching this more, directx commands will support brightness control and other camera supported capabilities , much like TWAIN does wioth scanners. I believe it would be possible to write a camera utility that checks for and then supports all supoprted camera hardware functions, which are queuryable from the device.
filtering after to adjust is not what i’m looking for when the device cleary supports them in other software, which also does not allow command line arguments for .
Oh, that’s interesting to hear! It’s quite a while since I’ve added any new features to the DirectShow / DirectX parts of CommandCam, and I don’t use those APIs anywhere else any more, so I guess I’m a bit out of touch with what they provide. Now that you mention it though, it is kind of ringing a faint bell. Perhaps it was always there, but not many camera drivers used to support those features?
Anyway, sorry I don’t have time to investigate incorporating this into CommandCam. It seems like it would be a very useful feature.
One final note. I too did a little googling to see whether VLC might support what you need. It seems like it might. I just had a dig around in the GUI version and you can “Open capture device” in the File menu and there are advanced options for controlling all kinds of things during capture. As I understand it, pretty much anything you can do in the VLC GUI can be done with the command line version too, allowing you to script the video capture. It’s geared to video rather than still capture, but there’s probably a way to set it to capture just one frame. That’s basically what CommandCam does – it opens a video stream, captures one frame, then closes the stream. VLC is free, stable and well supported and includes a ton of useful features, so it might be worth considering.
Best of luck!
How can I get a hold of you?
You can just leave a message here.
this is powershell sending to comm port I was wondering if you knew the same thing in CMD?
$robojax = New-Object System.IO.Ports.SerialPort COM5,9600,None,8,one
$powerON = 0xA0,0x01,0x01,0xA2
[Byte] $powerON = 0xA0,0x01,0x01,0xA2
I think you can do what you want using my SerialSend program:
SerialSend.exe /devnum 5 /baudrate 9600 /hex “\xA0\x01\x01\xA2”
You can download SerialSend from here:
There’s probably a way to do it without installing anything at all, but I don’t know how exactly off the top of my head.
Hi. can I ask some questions ?
I want use C++ system function to put my file to ftp .
but, I type ftp command I will be a new shell.
How can I use only one line command, put my file to ftp in ms-dos?
Hi Lewis, if you type "ftp --help" in a DOS window you will see a list of additional options for the ftp command. One of these options ("ftp -s:filename") lets you specify a file that contains a list of commands to be run automatically by the ftp program. You can therefore create a file that contains the commands necessary to upload a file to your FTP server and save it in the same folder as your C++ program. Let’s imagine that you call this file "upload.txt". In your C++ program, your system command would then look something like the following:
Obviously, you need to replace "my.ftphost.com" with whatever your FTP server is called.
Unfortunately, I haven’t been able to try this for myself to check the exact details because I don’t have upload access to any FTP servers. If you have a problem getting it working, let me know.
thank you I will try again
The device I’m using (Dual mode USB camera plus) supports two image sizes of 320×240 and 640×480. Is there anyway to select the larger image mode? Currently the device defaults to the smaller image. (When I use the “Select TWAIN device” from any photoeditor, I can change the size to the larger one)
Also thanks for releasing this program to us, the public 🙂
Sorry for the delay in responding. I’m hoping to add this functionality to CommandCam in the next round of development. In the meantime, you could try my other program, RobotEyez which basically allows this. Here’s the link:
RobotEyez is really just a program where I was trying out some different features that I plan to incorporate into CommandCam. To use RobotEyez this way, you would use a command like the following:
That should take a snapshot from the webcam after a 1 second delay, with the resolution set to 640×480, and save it to a file called “frame.bmp”.
I hope that helps!
I have to generate a PWM pulse from a carrier wave which is obtained by adding a triangular wave with a square pulse and this carrier wave should have a frequency of 1kHz frequency. The magnitude of carrier wave should be 2 at 0 seconds, 1 at 0.0005 seconds, 2 at 0.001 seconds and it reduces to -1 at the same moment that is at 0.001 seconds, -2 at 0.0015 seconds and -1 at 0.002 seconds and at the same instant it is increased to 2 and the same pattern repeats. Tthen this carrier is to be compared with a sinewave. Pleas help me to write a C program to generate a pulse. We have a micro controller PIC kit3 DSPIC4011. please help me.
if you give me your mail id, Ican send the diagrams of the carrier waves and pulses which I want to generate so that it will be clear to you.
Hello, I have proposed you for Nobel prize, and I haven’t checked your code on Acer but I would like to make a small proposition, I’m very good in VBA but C++ and other C-s are total mistery for me. If you could help me to adjust code for taking pictures with preview and naming files with date of creation (and not deleting them) I would press that DONATE button so strong it would fall of screen :). Please say yes :). p.s. Sorry my english is not very good but I’m trying :).
I just want to thank you! Your CommandCam tool was a great help!
If we ever meet, I’ll buy you a beer 😉
Greetings from Germany,
Ha ha, thanks Tobias! I’m delighted to hear you found it useful.
Hi batchloaf. From what library are you using the commands _LATD0, _TRISD0, etc ? Thanks.
For dsPIC programs, I use the XC16 compiler (previously called C30). With XC16, you just #include <xc.h> and it automatically pulls in the device specific header file for the specific chip you’re using (which it knows based on the compiler flags / MPLAB project settings). In the device specific header file (e.g. “p30f4011.h”) which is included automatically, the register names (LATD, TRISD, etc) are defined (hardlinked to specific memory addresses) and a number of C language bitfields are defined (LATDbits, TRISDbits, etc) which let you conveniently access individial bits within those registers. The ones you mentioned, such as “_LATD0” and “_TRISD”, are just macros defined as specific elements in the bitfields. For example,
_LATD0 is just LATDbits.LATD0
Hopefully, that explains it? If you have a dig through the header files in the compiler install directory, you’ll find the definitions there in the device specific header file for the chip you’re using.
Link to two-tone reverser image:
I live in Brazil and I’m writing to thank you for the informations that you post here. I never has worked with C on PICs but with your posts, I was able to do my final engineering graduate work last year.
Thanks for your kind words. I’m glad you found it useful!
i would like asking you if you have any idea how to send video through ethernet cables using usb webcam and pic Microcontroller ?
Do you mean that you want to create a device with a USB camera and PIC microcontroller (and no PC) which plugs straight into an ethernet socket and sends the video over the network?
If so, I probably wouldn’t suggest using a PIC, or at least none of the PICs that I have used (PIC16F, PIC18F, dsPIC30F, etc). It’s likely that the PIC32 devices might be more appropriate for this type of application, but I haven’t used them so I can’t say for sure.
If I was doing this myself, I’d probably start with a platform that has good support for hosting USB devices and connecting to Ethernet. For example, maybe you could use something like a Raspberry Pi or a higher end Arduino?
With a Raspberry Pi, you would have Linux on board, so you could just pick a supported USB camera that’s already supported and then use an existing application like VLC or ffmpeg to stream the video over the network. You would just need to install whichever application you need and then plug in the camera and Ethernet cable. That’s the general approach I would recommend.
I’m not saying it’s not possible with a PIC, but personally I’d be terrified of trying to do anything that complicated with any of the PICs I’ve used previously.
Hi, Ted. Thanks for your CommandCam code, I have been working on Directshow weeks, but since the latest Windows SDK never support sample grabber, that stumped me a lot. But thanks for your code, helping me to understand how to make sample grabber working on current SDK. I appreciate for it. 🙂
You’re very welcome! You might want to take a look at RobotEyez also, which is another image capture application I wrote after CommandCam. Here’s the link:
i red and found your blog due to the fact that im looking for a serial-send/receive program.
The serial send program looks very promising but is not completely what i was looking for and also i would need one for receiving data ….. so i came across you blog and red the posts and answer you wrote … here came my idea 😉
Would you be willing to adjust the proggi as well as to write a receiving proggi too ?
And yes i would place a donation too !! 😉
Thanx for any answer,
Can you explain a little bit more about exactly what you need to do? If it’s a specific send-and-receive sequence, such as to control a particular piece of equipment or something like that, it might be easy enough to do a one-off custom program to do it.
To be honest though, when I need to automate some serial interaction with a device – anything involving more than just simple one way communication – I normally just use a Python script to do it, which is really convenient and effective. Python is 100% free to download and it only takes a couple of minutes to install. You wouldn’t need to learn very much at all to begin using it either, but it provides enormous flexibility in return for minimal time investment.
You can see a simple Python serial example in this comment I wrote to Wim on the SerialSend page:
thank you for your fast reply .. i wasn’t in anymore yesterday that’s why my late reply right now.
Ok, the thing is this .. im programming in Filemaker(DB system) for a friend of mine, he’s optician and has an automated phoropter(serial connection).
The thing is 😦 Filemaker doesn’t handle direct send/receive date to the serial connector, that’s why i have to trigger another program to generate a file in case of receiving and also to send a file to the serial port.
So i came across your blog and thought ill ask you 😉
You mentioned python, i just had a small look at it .. looks promising .. but ill have right now no clue of it … i need an exe-file ..is it possible to compile that with python ?
What i know is :
– the serialport should be selectable
– the serial post setting are all the same(parity and so on)
– it should write/read txt-files
and i have to run/call it like :
– sendserial.exe port=1 textfile2send.txt
– receive.exe port=1 textfile2write.txt
is this also possible with python ?
Thanx for your time, i appreciate this very much !!
Ok i got a small script working 😉 ..not bad for a couple of hours reading and testing, since i’ve never done something with python :
import serial ## Load the serial library
## Select and configure the port
ser = serial.Serial(port=0, baudrate=9600, bytesize=8, parity=’N’, stopbits=1, timeout = 5)
## open text file
text_file = open(“workfile.txt”, ‘w’)
text_file = open(“workfile.txt”, ‘r+’)
##read serial port and write into textfile
## close the serial connection and text file
## Close the port so other applications can use it.
print “Info: program closed”
but as you can imagine not completely the way i would like to have it 😦
And most important it should be run as a standalone … and that seems to be tricky
Any comments and help are welcome 😉
Sorry, but I got totally sidetracked and forgot to get back to you. Hopefully you solved your problem during the intervening months. I’ve just been going back through the unread emails in my inbox and I spotted a wordpress update alerting me to your long unread comment! Sorry about that.
By the way, what I used previously to create a standalone exe file from my Python program was a tool called py2exe. It worked really well for me, but I did find it silghtly tricky to configure for some types of program. My guess is that what you were trying to do wouldn’t be too difficult to package as an exe file with py2exe though.
Hai, I saw your code for generating PWM using dsPIC30f. I’ve one doubt regarding the PDC1 value. How did you calculated it?
I suggest you look at this page on my Robotics blog:
Scroll down the section called “PWM – Pulse Width Modulation”. I explain how PDC1 is calculated there.
Hello, Mr.batchloaf i’m newbie with PIC in linux. And i don’t know how to write hex file to PIC i try to using pk2cmd but not work! output: (Program Memory Error)
and my compiler is xc16 .. thz for your recommend
When I installed pk2cmd on Linux previously, I needed to install the libusb-dev package also. After downloading the file “pk2cmdv1.20LinuxMacSource.tar.gz”, these were the commands I ran to install pk2cmd on CrunchBang Linux (very similar to Ubuntu):
To use pk2cmd to transfer a hex file,
My actual pk2cmd command is something like this:
…where “a.hex” is the name of my hex file and I’m using a dsPIC30F4011 microcontroller.
Hopefully, something there will point you in the right direction?
Im a student at the department of ECEE in Greece, specialized in power systems. I found this post of yours very interesting ” https://batchloaf.wordpress.com/2015/03/06/using-a-dspic30f4011-to-generating-4-pwm-signals-with-equal-duty-cycles-but-at-90-degree-phase-increments/ ” while searching for a solution in one of my projects. I want to generate a 6 pwm signals with equal duty cycles but at 60 degree increments to drive a 6 thyristors’ bridge. Is it possible to genarate these signals with my dsPIC30F4011 ?
Thank you in advance.
Can you tell me a bit more about the specific requirements? Such as the following…
1. Is the frequency fixed?
2. What is the frequency?
3. What is the required range of duty cycle?
4. How does the duty cycle vary?
If you can supply this information, I’ll try to think of a way of doing it.
Thank you very much for your interest!
Yes the frequency will be fixed at 300kHz and the duty cycle will be variable(0-100%).
Every pwm signal will have a 60 degree incriments from the previous, so we have 6 pwm signals with 60 degree phase shift from one to another.
Any help will be appreciated!
Sorry, I was snowed under with work and I never got a chance to get back to looking at your problem. Hopefully you got it solved in the meantime?
How can i use PWM port for push-pull mode in your code”PWM output on 2 channels of the dsPIC30F4011 with equal duty cycle and 180 degrees out of phase” but i want to use only PWM1H/1L
Thanks, so much, for comprinter! I use your “serial.c and “comprinter.c” for communications to / from USB virtual COM ports. Your code is the most straightforward, well documented, and seems to be bulletproof. I donated a small amount (all I can afford right now), but my appreciation is much, much greater.
Thanks so much John – both for your kind words and your generous donation! I’m delighted you’re finding ComPrinter useful!
Thank You for providing such an awesome blog….. All the information are useful… Thanks again.
Curious, what would someone the TCP/IP code look like if you were controlling something that had a simple ASCII protocol. I’m thinking like the Cisco VTC Codecs.
They have simple commands like:
xConfiguration SerialPort BaudRate:
Click to access sx80-api-reference-guide-tc71.pdf
I’m afraid I don’t know much about Cisco VTC codecs, so I’m probably not the best person to advise. However, from what I can see in the PDF you linked, it looks like a command line interface is already provided for sending the codes, so perhaps you could just use that if you want to script/automate the process of sending configuration settings?
Thank you so much for these EXTREMELY useful programs. I am now able to build the automated-photogram-turtable-thingimadoodle I’ve been ‘designing’ for years!
You’re welcome!! Automated-photogram-turntable-thingimadoodle, eh? Sounds brilliant – I hope you get it working!
Hi Ted … I want to thank you because your posts motivated me to do some simple videos with dsPIC30F2010 and dsPIC30F4013 microcontrollers. I hope you have some time to see them one day because I put reference your blog.
My channel on youtube is: https://www.youtube.com/user/gapguillen/videos
I am an engineer in electronics and I have the desire to make a sphygmomanometer and / or an ultrasonic nebulizer. I think the technology should be available to every human being regardless of wealth or poverty.
My question is this: You know of any tutorial or useful book about bioelectronics? … Also about programming with dspic30fxxxx or pic18fxxxx?
Greetings from Mexico
Hi Ted. I have just made the break in to DSP after nearly twenty years of programming 16f devices in assembler and now realise that I have to learn C (Which I have been avoiding for a long time until a week ago). Your examples have been useful and I have got a DSpic30f4013 doing simple things as a result, so thanks. There is one thing I can’t seem to find and that is a clear list of the assembler identities ( If that is the correct term) that are needed by C. I note that some have an underscore in front of them e.g. TRSID = 0 then there are others that are written as _LATD0=1, I have looked at the specific inc file in XC16 but it dose not look that simple.
I know just what you mean! Where on earth is the actual list of all those register #defines and so on?! I struggled with this for a long time and I’m afraid I never really identified a document that was quite what I was looking for. Over time, however, I did figure out most of what I was wondering about. The documents I find indispensable are:
From time to time, I’ve also looked at
In a nutshell, any #defined name starting with an underscore (e.g. “_LATA0”) is a bit field with
Both the _TRISAx bits and the TRISAbits bits structure are defined in “p30F4013.h” to refer to the exact same memory location at TRISA (but just one bit out of the 16 in the register).
Hope that helps!
You mentioned in the comments for CommandCam that you might make a version that would support recording a chunk of video from a webcam via command prompt. I’ve looked all over thinking that surely someone would have made a utility that does this, but I can’t find one.
Did you ever look into this? For a specific test I’m running (doing system resets and power cycles), I need to record 1 min of video form a USB webcam on each cycle to verify that the link came up and is working properly. Apparently a single frame capture is not sufficient for this test.
Thanks for any help or response. Great information here!
I’m afraid things just got too busy with teaching to get back to that video idea.
If you’re still looking for a solution, you could try something like VLC. I think it has a command line interface that you can use to launch it to capture to a file. I don’t think I’ve actually used it myself, but I’m pretty sure I’ve seen examples of that online.
Thanks for the reply, Ted. I ended up scripting the Windows Metro Camera app with AutoIt, which works, but it’s pretty clunky. I’ll look into the VLC option, since that sounds potentially much friendlier.
Best of luck with it!
sir i need to sense the magnitude and phase angle of current. I am using dspic30f2010. could you help me in generating a code for it.
sir i need to sense the magnitude and phase angle of current and voltage. I am using dspic30f2010. could you help me in generating a code for it.
how can sense current and voltage magnitude and phase angle. im using dspic30f2010. can you help me i generating the code
how to generate spwm signal code using dspic30f2010.
Can you please add CommandCam to https://chocolatey.org/? I find it very useful for my project, and would love a seamless means to install it on systems.
how to generate pwm sine wave triangle code using dspic30f2010.
Sorry Mounir, I haven’t used that particular dsPIC and I’m not quite sure what you mean by “pwm sine wave triangle”.
Hello! I found the article “writing Bitmap Files in Windows” compiledin MinGW very nice. Because I work in school in Arad Romania with students and we use C++ in MinGW, I wouls ask you to hel p us to READ BITMAP FILES usin MinGW compiler. We would lide to load small files 16 colors bitmap files with no more than 640×480 pixel. I look forwaard for your answer. All the best, Preda Octavian email : firstname.lastname@example.org
I wonder if you could offer any advice…
I’ve been using your SendSerial and ComPrinter tools (which are awesome by the way) to send and receive RS232 commands to a projector. I send a command then immediately launch ComPrinter to listen for a response. This works fine when using the actual serial port on a desktop PC, but fails when using a serial-usb lead. I’m thinking as the USB allows for faster communication and the response is received before ComPrinter is ready? Do you think there’s a a way of handing over from SS to CP more quickly?
Hi Ted, my name is Giuseppe. I’m the author of Imagination, a lightweight DVD slideshow maker. May I send you an email? I couldn’t find it here. Thanks.
thanks for the post but can i use the same code with dsPIC30F2010?
Sorry for the delay in replying. I haven’t had time to keep on top of my comments.
I’m not sure which example you’re referring to, but if the original code is for dsPIC30F4011 then it probably can be adapted without too many changes. The basic register structure is the same for all of the dsPIC30f chips – they just each have a different combination of features built in.
I am working on a project that uses U-center 18.10 to configure a GPS module which is always assigned to COM 3. We need to set the Main Talker ID to GPS and which is easy to do via U-center GUI but I need to automate this, so no manual intervention is required. I am working with Windows 10.
From several blogs i have discovered that i need an header and checksum for any HEX string that I send. I found that U-center GUI actually displays the string that needs to be sent. I have also found out that I need to send another string to save the configuration on the GPS Module. I have found that string from the U-center GUI as well.
There fore I have the two hex strings that need to be sent and they are the following
To configure the protocol
B5 62 06 17 14 00 00 40 00 02 00 00 00 00 00 01 00 01 00 00 00 00 00 00 00 00 75 4F
To save the configuration
B5 62 06 09 0D 00 00 00 00 00 FF FF 00 00 00 00 00 00 03 1D AB
I found an utility called SerialSend.exe which can send the string to the GPS module and i am presently trying to send the hex string with the following command
serialsend.exe /baudrate 9600 /devnum 3 /closedelay 500 /hex “\xB5 \x62 \x06 \x17 \x14 \x00 \x00 \x40 \x00 \x02 \x00 \x00 \x00 \x00 \x00 \x01 \x00 \x01 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x75 \x4F”
It seems to be successful with the following message
Then i send the following command to save the configuration
serialsend.exe /baudrate 9600 /devnum 3 /closedelay 500 /hex “\xB5 \x62 \x06 \x09 \x0D \x00 \x00 \x00 \x00 \x00 \xFF \xFF \x00 \x00 \x00 \x00 \x00 \x00 \x03 \x1D \xAB”
and i get the following message
However, when i open U-center to validate if the commands actually applied, I find that they do not apply. I have tried with the suffixes \r\n but to no avail. I used the following command with the suffixes
serialsend.exe /baudrate 9600 /devnum 3 /closedelay 500 /hex “\xB5 \x62 \x06 \x17 \x14 \x00 \x00 \x40 \x00 \x02 \x00 \x00 \x00 \x00 \x00 \x01 \x00 \x01 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x75 \x4F\r\n”
serialsend.exe /baudrate 9600 /devnum 3 /closedelay 500 /hex “\xB5 \x62 \x06 \x09 \x0D \x00 \x00 \x00 \x00 \x00 \xFF \xFF \x00 \x00 \x00 \x00 \x00 \x00 \x03 \x1D \xAB\r\n ”
I am at a loss at to what to try next. What am i missing or doing wrong to send these HEX strings to the GPS module? All help will be appreciated. Thanks
i want to ask you if i could use the CommandCam, in C programming not c ++ with directshow API, or if there is any alternative for C and works in windows 10. and thank you for your valuable blog keep it up:)
Sorry for the long delay in responding Bakie. Unfortunately, I don’t have a definite answer to this, but I suspect it’s probably not possible to use the DirectShow API with plain C.