linux fopen打开串口,使用C函数fopen,fread和fwrite与串口进行交互?

我一直试图通过RS232串口与我的设备进行通信(在我的情况下是COM6)。我的代码应该向设备写入一串ascii值,然后读取响应,但是我似乎无法得到任何响应。当我将它写入并读取到计算机中的文件时,该程序似乎工作得相当好,但不适用于指定COM6时。这是我的代码的最新版本:

using namespace std;

const char ASCII[ ]= "0123456789ABCDEF";

char *Checksum (char *buffer)

{

static char Hex[10];

static int a1, a2;

register unsigned int i;

int sum;

printf("%s \n", buffer);

sum = 256;

for ( i=0 ; i

{

sum-=buffer[i];

if ( sum<0 )

sum+= 256;

}

a1 = (sum & 0xF0) >> 4;

a2 = sum & 0x0F;

Hex[0] = ASCII[a1];

Hex[1] = ASCII[a2];

Hex[2] = 0;

printf("the checksum is %s \n",Hex);

return(Hex);

}

int main()

{

char data[80], input[80], *data2;

char *response;

DCB dcb;

bool retVal;

DWORD dwBytesTransferred;

DWORD byteswritten;

printf("the variable response is initially: %d\n", response);

dcb.BaudRate = CBR_19200; //19200 Baud

dcb.ByteSize = 8; //8 data bits

dcb.Parity = NOPARITY; //no parity

dcb.StopBits = ONESTOPBIT; //1 stop

//New open port area

HANDLE hPort;

if ((hPort = CreateFile ( "\\\\.\\COM6",

GENERIC_READ | GENERIC_WRITE,

0, // exclusive access

NULL, // no security attrs

OPEN_EXISTING,

FILE_ATTRIBUTE_NORMAL,

NULL)) != INVALID_HANDLE_VALUE)

{

printf("SUCCESS opening the port\n");// success

}

//GetCommState

DCB Dcb;

GetCommState (hPort, &Dcb);

Dcb.BaudRate = CBR_19200;

Dcb.StopBits = ONESTOPBIT;

Dcb.ByteSize = 8;

Dcb.Parity = NOPARITY;

Dcb.fParity = 0;

Dcb.fOutxCtsFlow = 0;

Dcb.fOutxDsrFlow = 0;

Dcb.fDsrSensitivity = 0;

Dcb.fTXContinueOnXoff = TRUE;

Dcb.fOutX = 0;

Dcb.fInX = 0;

Dcb.fNull = 0;

Dcb.fErrorChar = 0;

Dcb.fAbortOnError = 0;

Dcb.fRtsControl = RTS_CONTROL_DISABLE;

Dcb.fDtrControl = DTR_CONTROL_DISABLE;

//Flushing

FlushFileBuffers( hPort );

PurgeComm (hPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);

COMSTAT comStat;

DWORD dwErrorFlags;

ClearCommError ( hPort, &dwErrorFlags, &comStat );

//NEW commtimeouts area

COMMTIMEOUTS CommTimeOuts;

DWORD dwTimeout = 3000; //

if(!dwTimeout)

{ // Don't use timeout -> Read the bytes already in input buffer and return immediately

CommTimeOuts.ReadIntervalTimeout = MAXDWORD;

CommTimeOuts.ReadTotalTimeoutConstant = 0;

} else

{ // Use given timeout, wait until the requested number of bytes are read - or timeout

CommTimeOuts.ReadIntervalTimeout = 0;

CommTimeOuts.ReadTotalTimeoutConstant = dwTimeout;

}

CommTimeOuts.ReadTotalTimeoutMultiplier = 0;

CommTimeOuts.WriteTotalTimeoutMultiplier = 0;

CommTimeOuts.WriteTotalTimeoutConstant = 0;

SetCommTimeouts (hPort, &CommTimeOuts);

printf("insert ASCII code string you wish to send:");

scanf("%s", input);

strcpy(data, "{0x02}");

strcat(data, input);

printf("%s \n", data);

data2=Checksum(data);

strcat(data, data2);

strcat(data, "{0x03}");

printf("the final sent message will be: %s \n",data);

retVal = WriteFile(hPort,data, strlen(data), &byteswritten, NULL);

printf("Number of bytes written: %d\n", byteswritten);

printf("Write Success? %d\n", retVal);

retVal=ReadFile (hPort, &response, 20, &dwBytesTransferred, NULL);

printf("Read Success? %d\n", retVal);

printf("Port Response: %d\n", response);

free(response);

return 0;

}最新发现摘要:使用Habi建议的Free Serial Port Monitor,我现在知道WriteFile运行正常,并且COM6正在接收消息。我仍然在寻找交叉线来仔细检查信息是否正在传送。我想,如果有人可以看看这个新版本,并告诉我是否有任何问题,特别是与ReadFile函数有关,那么我会尽力解决这个问题。它困扰我,免费串行端口软件只显示从我的电脑传来的数据,而不是来自设备的响应。 = \

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值