2014年12月23日星期二
关于CLR的runtime的一个小记录
如果我们使用引用这个通过CLR编译的dll,查看它的属性,也会发现,如下图所示的,这个dll的Runtime Version是v4.0319。
如果我们的项目使用的不是.NET framework 4.5,使用上面所述的CLR编译的项目中的namespace就会出现下面的报错:
Error 492 The type or namespace name 'XXX' could not be found (are you missing a using directive or an assembly reference?)
最简单的解决的方法就是修改我们项目的Target Framework,将其改为4.5就可以了。
2014年8月12日星期二
设置GigEVision相机使用固定IP
在GigEVisionTransportLayer里面:
GevCurrentIPAddress 169.254.1.10
GevCurrentSubnetMask 255.255.0.0
GevPersistentDefaultGateway 0.0.0.0
GevIPConfiguration PersistenIP
在UserSets里面,将上面的设置储存,下次启动相机的时候上面的设置将会被使用:
User Set Default Selector UserSet1
set User Save-->Execute
如果要加快计算机接上GigEVision相机后找到相机的速度
也可以将计算机的Lan Adapter设成固定的IP
譬如说:
IP address: 169.254.200.9
Subnetzmake: 255.255.0.0
Standardgateway: 放空
关于自动专用IP寻址的介绍:
我们上面使用的是Automatic Private IP Addressing (from 169.254.0.0 - 169.254.255.255) 它们的subnet mask value 是 255.255.0.0。其实是Windows 计算机自动专用IP寻址(APIPA)是DHCP故障转移机制。自动专用IP 寻址(APIPA) 协议,就是, 当无DHCP 服务器时,通过APIPA, DHCP 客户端可自动配置IP 地址和子网掩码。
The Internet Assigned Numbers Authority (IANA) has reserved 169.254.0.0 through169.254.255.255 for Automatic Private IP Addressing. As a result, Automatic Private IP Addressing provides an address that is guaranteed not to conflict with routable addresses.
关于使用私有IP的介绍:
http://zh.wikipedia.org/wiki/%E4%B8%93%E7%94%A8%E7%BD%91%E7%BB%9C
在互联网的地址架构中,专用网络是指遵守RFC 1918和RFC 4193规范,使用私有IP地址空间的网络。私有IP无法直接连接互联网,需要公网IP转发。与公网IP相比,私有IP是免费的,也节省了IP地址资源,适合在局域网使用。私有IP地址在internet中不会被分配。
2014年8月5日星期二
使用MP4Box对MP4视频文件加入Chapters
2014年6月14日星期六
VS 2008编译的程序部署的一个问题解决
最近遇到一个问题,在一台机器上使用VS 2008编译的程序无法在其他机器上运行,马上想到了以前提到过的关于Side-by-Side问题, 但是安装了Microsoft Visual C++ 2008 SP1 Redistributable Package (x86)后程序还是无法运行,而且没有什么错误信息。然后使用Windows里的Computer Management里的Event Viewer的Windows Logs发现这个程序无法运行的原因在于其调用的一个动态链接库DLL。然后我查看了一下这个DLL的Manifest,原来多了一个Microshoft.VC90.DebugCRT的依赖,如下图所示。如果我们在机器上没有安装VS 2008当然就会没有Microshoft.VC90.DebugCRT的依赖需要的msvcm90d.dll,msvcp90d.dll,msvcr90d.dll,所以程序就无法运行了。
解决的方法就是使用在release模式下编译,或者如下图所示,修改C++ Project的Property里的Linker-->Debugging选项页,将Generate Debug Info修改成No,将Debuggable Assembly改成No Debuggable attribute emitted,原来如下图a所示,修改后如下图b所示:
现在再编译一次,查看一下新生成的DLL的Manifest可以发现Microshoft.VC90.DebugCRT的依赖已经没有了,现在程序应该可以在其他机器上运行了。
2014年1月8日星期三
优化网卡用于machine vision
以下设定最好是使用Intel network chipset的网卡,并确保使用最新的驱动
我们要做的就是optimize bandwidth,improve efficiency
在Win7下选择Local Area Connection → 鼠标右键,点选Properties → 点击Configure → 选择Advanced Tab
1. set interrupt moderation rate to extreme
2. set maximize jumbo packets
3. maximize number of receive buffers
4. disable power saving on the network card
2013年12月26日星期四
TWINCAT 3使用记录4: EtherCAT "Compatible" 网卡
如果要使用real-time特性,那么需要我们的网卡支持。据我的测试(五台不同的笔记本,一台台式机),所有使用Intel芯片的网卡都支持real-time,其他的芯片组都不支持。网上据说Realtek 的部分芯片组支持,但是我没有测试过。
理论上系统允许我们使用所谓的"incompatible"网卡,但是real-time特性会被限制,而且无法对其安装TwinCAT的real-time Ethernet驱动。
总结最好就是使用EtherCAT "Compatible" 网卡,最简单的方法就是选择带有Intel芯片的网卡。
2013年11月26日星期二
开始使用C++/CLI编程
一直以来一直是使用C++和C#,最近接触了C++/CLI并开始使用它编写程序,已经写了两三千行程序,使用C++/CLI对一个C++和C#程序员来说简直是轻而易举,C++/CLI也会给我们带来很多好处,有了C++/CLI对于.NET平台上的C++来说就是多了一个非常好用的武器,C++在.NET平台中得到极大的补充,所谓“C++/CLI”是静态C++对象模型到CLI的动态组件对象编程模型的捆绑。简而言之就是如何用C++在·NET中编程,而不是C#或Visual Basic,它实现了ISOC++和.NET的无缝连接。像C#和CLI本身一样符合ISO标准。微软的.NET的基础CLR(Common Language Runtime,通用语言运行时)就是CLI的一个实例。
百度百科,维基百科:
http://baike.baidu.com/view/459502.htm?noadapt=1
http://zh.wikipedia.org/wiki/C%2B%2B/CLI
以下内容转载自《C++/CLI in action》
The Common Language Infrastructure (CLI) is an architecture that supports a dynamic language-independent programming model based on a Virtual Execution System.
The role of C++/CLI
C++ has been paired with language extensions before, and the result hasn’t always been pretty. Visual C++ 2005 is the first version of a Microsoft C++ compiler that has implemented the C++/CLI specification. This means three things for the C++ developer:
■ C++ can be used to write applications that run on the .NET Framework. There is no need to learn a totally new language or to abandon all the C++ knowledge and experience built up through years of coding.
■ C++/CLI lets developers reuse their native C++ code base, saving the agony of having to rewrite all the existing code to enable it to run on the .NET Framework.
■ C++/CLI is designed to be the lowest-level language for the .NET Framework. For writing purely managed applications, it’s your most powerful choice; or, as I like to say, “C++/CLI actually lets you smell the CLR (Common Language Runtime).”
C++/CLI. C++ with some additional keywords and semantics that support .NET
One of the core aspects of the CLR is a unified type system called the Common Type System (CTS). The CTS defines the model followed by the CLR for the declaration and usage of CLI types.
What is the .NET Framework?
The .NET Framework provides an abstraction layer on top of the native operating system that allows applications compiled into a special intermediate language called Microsoft Intermediate Language (MSIL) to run under a managed environment— where both execution and memory are managed by various parts of the framework. Its two primary components are the Common Language Runtime (CLR) and an extensive Base Class Library (BCL). The CLR is responsible for the execution of .NET applications, inducing memory management through its built-in Garbage Collector (see the section on memory management for more information), whereas the BCL provides various hierarchical class libraries for creating rich user interfaces (UIs), both for the desktop and for the Web—accessing data, security, web services, and many more commonly-performed programming chores.
2013年11月25日星期一
Ubuntu 12.04无法进GUI mode,无法login解决方法
Ubuntu 12.04 出现 autolaunch error 而且直接booting进入command line,根本无法进GUI mode。如果运行sudo startx会出现类似xauth: error in locking authority file .Xauthority的报错,以下是简单的解决方法:
先删除所有的.Xauthority files
sudo rm -f .Xauthority*
然后sudo startx
如果还有问题
重新安装gnome-desktop-environment
sudo apt-get install aptitude (如果没有装aptitutde)
sudo aptitude install gnome-desktop-environment
####
如果进入GUI的login界面,但是无法使用login的password进入系统:
按CTRL+ALT+F1 然后log in 再运行:
sudo chown -R $USER:$USER $HOME
→ 然后重启就可以了
2013年11月22日星期五
TWINCAT 3使用记录3:建立在PC和slave devices之间的连接
如果发现Scan你的EtherCAT Devices出现这样的报错:
就是有可能没有装上TwinCAT EtherCAT的驱动。
在VS2010或2012里TwinCAT -> Show Realtime Compatible Devices可以找到,如下图所示:
其实就是启动C:\TwinCAT\3.1\System里的TcRteInstall.exe,下图就是这个工具的界面,只需要点击几下鼠标就可以安装好TwinCAT 实时Enternet Adapters。
也有可能有些Ethernet adapter无法装上驱动,使之可以连接EtherCAT device,我有一台Sony的笔记本就无法装上驱动。如果顺利安装后,就可以Scan到连接的设备,并进行连接,注意:在Config Mode才可以进行实时Scan设备,而Run Mode不可以。
2013年11月21日星期四
TwinCAT 3使用记录2:ADS介绍
今天简要地介绍一下ADS和一个简单的实际例程。ADS就是Automation Device Specification,通过使用Beckhoff的ADS接口可以和一些virtual Automation Device或者是Windows的程序相连。如果安装了TwinCAT3可以在C:\TwinCAT\AdsApi\TcAdsDll\Lib\找到TcAdsDll.lib,这就是我们需要的ADS的lib文件。对于ADS接口,最重要的操作就是READ和WRITE了,以下是API的说明。
long __stdcall AdsSyncWriteReq( AmsAddr* pServerAddr, // Ams address of ADS server
unsigned long indexGroup, // index group in ADS server interface
unsigned long indexOffset, // index offset in ADS server interface
unsigned long length, // count of bytes to write
void* pData // pointer to the client buffer
);__declspec( dllexport )
long __stdcall AdsSyncReadReq( AmsAddr* pAddr, // Ams address of ADS server
unsigned long indexGroup, // index group in ADS server interface
unsigned long indexOffset, // index offset in ADS server interface
unsigned long length, // count of bytes to read
void* pData // pointer to the client buffer
);
先启动TwinCAT XAE(VS 2010)也就是VS 2010,新建一个TwinCAT的Project。在SYSTEM里的License选项里可以点击“Activate 7 Days Trial License”,然后会出现以下的对话框,填入生成的Code就完成了注册,用于开发,我们可以一直“Activate 7 Days Trial License”并可以使用TwinCAT 3的完整功能。
如果我们使用了Beckhoff的EtherCAT的IO模块,并通过Ethernet线和PC相连,我们可以将这个设备和TwinCAT相连,在Solution Explorer里的I/O一栏里,展开它,点击Devices,点鼠标右键,点选Scan就可以连接我们的EtherCAT I/O模块了,如下图所示,也可使用工具栏上的Scan图标。
如果发现有正确连接,就会出现下图的对话框。
点选OK,我们就是Active Free Run了。然后在Solution Explorer会看到Device的信息,如下图所示。
如果我们点选譬如Term 2 (EL2004),并展开它,就会看到这个模块的Channel,如果点击其中一个Channel就会看到上面的I/O,点击这个I/O就会在Variable这个Tab里找到ADS info,如下图所示。
接下来我们就要使用这个ADS Info,并使用上面提到的API对其进行读和写。我们可以在以下链接中找到SAMPLE2(写)和SAMPLE3(读),这两个例程使用了SOFT PLC,提供的PLC可以加载到TwinCAT Project里,这里我们不使用PLC而是直接使用我们上面已经连接好的I/O模块:
http://download.beckhoff.com/download/Software/TwinCAT/TwinCAT3/Samples/TC1000-ADS/ADS/C/
#include <iostream>
#include <windows.h>
#include "C:\TwinCAT\AdsApi\TcAdsDll\Include\TcAdsDef.h"
#include "C:\TwinCAT\AdsApi\TcAdsDll\Include\TcAdsAPI.h"using namespace std;
void main()
{
long nErr, nPort;
AmsAddr Addr;
PAmsAddr pAddr = &Addr;
DWORD dwData;// Open communication port on the ADS router
nPort = AdsPortOpen();
nErr = AdsGetLocalAddress(pAddr);
if (nErr) cerr << "Error: AdsGetLocalAddress: " << nErr << '\n';
pAddr->port = 11;// Read the vlaue of the user, who has to be written to the PLC
cout << "Value: ";
cin >> dwData;//Port: 11, IGrp: 0x3040010, IOffs: 0xC10000D1, Len: 1
nErr = AdsSyncWriteReq( pAddr, 0x3040010, 0xC10000D1, 0x1, &dwData );
if (nErr) cerr << "Error: AdsSyncWriteReq: " << nErr << '\n';// Close the communication port
nErr = AdsPortClose();
if (nErr) cerr << "Error: AdsPortClose: " << nErr << '\n';
}
例程中的代码只需进行两处的改动(红笔处),就可以将我们输入的Data通过ADS接口传递给我们的EtherCAT I/O模块,这里使用的port of ADS server,index group in ADS server interface,index offset in ADS server interface,count of bytes to write,就是我们在上图看到的我们读到的ADS info。对于Read的例程SAMPLE3我就不多加描述了,就是使用AdsSyncReadReq这个API,然后给其相对应的ADS info就行。
2013年11月18日星期一
TwinCAT 3使用记录1
我发现TwinCAT 3在一些PC上不能正常使用,研究了半天才发现TwinCAT 3需要Intel’s Virtualization Technology Extentions (VTx)或者AMD (AMD-V)。需要在BIOS启动这个功能。有些电脑的BIOS比较老还必须要flash它才能得到这个功能,当然了这个CPU必须支持Virtualization Technology。
关于x86 Virtualization 详见:http://en.wikipedia.org/wiki/Virtualization_Technology
关于Intel CPU的Virtualization列表详见:http://ark.intel.com/Products/VirtualizationTechnology
2013年11月1日星期五
ASP.NET MVC 4使用技巧9:使用Google Charts和JSON
Google Charts 令人爱不释手,功能强大而且简单美观,文档非常完善。
https://developers.google.com/chart/
这里同时推荐一下Playerground,真是是一个非常好用的“工具”,可以非常方便地进行测试。
http://code.google.com/apis/ajax/playground/
如果想画一个line chart,并使用JSON得到做图的数据,很简单,以下就是一个简单的例子,在我们的.cshtml中添加上我们需要的js代码,在这个例子中,
- 引用jsapi库并使用一系列Google Chart API,进行做图。
- 通过点击一个button激活做图function。
- 通过google.visualization.events.addListener得到点击line的事件。
- 通过$.post('/YourController/GetDrawJsonData', {}, function (d) { … …); }); 从我们的Controller中得到Json数据。
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript">google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
var button_show_all = document.getElementById('show_all');
//-- Set a button onclick event // Multi graph. button_show_all.onclick = function () { var options = { title: 'All Heuristic Methods', colors: ['#E6E6E6'], //colors: ['#93ACE7'], legend: { position: 'none' } //legend: { position: 'top', maxLines: '5' } //series: [{ color: 'blue', visibleInLegend: true }, { color: 'blue', visibleInLegend: true }]};
// Add our selection handler. google.visualization.events.addListener(chart, 'select', lineChartHandler); //-- Chart click event function lineChartHandler() { var selection = chart.getSelection(); if (selection.length > 0) { alert('A Line was selected');}
}
$.post('/Plan/GetDrawJsonData', {}, function (d) { //alert(d); var data = google.visualization.arrayToDataTable(d); // At the end draw the chart.chart.draw(data, options);
});
}
}
</script>
它的html代码就更简单了:
<input id="show_all" type="button" class="btn btn-info" value="Show All Charts" />
<br />
<div id="chart_div" style="width: auto; height: 400px;">
</div>
在Controller中只需要加入生成JSON数据的方法:
public ActionResult GetDrawJsonData(){ List<object> data_todraw = ... ... ...; return Json(data_todraw);}
进阶一下,如果想要通过JSON改变做图的options,譬如说是颜色,其实也很方便,先在js中加入一个chart_opt的arrary:
var chart_opt = new Object();
chart_opt.title = "Heuristic Method";var colors = new Array();
colors[0] = "#93ACE7"chart_opt.colors = colors;
var lengend = new Object();
lengend.position = 'none';chart_opt.legend = lengend;
然后通过一个function得到json数据,这里的row: input_counter是一个输入,我们的Controller的方法可以通过这样的方式得到这个输入:
$.post('/YourController/GetDrawJsonOptionSingle', { row: input_counter },function (f) { //console.log(f); //If for some reason you cannot modify your server side script to provide //a valid JSON in the tags property you could still use eval instead of $.parseJSON:var tags = $.parseJSON(response.tags); var colors_tmp = eval(f);console.log(colors_tmp);
for (i = 0; i < colors_tmp.colors.length; i++) {colors[i] = colors_tmp.colors[i];
}
});
与之对应的Controller里的method,需要使用一个Class里面定义colors的List,最后通过return Json(…)返回JSON数据:
public class ChartOptions
{public List<string> colors;
}
public ActionResult GetDrawJsonOptionSingle(int row)
{
ChartOptions tmp = new ChartOptions();tmp.colors = new List<string>();
for (int i = 0; i < ROW_COUNT_ALL; i++)
{ if (i == row) { tmp.colors.Add("#F30A0A");}
else { tmp.colors.Add("#E6E6E6");}
}
return Json(tmp);}
2013年8月13日星期二
ASP.NET MVC 4使用技巧8:自定义CheckBox及ActionResult的Subtypes
今天简单介绍一下如何编写一个自定义的CheckBox:
<script type="text/javascript">
function unhide(id_name, check_box) {
var check = (check_box.checked) ? "block" : "none";
document.getElementById(id_name).style.display = check;
}
</script>
@Html.CheckBoxFor(model => model.NotifyEmail, new { @checked = "unchecked" , @onclick="unhide('hidden-input-email', this)"})Email Notification<br>
<div id="hidden-input-email" style="display:none">
<p>
@Html.LabelFor(model => model.Email)
@Html.TextBoxFor(model => model.Email, new { @readonly="readonly" })
</p>
<div class="editor-field">
@Html.TextAreaFor(model => model.EmailInfos, new { style = "width: 200px; height: 100px;" })
</div>
上面的View代码,使用了自定义的CheckBox
简单的说明一下:
- @Html.CheckBoxFor(model => model.NotifyEmail, new { @checked = "unchecked" , @onclick="unhide('hidden-input-email', this)"})Email Notification<br>
- 当我们点击这个CheckBox的时候会将隐藏的输入框显示出来,这里使用了一个简单的Javascript,当@onclick的事件被触发时。
- 同时这里使用@checked = "unchecked"设置初始为unchecked。
- 参数:model => model.NotifyEmail是我们对于checkbox的view model binding。
- 我们的TextAreaFor也通过参数:new { style = "width: 200px; height: 100px;" }自定义了大小。
ActionResult有很多种Subtypes,我今天才知道,以前只用过3种。
出处:http://forums.asp.net/t/1448398.aspx
ViewResult - Renders a specifed view to the response streamPartialViewResult - Renders a specifed partial view to the response stream
EmptyResult - An empty response is returned
RedirectResult - Performs an HTTP redirection to a specifed URL
RedirectToRouteResult - Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data
JsonResult - Serializes a given ViewData object to JSON format
JavaScriptResult - Returns a piece of JavaScript code that can be executed on the client
ContentResult - Writes content to the response stream without requiring a view
FileContentResult - Returns a file to the client
FileStreamResult - Returns a file to the client, which is provided by a Stream
FilePathResult - Returns a file to the client
2013年8月11日星期日
ASP.NET MVC 4使用技巧7:加入角色(Roles.AddUserToRoles)和使用者授权
加入角色
1. 加入角色信息到webpages_Roles表中
我们可以在Server Explorer中非常方便地加入角色信息进入webpages_Roles表,如下图所示:
然后加入信息,如下图所示,我们加入5种角色。
2. 在Controller中给注册的用户信息加入角色,需要使用到Roles.AddUserToRoles方法,具体可以参阅:
http://msdn.microsoft.com/en-us/library/system.web.security.roles.addusertoroles.aspx
在下面的例子中,我们在Register方法(位于AccountController.cs中)使用了Roles.AddUserToRoles方法(它也就是一个User, Roles, and Membership static classes ),将得到的model的role信息存入,这里需要对应model和view,关于register的model和view这里就不多写了。
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
your code…. your code
string choosenRole;
if (model.Position == 0)
choosenRole = "Admin";
else if (model.Position == 1)
choosenRole = "Planer";
else if (model.Position == 2)
choosenRole = "Controller";
else if (model.Position == 3)
choosenRole = "Manager";
else
choosenRole = "Employee";
System.Web.Security.Roles.AddUserToRole(model.UserName, choosenRole);
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
your code … your code
return View(model);
}
使用者授权
需要使用membership的role控制这样才能使用,很简单我们使用[Authorize]修饰词
[Authorize(Roles = "Admin,Manager")]
或者
[Authorize(Roles = "Manager")]
[Authorize(Roles = "Admin")]
也可以限定用户,譬如
[Authorize(User="Alex")]
譬如在HomeController.cs中:
public ActionResult Contact()
{
ViewBag.Message = "";
return View();
}


