How To Read Com Port

bablobablo Member Posts: 43
I want to connect a scanner to my system and read the data from the port where the scanner is attached.

Comments

  • kinekine Member Posts: 12,562
    Best is to create Automation for that. Or you can use the "Microsoft Communications Control, version 6.0" OCX (without triggers), you cannot create code "when scanner read something, run this trigger). For this you need MSCOMM32.OCX. I will try to upload my application changing COM scanner into Keyboard scanner and with interface to NAV to be able to read data from the scanner.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • ta5ta5 SwitzerlandMember Posts: 1,164
    I worked for 2 projects where scanners were involved. One used the com port automation as kine noted and one used a usb port sending the data direct to the keyboard buffer. Acutally it does not matter wheter you are using com or usb.
    What I want to say is: Its maybe possible to just have the cursor blinking on the field you want and then do your scan ==> Data is written to the field without fuss. Maybe nonsense, but just to think about it.

    Thomas
  • bablobablo Member Posts: 43
    kine wrote:
    Best is to create Automation for that. Or you can use the "Microsoft Communications Control, version 6.0" OCX (without triggers), you cannot create code "when scanner read something, run this trigger). For this you need MSCOMM32.OCX. I will try to upload my application changing COM scanner into Keyboard scanner and with interface to NAV to be able to read data from the scanner.
    can u help me with the code?
  • bablobablo Member Posts: 43
    ta5 wrote:
    I worked for 2 projects where scanners were involved. One used the com port automation as kine noted and one used a usb port sending the data direct to the keyboard buffer. Acutally it does not matter wheter you are using com or usb.
    What I want to say is: Its maybe possible to just have the cursor blinking on the field you want and then do your scan ==> Data is written to the field without fuss. Maybe nonsense, but just to think about it.

    Thomas
    Thomas can u please help me with some codes.
  • ta5ta5 SwitzerlandMember Posts: 1,164
    What type of code?
    Thomas
  • bablobablo Member Posts: 43
    ta5 wrote:
    What type of code?
    Thomas
    An Automation code, that will be able to read the port and sent to a field.
  • ta5ta5 SwitzerlandMember Posts: 1,164
    Sorry, cant send you the automation object, since I have developed it for my last company I worked for.

    What excactly do you want to do with the scanner?
  • bablobablo Member Posts: 43
    ta5 wrote:
    Sorry, cant send you the automation object, since I have developed it for my last company I worked for.

    What excactly do you want to do with the scanner?
    thnks ta5, actually its a scanner to read a bar code of a product and sent it to a field in NAV. i hve written some automation code bt it given some problem.
  • ta5ta5 SwitzerlandMember Posts: 1,164
    Can you configure your scanner to have the data from the scanner right to the keyboard buffer. If yes you don't have to fiddle with com port functions.
  • bablobablo Member Posts: 43
    ta5 wrote:
    Can you configure your scanner to have the data from the scanner right to the keyboard buffer. If yes you don't have to fiddle with com port functions.
    okay thnks
  • kinekine Member Posts: 12,562
    I am not allowed to upload my whole application, but it is easy to create one application or Automation for that (if you are using serial port scanner). Just create new application/automation, use the SerialPort component in VisualStudio 2005, use somewhere this code:
    //sPort1 - System.IO.Ports.SerialPort
    //EmulateKeyb - flag if keyboard emulation is enabled
    
                String Data = sPort1.ReadLine();
                Data = Data.Replace("\r", "");
                IWshRuntimeLibrary.WshShellClass Shell = new IWshRuntimeLibrary.WshShellClass();
                bool bWait = false;
                object Wait = bWait;
                if (EmulateKeyb)
                {
                    Shell.SendKeys(Data, ref Wait);
                }
                else
                {
                    LastData = Data;
                    Shell.SendKeys("^{F12}", ref Wait); //Ctrl+F12
                }
    

    this code will send the data as keyboard keys if emulation is enabled, else it will store the data into buffer and send Ctrl+F12 into keyboart input. Than it is easy to catch this key on your form (through hidden menu button with one item with this shortcut) and call the function to read the buffer (assume Automation and not just simple application).
    Of course you need to configure and open the port first, but it is easy and I know that you will not have problems with that.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • bablobablo Member Posts: 43
    kine wrote:
    I am not allowed to upload my whole application, but it is easy to create one application or Automation for that (if you are using serial port scanner). Just create new application/automation, use the SerialPort component in VisualStudio 2005, use somewhere this code:
    //sPort1 - System.IO.Ports.SerialPort
    //EmulateKeyb - flag if keyboard emulation is enabled
    
                String Data = sPort1.ReadLine();
                Data = Data.Replace("\r", "");
                IWshRuntimeLibrary.WshShellClass Shell = new IWshRuntimeLibrary.WshShellClass();
                bool bWait = false;
                object Wait = bWait;
                if (EmulateKeyb)
                {
                    Shell.SendKeys(Data, ref Wait);
                }
                else
                {
                    LastData = Data;
                    Shell.SendKeys("^{F12}", ref Wait); //Ctrl+F12
                }
    

    this code will send the data as keyboard keys if emulation is enabled, else it will store the data into buffer and send Ctrl+F12 into keyboart input. Than it is easy to catch this key on your form (through hidden menu button with one item with this shortcut) and call the function to read the buffer (assume Automation and not just simple application).
    Of course you need to configure and open the port first, but it is easy and I know that you will not have problems with that.
    thanks kine, pls can u help me on how to get my serial port ID on my local machine
  • kinekine Member Posts: 12,562
    COM1, COM2 ... it is the ID... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • bablobablo Member Posts: 43
    kine wrote:
    COM1, COM2 ... it is the ID... ;-)
    thanks i mean port number
  • kinekine Member Posts: 12,562
    It is written over the port on the case of your PC... if you mean if it is 1 or 2...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • ta5ta5 SwitzerlandMember Posts: 1,164
    Hi Kine
    Maybe a stupid question, but to ask again:
    Why not configure the scanner in a way so the scanner is writing the code directly to the keyboard buffer :?

    Thomas
  • kinekine Member Posts: 12,562
    Yes, you can, but if the scanner is connected to COM, you need some driver or application on the PC which will do that. And if you do not have such a driver with the scanner, there is no way and you need to create your own one... :-) Scanner as a HW is not able do it without SW support...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
Sign In or Register to comment.