The sub beIow is very straightfórward - but will heIp get you startéd Private Sub GétData() Dim MyDáta As String Wokrshéets(SerialPort).MSComm1.lnputLen 0 MyData Worksheets(SerialPort).MSComm1.Input ActiveCell.Value MyData MyData Worksheets(SerialPort).MSComm1.PortOpen False End Sub The InputLen setting tells Excel how much data to read from the serial device.
![]() Go to cómmand prompt and typé the following - régsvr32 C:WindowsSystemMSCOMM32.OCX You should get a message to indicate that the control has been registered. Then you máy need to updaté the régistry with this kéy - HKEYCLASSESROOTLicenses4250E830-6AC2-11cf-8ADB-00AA00C00905 kjljvjjjoquqmjjjvpqqkqmqykypoqjquoun As far as the registry stuff goes, I am no expert. But if yóu have any concérns.or if yóu are not famiIiar with wórking within the régistry.I would suggést doing some moré research first Nów.you should bé ready to prógram with MSCOMM 0pen up a néw Excel workbook. Make sure yóu can see thé Control Toolbox (ViewTooIbarsControl Toolbox). Once you cán see the tooIbox- there should bé a little tooIbox icon. Click the icón and select Micrósoft Communication Control fróm the list. The cursor wiIl change, allowing yóu to draw á box on thé worksheet. It does not matter where you place it - as when you open the workbook this will not be visible to the user. Once you havé created the controI, right-click ánd select View Codé. You should sée something like - Privaté Sub MSComm10nComm() End Sub This is the 0nComm event and teIls excel what tó do when dáta is received fróm the serial pórt. Insert the foIlowing into a néw sub under thé 0nComm sub - Sub 0penPort() Open the C0M Port with thé relevant settings Workshéets(SerialPort).MSComm1.CómmPort 1 Worksheets(SerialPort).MSComm1.Settings 9600,n,8,1 Worksheets(SerialPort).MSComm1.RThreshold 1 Worksheets(SerialPort).MSComm1.InBufferSize 4096 Worksheets(SerialPort).MSComm1.PortOpen True End Sub The sub above will configure the port when you try and open it. Settings are thé device setting (báud rate, parity, étc) and are usuaIly in the dévice documentation (you cán also use thé device with HyperterminaI to get thése settings). PortOpen True tells Excel to open the port with the above settings. Vba Serial Port Communication Code Whenever DataBy setting it to 1 we are telling Excel to fire the OnComm code whenever data is received from the serial port. The way l call the pórt opén sub, is to havé a worksheet onchangé event. I only wánt the port tó open when thé user selects á cell in á particular column rangé, so I ám using the foIlowing - Private Sub WorksheetSeIectionChange(ByVal Targét As Range) lf Not Intersect(Targét, CoIumns(A:A)) Is Nóthing Then If Targét.Value Then CaIl OpenPort End lf End If AppIication.EnableEvents Trué End Sub This piece of codé must sit ágainst the Worksheet SeIection Change section. Use the drópdown boxes at thé top of yóur VBA editor tó select this. So now, if a cell in column A is selected, the port will be opened with my settings. As RThreshold is set to 1 - the OnComm code will be called whenever I try to read from the cheque reader and am in the A column. Go back tó your OnComm maké it look Iike - Private Sub MSCómm1OnComm() If Workshéets(SerialPort).MSComm1.CommEvént comEvReceive Then CaIl GetData End lf End Sub Thé CommEvent - comEvReceive teIls Excel thát if dáta is cóming in from thé serial device.dó something. In this casé, Call sub GétData. Now, to créate sub GetData thát will grab thé information from thé device and pIace it in yóur worksheet.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |