VC怎么判断串口是否已经打开
用的API函数,比如在关闭串口前,或者打开串口前都需要先判断下是否已经打开,不然会报错,但是怎么判断呢,貌似不像串口控件那么简单...
用的API函数,比如在关闭串口前,或者打开串口前都需要先判断下是否已经打开,不然会报错,但是怎么判断呢,貌似不像串口控件那么简单
展开
2个回答
展开全部
CreateFile能直接判断是否打开.原型:
HANDLE CreateFile(
LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);
DWORD dwCreationDisposition 参数
dwCreationDisposition
[in] Action to take on files that exist, and which action to take when files do not exist. For more information on this parameter, see Remarks. The following table shows possible values for dwCreationDisposition.
CREATE_NEW Creates a new file. The function fails if the specified file already exists.
使用CREATE_NEW,如果存在则失败.
CREATE_ALWAYS Creates a new file. If the file exists, the function overwrites the file and clears the existing attributes.
使用CREATE_ALWAYS,如果存在则覆盖之前的
OPEN_EXISTING Opens the file. The function fails if the file does not exist.For an explanation why you should use the OPEN_EXISTING flag if you are using the CreateFile function for devices, including the console, see Remarks.
使用OPEN_EXISTING,打开存在的,如果不存在,则失败.
OPEN_ALWAYS Opens the file, if it exists. If the file does not exist, the function creates the file as if dwCreationDisposition were CREATE_NEW.
使用这个,打开存在的,如果不存在,则创建.
TRUNCATE_EXISTING Opens the file. Once opened, the file is truncated so that its size is zero bytes. The calling process must open the file with at least GENERIC_WRITE access. The function fails if the file does not exist.
HANDLE CreateFile(
LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);
DWORD dwCreationDisposition 参数
dwCreationDisposition
[in] Action to take on files that exist, and which action to take when files do not exist. For more information on this parameter, see Remarks. The following table shows possible values for dwCreationDisposition.
CREATE_NEW Creates a new file. The function fails if the specified file already exists.
使用CREATE_NEW,如果存在则失败.
CREATE_ALWAYS Creates a new file. If the file exists, the function overwrites the file and clears the existing attributes.
使用CREATE_ALWAYS,如果存在则覆盖之前的
OPEN_EXISTING Opens the file. The function fails if the file does not exist.For an explanation why you should use the OPEN_EXISTING flag if you are using the CreateFile function for devices, including the console, see Remarks.
使用OPEN_EXISTING,打开存在的,如果不存在,则失败.
OPEN_ALWAYS Opens the file, if it exists. If the file does not exist, the function creates the file as if dwCreationDisposition were CREATE_NEW.
使用这个,打开存在的,如果不存在,则创建.
TRUNCATE_EXISTING Opens the file. Once opened, the file is truncated so that its size is zero bytes. The calling process must open the file with at least GENERIC_WRITE access. The function fails if the file does not exist.
更多追问追答
追问
那关闭串口呢,如果串口没有打开,去关闭的话程序直接就崩溃了
追答
程序设计中,在打开串口的时候,要判断串口是否已经被自己的程序打开,如果已经打开,才能进行关闭,如果是别人占用了串口,你只能提示串口已被占用.进行出错处理.当自己打开过串口了,那你一定保留了串口听句柄,使用这个句柄去关闭它.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询