Question

How to read Data from USB and Serial Port

Asked by: Adabara2009

Can someone help me with code to read data from a sysmex kx-21N heamatology analyzer through the serial RS-232 port? The device send data in ACSII format through the serial port. I need to collect the string of data and extract the test value and store in my database. I am using a USB to Serial cable to connect the device to my pc
I have tried using MScomm in VFP, no data was retrieved. I now want to use VB6.0.
I also need to read barcode into a textbox of the same form from a USB barcode reader.
I am a beginner in VB.
below is my VFP code that did not work

**form init event code
PUBLIC gnFileHandle
 
   * You should replace 'c:\sample.hlp' with your own file name
   IF FILE('c:\FHIapps\sample.txt')  && Does file exist?
      gnErrFile = FOPEN('c:\FHIapps\sample.txt',12)  && If so, open read-write
   ELSE
      gnErrFile = FCREATE('c:\FHIapps\sample.txt')  && If not, create it
   ENDIF
 
   = FCLOSE(gnErrFile)  && Close the file
 
   STORE FOPEN('c:\FHIapps\sample.txt',1) TO gnFileHandle     && Open the file
 
**Command button click code**
PUBLIC instring as character
 
 
* Use COM1.
   thisform.testcom.CommPort = 1
   * 9600 baud, no parity, 8 data, and 1 stop bit.
   thisform.testcom.Settings = "9600,N,8,1"
   * Tell the control to read entire buffer when Input is used.
   thisform.testcom.InputLen = 0
   * Open the port.
 
   * Send the attention command to the modem.
   * Wait for data to come back to the serial port.
   DO WHILE thisform.testcom.InBufferCount = 2
     * Read the "OK" response data in the serial port.
     InString = thisform.testcom.Input
   ENDDO
   STORE instring TO thisform.text7.Value
   
   * Close the serial port.
   thisform.testcom.PortOpen = .F.

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-05-27 at 04:55:22ID24441096
Tags

Com Port

,

USB Communication

,

VB6

,

VFP

Topics

FoxPro Database

,

VB Database Programming

,

DCOM

Participating Experts
1
Points
500
Comments
17

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Barcode and VFP 5
    I would like that my aplication could generate and read barcodes, and put the serial number in a specific field. How can I do that.

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Olaf_DoschkePosted on 2009-05-27 at 16:06:03ID: 24488457

You have a comment "  * Send the attention command to the modem.", but there is no code doing so. Seems you are waiting for a resonse to a sent command never sent.

In fact a USB/RS232 Adapter will work just as a normal COM port, just make sure in the device manager, it's configured to mimic a COM1 port. Some USB devices by default are set up as COM3.

Bye, Olaf.

 

by: Adabara2009Posted on 2009-05-28 at 07:54:52ID: 24493909

Thanks, Olaf
I am not using a modem hence I delete the code for that section. It is a direct connection between the comport aand the rs-232 port of the sysmex usig rs-232 standard db9 cable.

How do i configure usb to mimic com port in device manager? how do i know which usb listed on the device manager correspond to the usb port physically?
If foxpro cannot achive it, send me Vb code to achieve it please.
Thanks again.

 

by: Adabara2009Posted on 2009-06-01 at 03:17:42ID: 24516124

I put some cod eon the commevent of the MScomm1 and recieve empty data whereas when the same data is sent to the inbuilt printer it prints. I still need help. Even if its vb code.

 

by: Olaf_DoschkePosted on 2009-06-01 at 04:14:43ID: 24516408

You said: " I am using a USB to Serial cable to connect the device to my pc"

This cable must come with a driver you installed. Then you'll find a divice listed in device manager under the COM ports section. It's configurable what COM port this USB/COM adapter does mimic. It depends on your devide/driver what option you have. As I don't know what device you use, no chance to give you any code. But it's merely a manual setup anyway.

Bye, Olaf.

 

by: Adabara2009Posted on 2009-06-03 at 23:19:55ID: 24543964

I realize my cable was not properly configured, I used a gender changer on the serial end and seems to have change the pin assignmnet. I have ordered for a db9 female to db9 female type C. will try it and get back to you on the situation. Thanks very much. You need to be awarded some point for the job so far.

 

by: Adabara2009Posted on 2009-06-06 at 00:21:44ID: 24562016

i got the right cable now RS232c connected the equipment to computer through com1 but no data recieved.
Do I need to create a cross over cable? I am wondering, both the equipment and the computer has a male com port. the RS232C cable is a straight cable. i am thinking like in networking there may be need for a crossover of pin 2 and 3 and so on. i need help despirately

 

by: Olaf_DoschkePosted on 2009-06-06 at 03:03:48ID: 24562530

Both male ports? Sounds like you need a null Modem cable, like the ones used for comlink of two computers. Your assumption may be right, because that type of cable has a crosslink of pins, see here for details: http://en.wikipedia.org/wiki/Null_modem

But what you need depends on the device you connect. I know nothing about the sysmex kx-21N heamatology analyzer.

I use Commtools to read electronic scales, that respond to commands send to them. So listening to the port may also be unsufficient and the reason you don't receive anything. Do you have documentation about this analyzer?

Bye, Olaf.

 

by: Adabara2009Posted on 2009-06-06 at 09:37:20ID: 24563484

Thanks
Yes, the manual said when you select the menu from the screen to output to host computer connected over the serial rs232c port, it will out a ascii format to the system. In the iutput screen, when i choose printer it prints through the inbuilt printer, but when i choose host, it will not send any data. My code will activate the OnCom event of MSComm but no data recieved as the buffer will be empty.
Can I use a Serial to RJ45? how do i code that? is network a port?

 

by: Olaf_DoschkePosted on 2009-06-06 at 12:26:31ID: 24564066

Serial to RJ45? I doubt it.

What you should find out is specifcs of the COM configuration needed: Parity Bit or not? Stop Bit or not? What Baud rate? those are essential, so the comport works, it's not as simple as wait for an OnCom event.

Bye, Olaf.

 

by: Adabara2009Posted on 2009-06-08 at 05:56:52ID: 24571315

comport setting on the sysmex kx-21N buad rate = 9600, databit =8 stopbit=1 parity =none. I have the same setting on my code.  I manually send data to host (serial) port to be recieved on the computer comm port. What am I suppose to do? No modem between the two device. is there any need for special hardware?
Thanks, Olaf

 

by: Olaf_DoschkePosted on 2009-06-08 at 11:56:03ID: 24575159

did you already tried different cables? You said you have a 1:1, not crossed, but I already recommended trying ZMODEM... did you?

Bye, Olaf.

 

by: Adabara2009Posted on 2009-06-09 at 01:38:19ID: 24579154

I bought a null modem adapter to convert the cable but still get no result. You said it is not just to listen to onCom event, what do i need to add to the code listed? i am going to get a plug and create a custom cable using the information on the equipment manual and try again. but please, send me a working code that i will try after making the cable.

 

by: Adabara2009Posted on 2009-06-18 at 04:31:30ID: 24656362

I got a null modem cable which worked but am reading only  the first 14 characters out of 131. When I send data again, I will then recieve the full text plus the initial 14. How do I stop that?

Below is my oncomm event code:

  IF This.CommEvent = 2 AND This.InBufferCount >0

      rdata=rdata+this.input    
     
      rdata2=(LEN(this.input))
     
   
    STORE rdata TO thisform.text0.Value
    STORE rdata2 TO thisform.text01.Value
   
       
    MESSAGEBOX("Unknown Data recieved",0+64+0,"OnComm")

    ELSE
        STORE rdata TO thisform.text0.Value
    MESSAGEBOX("No Data recieved",0+64+0,"OnComm")
    ENDIF

 

by: Olaf_DoschkePosted on 2009-06-18 at 10:26:44ID: 24659704

First of all, you shouldn't put a Messagebox in an OnComm event, it compares to  timer event of a timer and will be triggered again while it's still running, that causes bad side effects. To what did you set the RThreshold property? After receiving to rdata, set InputLen=0 to clear the input buffer and you won't receive the same bytes again.

Bye, Olaf.

 

by: Adabara2009Posted on 2009-06-20 at 06:33:00ID: 24673684

Thanks
Olaf
Finally it work. below is the code that worked.

***Init Event code
This.Settings = "9600,N,8,1"
   This.InputLen = 0
   This.CommPort = 1
   This.PortOpen = .T.
 PUBLIC rdata,rdata2, rdata3 as String
 rdata=""
 rdata2=""
 rdata3=""
 ****OnComm event code

     IF This.CommEvent = 2 AND This.InBufferCount >0

      rdata=rdata+this.input        
   thisform.text0.Value=""
thisform.Refresh

    STORE SUBSTR(rdata,1,131) TO thisform.text0.Value
 thisform.command1.Enabled= .T.
    ELSE
          MESSAGEBOX("No Data recieved",0+64+0,"OnComm")
    ENDIF
 

 

by: Adabara2009Posted on 2009-06-20 at 06:37:44ID: 31585724

I have attached the code that worked

 

by: Olaf_DoschkePosted on 2009-06-20 at 09:09:25ID: 24674255

Glad you got it working, thanks for sharing the code.

I wonder what for you declare rdata2 and 3, they are not used here. I wonder why you don't use properties instead of public variables. Eg if you don't put the native MScomm on your form, but first create a class based on olecontrol with the Microsoft Communications Control 6.0 in it, you can add some properties rdata, rdata2, rdata3 to it and use them instead of public vars.

Bye, Olaf.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...